检查主机的某些tcp端口在一段时间内是否持续存活

900阅读 0评论2020-04-30 zzjlzx
分类:服务器与存储

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
上一篇:VMware-VCSA-all-6.5.0-4602587安装到80%卡住,提示rpm安装失败,root密码未设置
下一篇:ESXi 通过命令行升级