檢查主機的某些tcp端口在一段時間內是否持續存活

1.以":"做爲分隔符,將IP及port信息貼入ip_port_info文件中,第一列爲IP,第二列爲端口
api

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.腳本內容bash

#!/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.使用方法ssh

nohup sh check_port.sh &便可ide

4.輸出結果ip

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
相關文章
相關標籤/搜索