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

本文介绍了三种常用的网络服务状态检查方法:使用telnet确认服务端口是否开放;利用curl请求检查HTTP服务的状态码;借助wget工具尝试访问网页并判断响应结果。这些方法适用于基本的网络故障排查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

       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



         

       



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值