# 数值比较 if [ ${ans_lines} -eq ${lines} ] then echo "lines is ok" else echo "lines is not ok" fi # 判断目录是否存在(HDFS) hdfs dfs -test -d ${HDFS_path}/wap if [ $? == 0 ]; then hdfs dfs -rmr ${HDFS_path}/wap fi
for 循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for i in {1..10}; do echo $i; done # 边界为变量时用 seq END=10 for i in $(seq 1 $END); do echo $i; done # 带步长 INTERVAL=2 for i in $(seq 1 $INTERVAL $END); do echo $i; done
for i in {2..5}; do tablesuffix=$i echo $tablesuffix sleep 1 done
encrypt() { local str=$1 # Use PBKDF2 with 10000 iterations for better security echo"$str" | openssl enc -aes-256-cbc -a -pbkdf2 -iter 100 \ -S "1234567890ABCDEF" \ -iv "0123456789ABCDEF0123456789ABCDEF" \ -pass pass:"${ENCRYPTION_KEY:-default_key}" }
decrypt() { local str=$1 # Use PBKDF2 with 10000 iterations for better security echo"$str" | openssl enc -aes-256-cbc -a -d -pbkdf2 -iter 100 \ -S "1234567890ABCDEF" \ -iv "0123456789ABCDEF0123456789ABCDEF" \ -pass pass:"${ENCRYPTION_KEY:-default_key}" }
CONFIG_FILE="config.dat" ENCRYPTION_KEY="your_encryption_key_here"# Change this to your secure key SEPARATOR="|"# Separator for config file entries
# Function to parse user@host format parse_user_host() { local input=$1 local username=$(echo"$input" | cut -d'@' -f1) local hostname=$(echo"$input" | cut -d'@' -f2) echo"$username|$hostname" }
# Function to add new SFTP configuration add_config() { echo"Enter connection string (format: user@host):" read -r connection_string
if [[ ! "$connection_string" =~ ^[^@]+@[^@]+$ ]]; then echo"Invalid format. Please use user@host format" exit 1 fi
# Set default nickname as username if not provided echo"Enter nickname (press Enter to use '$username' as default):" read -r nickname nickname=${nickname:-$username}
# Set default port as 22 if not provided echo"Enter port (press Enter to use default port 22):" read -r port port=${port:-22}
# Alternative add_config function that accepts direct parameters add_config_direct() { if [ "$#" -ne 4 ]; then echo"Usage: $0 add-direct <port> <host> <user> <password>" exit 1 fi local port=$1 local host=$2 local username=$3 local password=$4 local nickname=${username} # If nickname already exists, append a random number if [ -f "$CONFIG_FILE" ]; then if grep -q "^$nickname$SEPARATOR""$CONFIG_FILE"; then nickname="${nickname}_$(date +%s)" echo"Nickname already exists. Using '$nickname' as nickname." fi fi # Encrypt sensitive data encrypted_user=$(encrypt "$username") encrypted_pass=$(encrypt "$password") # Add to config file echo"$nickname$SEPARATOR$host$SEPARATOR$port$SEPARATOR$encrypted_user$SEPARATOR$encrypted_pass" >> "$CONFIG_FILE" echo"Configuration added successfully!" }
# Function to get configuration by nickname get_config() { local nickname=$1 if [ ! -f "$CONFIG_FILE" ]; then echo"Config file not found!" exit 1 fi # Get the line for this nickname local config_line=$(grep "^$nickname$SEPARATOR""$CONFIG_FILE") if [ -z "$config_line" ]; then echo"Nickname not found!" exit 1 fi # Parse the line using separator host=$(echo"$config_line" | cut -d"$SEPARATOR" -f2) port=$(echo"$config_line" | cut -d"$SEPARATOR" -f3) encrypted_user=$(echo"$config_line" | cut -d"$SEPARATOR" -f4) encrypted_pass=$(echo"$config_line" | cut -d"$SEPARATOR" -f5) # Decrypt sensitive data user=$(decrypt "$encrypted_user") password=$(decrypt "$encrypted_pass") }
# Function to list all configured nicknames list_configs() { if [ ! -f "$CONFIG_FILE" ]; then echo"No configurations found." return fi echo"Available SFTP configurations:" while IFS="$SEPARATOR"read -r nickname host port encrypted_user _; do if [ ! -z "$nickname" ]; then user=$(decrypt "$encrypted_user") echo" $nickname -> $user@$host:$port" fi done < "$CONFIG_FILE" }
# Function to list remote directory sftp_ls() { local nickname=$1 local remote_path=${2:-.} get_config "$nickname" sshpass -p "$password" sftp -P "$port""$user@$host" << EOL cd "$remote_path" ls bye EOL }
# Function to download files/directories sftp_get() { local nickname=$1 local remote_path=$2 local local_path=${3:-.} get_config "$nickname" if [[ "$remote_path" == */ ]]; then # It's a directory sshpass -p "$password" sftp -P "$port""$user@$host" << EOL get -r "$remote_path" "$local_path" bye EOL else # It's a file sshpass -p "$password" sftp -P "$port""$user@$host" << EOL get "$remote_path" "$local_path" bye EOL fi }
# Function to upload files/directories sftp_put() { local nickname=$1 local local_path=$2 local remote_path=${3:-.} get_config "$nickname" if [ -d "$local_path" ]; then # It's a directory sshpass -p "$password" sftp -P "$port""$user@$host" << EOL put -r "$local_path" "$remote_path" bye EOL else # It's a file sshpass -p "$password" sftp -P "$port""$user@$host" << EOL put "$local_path" "$remote_path" bye EOL fi }
# Main menu case"$1"in "add") add_config ;; "add-direct") shift# Remove first argument add_config_direct "$@" ;; "list") list_configs ;; "ls") if [ -z "$2" ]; then echo"Usage: $0 ls <nickname> [remote_path]" exit 1 fi sftp_ls "$2""$3" ;; "get") if [ -z "$3" ]; then echo"Usage: $0 get <nickname> <remote_path> [local_path]" exit 1 fi sftp_get "$2""$3""$4" ;; "put") if [ -z "$3" ]; then echo"Usage: $0 put <nickname> <local_path> [remote_path]" exit 1 fi sftp_put "$2""$3""$4" ;; *) echo"Usage: $0 {add|list|ls|get|put} [arguments]" echo"Commands:" echo" add Add new SFTP configuration" echo" add-direct Add with direct parameters <port> <host> <user> <password>" echo" list List all configured SFTP connections" echo" ls <nickname> [path] List remote directory contents" echo" get <nickname> <remote_path> [local_path] Download file/directory" echo" put <nickname> <local_path> [remote_path] Upload file/directory" exit 1 ;; esac
tmp = set() lines = output.split('\n') for line in lines: ifstr(line).startswith('tcp'): items = str(line).split() ifnot items[3].startswith('127.0.0.1') : if items[3].startswith('0.0.0.0') or items[3].startswith(':::') or items[3].split(':')[0] != items[4].split(':')[0]: print('%s\t\t%s\t\t%s' %(items[3], items[4], items[6])) tmp.add(int(items[3].split(':')[-1]))
listeningPort=list(tmp) listeningPort.sort()
for p in listeningPort: print(p)
######### inUsePortInfo = {} retCode,output = commands.getstatusoutput('netstat -ntp -4 |grep ESTABLISHED') lines = output.split('\n') for line in lines: ifstr(line).startswith('tcp'): items = str(line).split() if items[3].split(':')[0] != items[4].split(':')[0]: port = items[3].split(':')[1] + ' ' + items[6] tmp = inUsePortInfo.get(port, list()) tmp.append(items[4]) tmp.sort() inUsePortInfo[port] = tmp
for i insorted(inUsePortInfo): print('%s \t %s' %(i, inUsePortInfo[i]))
# Define IP addresses array ip_list=( "192.168.1.10" "10.0.0.15" "172.16.0.20" )
# Port to check PORT=1019
# Function to check port connectivity check_port() { local ip=$1 result=$(timeout 3 telnet "$ip"$PORT 2>&1) ifecho"$result" | grep -q "Connected to"; then return 0 else return 1 fi }
echo"Checking port $PORT connectivity..." echo"Unreachable IPs:"
# Check each IP for ip in"${ip_list[@]}"; do if ! check_port "$ip"; then echo"$ip" fi done
if [ "$FILE_COUNT" -le "$MIN_FILE_COUNT" ]; then echo"Error: File count ($FILE_COUNT) is not greater than minimum required ($MIN_FILE_COUNT)" exit 1 fi
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git "${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k" # Open ~/.zshrc, find the line that sets ZSH_THEME, and change its value to "powerlevel10k/powerlevel10k"