Contents
  1. 1. 读取文件为参数
  2. 2. if-else
  3. 3. for loop
  4. 4. ftp
  5. 5. 传FTP文件脚本
  6. 6. sftp nickname
  7. 7. loop file in dir
  8. 8. tar
  9. 9. 常用日期
  10. 10. 目录下最新日期文件
  11. 11. lsof
  12. 12. 端口使用分析
  13. 13. 端口测试
  14. 14. 同步文件
  15. 15. 异常处理
    1. 15.1. jps – process information unavailable
    2. 15.2. fork: retry: Resource temporarily unavailable
  16. 16. Bash路径通配符
  17. 17. 文件切割
  18. 18. 编码转换
  19. 19. bash shortcut
  20. 20. vi shortcut
  21. 21. telnet批量测试

读取文件为参数

1
2
ans_lines=`sed -n 1p  ${verf_file}`
ans_md5=`sed -n 2p ${verf_file}`

if-else

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# value equal
if [ ${ans_lines} -eq ${lines} ]
then
echo "lines is ok"
else
echo "lines is not ok"
fi

# file exist
hdfs dfs -test -d ${HDFS_path}/wap
if [ $? == 0 ]; then
hdfs dfs -rmr ${HDFS_path}/wap
fi

for loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for i in {1..10}; do echo $i; done
echo "**************"
END=10
for i in $(seq 1 $END); do echo $i; done
echo "**************"
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

ftp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
sftp_get_wap()
{
if [ -d ${sftpLocal_wap} ]; then
rm -rf ${sftpLocal_wap}
fi
mkdir -p ${sftpLocal_wap}
expect <<- EOF
set timeout 120
spawn sftp $sftp_user@$sftp_IP
expect {
"(yes/no)?" {send "yes\r"; expect_continue }
"*assword:" {send "$sftp_password\n"; exp_continue}
"*Permission*" {exit -1}
"sftp>" {send "pwd\n"}
}
expect "sftp>"
send "cd $sftpRemote_wap\n"
expect "sftp>"
send "lcd $sftpLocal_wap\n"
expect "sftp>"
set timeout -1
send "mget *$DT_time*log\n"
expect "sftp>"
set timeout -1
send "mget *$DT_time*verf.txt\n"
expect "sftp>"
send "bye\n"
EOF
}

传FTP文件脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
sftp_IP=10.xx
port=3030
sftp_user=biuser
sftp_password=xx
sftpRemote=/hcycdnsrc/biapp/
sftpLocal=/data/tmp
putfiles=ndmc_ispace_disk_attr_brwspace.csv

expect <<- EOF
set timeout 5
spawn sftp -P $port $sftp_user@$sftp_IP
expect {
"(yes/no*" {send "yes\r"; exp_continue }
"*assword:" {send "$sftp_password\n"}
}
expect "sftp>"
send "cd $sftpRemote\n"
expect "sftp>"
send "lcd $sftpLocal\n"
expect "sftp>"
set timeout -1
send "mput $putfiles\n"
expect "sftp>"
send "ls -l \n"
expect "sftp>"
send "bye\n"

sftp nickname

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash

#transfer.sh
# Function to encrypt string
encrypt() {
echo "$1" | openssl enc -aes-256-cbc -a -salt -pass pass:"${ENCRYPTION_KEY:-default_key}"
}

# Function to decrypt string
decrypt() {
echo "$1" | openssl enc -aes-256-cbc -a -d -salt -pass pass:"${ENCRYPTION_KEY:-default_key}"
}

CONFIG_FILE="config.yml"
ENCRYPTION_KEY="your_encryption_key_here" # Change this to your secure key

# Function to add new SFTP configuration
add_config() {
echo "Enter nickname for the SFTP configuration:"
read -r nickname
echo "Enter host:"
read -r host
echo "Enter port:"
read -r port
echo "Enter username:"
read -r username
echo "Enter password:"
read -rs password
echo

# Encrypt sensitive data
encrypted_pass=$(encrypt "$password")

# Add to config file
cat >> "$CONFIG_FILE" << EOL
$nickname:
host: $host
port: $port
user: $username
password: $encrypted_pass
EOL
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

# Parse YAML and get configuration
host=$(grep -A4 "^$nickname:" "$CONFIG_FILE" | grep "host:" | cut -d: -f2 | tr -d ' ')
port=$(grep -A4 "^$nickname:" "$CONFIG_FILE" | grep "port:" | cut -d: -f2 | tr -d ' ')
user=$(grep -A4 "^$nickname:" "$CONFIG_FILE" | grep "user:" | cut -d: -f2 | tr -d ' ')
encrypted_pass=$(grep -A4 "^$nickname:" "$CONFIG_FILE" | grep "password:" | cut -d: -f2 | tr -d ' ')

if [ -z "$host" ]; then
echo "Nickname not found!"
exit 1
fi

password=$(decrypt "$encrypted_pass")
}

# 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
;;
"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|ls|get|put} [arguments]"
echo "Commands:"
echo " add Add new SFTP configuration"
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

loop file in dir

1
2
3
4
5
6
7
fileList=`ls *$DT_time*.zip`
fileArr=($fileList)
for fileName in ${fileArr[@]}
do
unzip $unzip_name
done

tar

打包、排除指定文件、删除源文件

1
tar -zcf wap.tar.gz wap --exclude *.zip --remove-files

常用日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ymd=${date}
year=${ymd:0:4}
month=${ymd:4:2}
day=${ymd:6:2}

nowdate=`date +%Y%m01` #本月第一天
startdate=`date -d"$nowdate last month" +%Y%m%d` #上个月第一天
enddate=`date -d"$nowdate last day" +%Y%m%d` #上个月最后一天

daytime=20220315
year=${daytime:0:4}
month=${daytime:4:2}
nowdate=${year}${month}01
nextmonth=`date -d"$nowdate +1 month" +%Y%m%d`
enddate=`date -d"$nextmonth last day" +%Y%m%d` #本月最后一天

目录下最新日期文件

find /opt/folder -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | cut -d: -f2- | head

lsof

端口查进程
lsof -i:<port>

进程查端口
lsof -Pp 123674 |grep IPv |grep LISTEN

进程的线程信息
ps -mp 11139 -o THREAD

本机所有进程监听端口
lsof -nP -iTCP -sTCP:LISTEN

端口使用分析

netstat -an //查看所有
netstat -plntu //查询当前处于监听状态的端口
netstat -plnt -4 //查询当前处于监听状态的tcp4端口

netstat -ntlp -4 | head |awk ‘{print}’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 这个方法得不到shell命令的输出

import commands,os
# os.system('netstat -ntlp -4')
retCode,output = commands.getstatusoutput('netstat -ntlp')

tmp = set()
lines = output.split('\n')
for line in lines:
if str(line).startswith('tcp'):
items = str(line).split()
if not 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()
listeningPort

# 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:
if str(line).startswith('tcp'):
items = str(line).split()
if items[3].split(':')[0] != items[4].split(':')[0]:
port = items[3].split(':')[1] + ' ' + items[6]
# print(line)
tmp = inUsePortInfo.get(port, list())
tmp.append(items[4])
tmp.sort()
inUsePortInfo[port] = tmp


# for k,v in inUsePortInfo.items():
# print('%s \t %s' %(k, v))

for i in sorted(inUsePortInfo):
print('%s \t %s' %(i, inUsePortInfo[i]))


listeningPort.sort()
listeningPort


Expected Output:
port, foreign address list

Filter
local server 127.0.0.1
local 和 remoter 都是本机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# iftop/iptraf - IP连接分析

nohup sudo iftop -n -N -t -s 10 -L 1000 -o destination > /data/tmp/iftop20231211 &

nohup sudo iftop -n -N -t -s 86400 -L 5000 -o destination > /data/tmp/iftop20231211 &
cat /data/tmp/iftop20231211 |grep '<=' |awk '{print $1}' |grep -v :

sudo iptraf -i eth0 -L /var/log/traffic_log -B



# crontab监控重启

写脚本时注意环境变量问题,建议程序使用全路径执行

```shell
#!/bin/bash

# */5 * * * * /bin/sh /data/dolphinscheduler-1.3.6/ds-watcher.sh

restart_worker()
{
#pid=`pgrep -f org.apache.dolphinscheduler.server.worker.WorkerServer`
/usr/bin/pkill -f org.apache.dolphinscheduler.server.worker.WorkerServer
rm -f /data/dolphinscheduler-1.3.6/pid/dolphinscheduler-worker-server.pid
sleep 3s
su dolphinscheduler -c "sh /data/dolphinscheduler-1.3.6/bin/dolphinscheduler-daemon.sh start worker-server"
}

# worker
ret=`/data/java/bin/java -cp /data/dolphinscheduler-1.3.6/ds-watcher.jar ds.ZkCheckNode master:2181,slave1:2181,slave2:2181 /dolphinscheduler/nodes/worker/default/`

if [ $ret = 'false' ] ;then
restart_worker
fi

端口测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash

# 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)
if echo "$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

同步文件

集群服务器 增减同步,注意目的地址写父目录
rsync -av –delete /data/soft/spark/jars LGJF-ZYC6-SJZT-SVR04:/data/soft/spark/

异常处理

jps – process information unavailable

问题:无法显示进程名称

解决:删除目录

1
rm -rf /tmp/hsperfdata_*

fork: retry: Resource temporarily unavailable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/etc/security/limits.d/90-nproc.conf

* soft nproc 65535
hadoop soft nproc 257529
root soft nproc unlimited

修改后重启进程.

在控制台执行(修改这个不需要重启)

   ulimit -u 655350

检查下是否生效,在控制台切到该用户下

   ulimit -u

其他信息查看命令:

ps h -Led -o user | sort | uniq -c | sort -n

ps -o nlwp,pid,lwp,args -u hadoop | sort -n

第一列NLWP为线程数

Bash路径通配符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
?:匹配任意单个字符  
*:匹配任意个任意字符
[]:中括号通配。包括下面几种模式:
[abc]:匹配中括号中任意单个字符,即匹配a或b或c均可
[^abc]和`[!abc]`:匹配非中括号中的任意单个字符
[a-z] [^a-z]:匹配abc...z,但和Locale环境的排序规则有关。
Locale C环境下,[a-d]表示abcd,字典排序规则的[a-d]表示aBbCcDd
字符类:
[:alpha:]、[:alnum:]、[:ascii:]、[:blank:]、[:cntrl:]、
[:digit:]、[:graph:]、[:lower:]、[:print:]、[:punct:]、
[:space:]、[:upper:]、[:word:]、[:xdigit:]

{abc,def} 枚举,支持连续字符,支持嵌套;
- echo {j{p,pe}g,png}
- echo {0{2..9},{1..2}{0..9},3{0..1}}

每月6日至次月5日
statis_date=2024{06{0[^1-5],1*,2*,3*},070[1-5]}

文件切割

100w每个文件,3位数字序号,前缀cloudid_part
split -l 1000000 -d -a 3 cloudid533379830 cloudid_part

编码转换

iconv -f 'gbk' -t 'utf-8' 199801.txt > 199801_utf8.txt

iconv -f 'utf-8' -t 'gbk' 199801.txt > 199801_utf8.txt

bash shortcut

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
CTRL+A              # 移动到行首,同 <Home>
CTRL+E # 移动到行末,同 <End>
CTRL+B # 向后移动,同 <Left>
CTRL+F # 向前移动,同 <Right>

CTRL+D # 删除光标前的字符,同 <Delete> ,或者没有内容时,退出会话
CTRL+G # 退出当前编辑(比如正在 CTRL+R 搜索历史时)
CTRL+H # 删除光标左边的字符,同 <Backspace>
CTRL+K # 删除光标位置到行末的内容
CTRL+L # 清屏并重新显示
CTRL+N # 移动到命令历史的下一行,同 <Down>
CTRL+O # 类似回车,但是会显示下一行历史
CTRL+P # 移动到命令历史的上一行,同 <Up>
CTRL+R # 历史命令反向搜索,使用 CTRL+G 退出搜索
CTRL+S # 历史命令正向搜索,使用 CTRL+G 退出搜索
CTRL+T # 交换前后两个字符
CTRL+U # 删除字符到行首
CTRL+V # 输入字符字面量,先按 CTRL+V 再按任意键
CTRL+W # 删除光标左边的一个单词
CTRL+X # 列出可能的补全
CTRL+Y # 粘贴前面 CTRL+u/k/w 删除过的内容
CTRL+Z # 暂停前台进程返回 bash,需要时可用 fg 将其切换回前台
CTRL+_ # 撤销(undo),有的终端将 CTRL+_ 映射为 CTRL+/ 或 CTRL+7

ALT+b # 向后(左边)移动一个单词
ALT+f # 向前(右边)移动一个单词

ALT+d # 删除光标后(右边)一个单词
ALT+t # 交换字符
ALT+BACKSPACE # 删除光标前面一个单词,类似 CTRL+W,但不影响剪贴板

CTRL+X CTRL+X # 连续按两次 CTRL+X,光标在当前位置和行首来回跳转
CTRL+X CTRL+E # 用你指定的编辑器,编辑当前命令

vi shortcut

跳转 备注
b 上一单词 w 下一单词 e 词尾
B 上一单词 W 下一单词 小写的可以标点、空格分隔,大写的必须空格分隔
( 上一句首 ) 下一句首 句号后面的第一个字符为句首
0 或 | 行首 $ 行末 ^ 行首非零字符
- 上一行首 + 下一行首
{ 段首 } 段末 o的操作算段
H 屏幕顶行 L 屏幕底行
? 向前搜索 / 向后搜索
N 查找上一处 n 查找下一处
编辑
~ 大小写转换
o 分段(后) O 分段(前)
p 粘贴(后) P 粘贴(前)
i 插入模式 I 插入到行首
d 删除 D 删除到行末 dd 删除行
x 删除 X 删除前面字符,退格
s 删除并插入 S 删除行并插入
c C 修改至行末 cc 删除行并编辑
r 替换 R 替换模式
< 反缩进 > 缩进 <<, >>
y 复制 Y 复制行
u 撤销
J 与下一行合并
1
2
3
4
5
ZZ 保存退出 
ZQ 不保存退出



telnet批量测试

1
2
3
4
5
6
7
8
for i in {14..23}; 
do
a=`(sleep 1;echo 'quit') |telnet 10.27.48.${i} 22 |grep OpenS |wc -l`;
if [ ${a} == 0 ];
then echo "10.27.48.${i} failed";
else echo "10.27.48.${i} ok";
fi;
done