shell之检查端口服务是否正常

       有很多时候需要检查网络服务是否正常,以下是几种常用的方法。

       1.telnet

#/bin/sh
. /etc/init.d/functions
if [ $# -ne 1 ]
then
     echo "Usage: sh $1 ip"
     exit
fi
num=`echo -n "\n"|telnet $1 80|grep Connected|wc -l`
if [ $num -eq 1 ]
then
      action "network is open." /bin/true
else
       action "network is closed." /bin/false
fi

         2.curl

#!/bin/sh
[ $# -ne 1 ] && echo "Usage: sh $0 IP"
[ -f /etc/init.d/functions ] && . /etc/init.d/functions||exit 1
ReturnCode=`curl -I -s $1|head -1|cut -d" " -f2`
[ "$Code" == "200" ] && action "$1 is open." /bin/true || action "$1 is closed." /bin/false

       3.wget
#!/bin/sh
[ $# -ne 1 ] && echo "Usage: sh $0 IP."
[ -f /etc/init.d/functions ] && . /etc/init.d/functions || exit 1
wget -T 10 -q --spider http//$1 &> dev/null
[ $? -eq 0 ] && action "$1 is open." /bin/true || action "$1 is closed." /bin/false



         

       




版权声明:本文为canyudeguang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。