1.以":"作为分隔符,将IP及port信息贴入ip_port_info文件中,第一列为IP,第二列为端口

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.脚本内容

#!/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.输出结果

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