1.以":"作为分隔符,将IP及port信息贴入ip_port_info文件中,第一列为IP,第二列为端口
1
2
3
4
5
6
7
|
192.168.233.155:2337
192.168.233.155:2338
192.168.233.155:2339
192.168.233.136:3015
192.168.233.155:3022
192.168.233.136:3299
192.168.233.136:4000
|
2.脚本内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/bin/bash
>temp_file
>check_result
echo
"ip port result"
>> check_result
cat
ip_port_info |
while
read
line
do
ip=$(
echo
"${line}"
|
awk
-F
":"
'{print $1}'
)
port=$(
echo
"${line}"
|
awk
-F
":"
'{print $2}'
)
ssh
-o ConnectTimeout=10 -o BatchMode=
yes
-o StrictHostKeyChecking=no -p ${port}
test
@${ip} >> temp_file 2>&1
value=$(
cat
temp_file |
tail
-n 1 |
grep
-E
'Connection timed out during|gssapi-with-mic'
|
wc
-l)
echo
${value}
if
[ ${value} -
eq
1 ]
then
echo
"${ip} ${port} true"
>> check_result
else
echo
"${ip} ${port} false"
>> check_result
fi
echo
""
>temp_file
done
|
3.使用方法
nohup sh check_port.sh &即可
4.输出结果
1
2
3
4
5
6
7
8
|
ip port result
192.168.233.155 2337 true
192.168.233.155 2338 true
192.168.233.155 2339 true
192.168.233.136 3015 false
192.168.233.155 3022 false
192.168.233.136 3299 true
192.168.233.136 4000 true
|
本文转自 xoyabc 51CTO博客,原文链接:http://blog.51cto.com/xoyabc/1899021,如需转载请自行联系原作者