Linux telnet、nc、ping监测状态

    在工作中会遇到网络出现闪断丢包的情况,最终影响业务工常使用。可以业务服务器上发起监测。

    1、通过telnet

       echo  -e  "\n" | telnet localhost 22 | grep Connected

       返回结果为$? 为 0 ,证明端口可正常访问

    2、通过nc

     nc -nz -w 1 192.168.1.3 22    #centos7 以前版本,  在centos7中操作  nc -n  -w 1 192.168.1.3 22 </dev/null  取消了-z,通过重定向可实现.  如果测试UDP 时,加上-u

     返回结果为$? 为 0 ,证明端口可正常访问

    3、通过ping

      ping -c 1 -w 1  192.168.1.3

     返回结果为$? 为 0 ,证明端口可正常访问

eg:

#/bin/bash
#check server oracle ip and port

function check_ping(){
    while [ 1 -ne 2 ]
    do
        ping -c 1 -w 1 $1 >> /dev/null
        if [ $? = 0 ];then
            date >> ping_s_y_$1.log
            echo "normal" >> ping_s_y_$1.log
        else
            date >> ping_s_n_$1.log
            echo "abnormal" >> ping_s_n_$1.log
        fi
        sleep 1
   done
}
IP1='10.204.198.13'
IP2='10.204.198.14'
IP3='10.204.14.46'
for IP in $IP1 $IP2 $IP3
do
{
    check_ping $IP
} &
done
wait

猜你喜欢

转载自www.cnblogs.com/mrice/p/10002673.html
今日推荐