通过脚本判断远程Web服务器状态码是否正常
说明:
(1)生产环境常见的HTTP状态码列表,请查看我的博文:http://wutengfei.blog.51cto.com/10942117/1934645
(2)实验中远程nginx服务器IP地址:192.168.100.114
本地客户端IP地址:192.168.100.118
-
脚本如下
方法1:if
#!/bin/bash
httpcode=`curl -I -s 192.168.100.114|head -1|cut -d " " -f2`
if [ "$httpcode" == "200" ];then
echo "nginx is running."
else
echo "nginx is not running."
fi
验证结果如下:
开启nginx服务
在客户端执行脚本:
现在关闭nginx服务:
在客户端执行脚本:
方法2:利用传参
#!/bin/bash
if [ $# -ne 1 ];then
echo "Usage:$0 IP port."
fi
httpcode=`curl -I -s $1|head -1|cut -d " " -f2`
if [ "$httpcode" == "200" ];then
echo "nginx is running."
else
echo "nginx is not running."
fi
验证结果如下:
开启nginx服务
在客户端执行脚本:
现在关闭nginx服务:
在客户端执行脚本:
方法3:利用read,界面比较友好
#!/bin/bash
read -p "please input IP:" a
if [ -z $a ];then
echo "Usage:$0 please input ip."
fi
httpcode=`curl -I -s $a|head -1|cut -d " " -f2`
if [ "$httpcode" == "200" ];then
echo "nginx is running."
else
echo "nginx is not running."
fi
验证结果如下:
开启nginx服务
在客户端执行脚本:
现在关闭nginx服务:
在客户端执行脚本:
方法4:利用函数
#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions || exit 1
read -p "please input IP:" a
httpcode=`curl -I -s $a |head -1|cut -d " " -f2`
if [ "$httpcode" == "200" ];then
action "nginx is running." /bin/true
else
action "nginx is not running." /bin/false
fi
验证结果如下:
开启nginx服务
在客户端执行脚本:
现在关闭nginx服务:
在客户端执行脚本:
转载于:https://blog.51cto.com/wutengfei/1946907