批量网络策略验证
# 脚本
#!/bin/bash
telnets()
{
#先ping判断,避免telnet耗时过长
P=`ping -c4 -w3 $1 | grep 'packet loss' | grep -v grep | awk -F',' '{print $3}' | awk -F'%' '{print $1}'`
if [ $P -eq 100 ];
then
echo "ping不通"
echo `date +"%Y.%m.%d %T"` " ping "${1}" 失败" >>telnetresults.txt
else
echo "telnet开始"
#`echo "\035"`和`echo "\015"`分别模拟键盘输入ctrl+] 和回车,quit退出
telnet $1 $2 <<EOF 2>temp.txt 1>&2
`echo "\035"`
`echo "\015"`
quit
EOF
echo "telnet结束"
echo
RESULT=`cat temp.txt 2>/dev/null | grep "Connection closed" | wc -l`
if [ $RESULT -eq 1 ];
then
echo `date +"%Y.%m.%d %T"`" telnet "${1}":"${2}" 成功" >>telnetresults.txt
else
echo `date +"%Y.%m.%d %T"`" telnet "${1}":"${2}" 失败" >>telnetresults.txt
fi
fi
}
OLD_IFS="$IFS"
IFS=":"
#清空文件使用
echo "" >telnetresults.txt
echo "" >temp.txt
while read LINE
do
echo $LINE
array=$LINE
ip=${array[0]}
port=${array[1]}
telnets $ip $port
done <ip.txt
IFS="$OLD_IFS"
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
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
# 使用
- 新建上述sh文件
- 同级目录新建ip.txt,每行为一个策略,[IP:PORT]
ip.txt 示例:
127.0.0.1:8080
127.0.0.1:3306
1
2
2
编辑 (opens new window)
上次更新: 2024/05/16, 09:20:45