天天看點

通過腳本判斷遠端Web伺服器狀态碼是否正常

通過腳本判斷遠端Web伺服器狀态碼是否正常

說明:

(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服務

<a href="https://s2.51cto.com/wyfs02/M01/9B/B8/wKiom1lmMUOB2nAXAAANTG3xlxk248.png" target="_blank"></a>

在用戶端執行腳本:

<a href="https://s3.51cto.com/wyfs02/M00/9B/B8/wKiom1lmMYqRx47cAAAJRXT2nq0061.png" target="_blank"></a>

現在關閉nginx服務:

<a href="https://s3.51cto.com/wyfs02/M02/9B/B8/wKiom1lmMfaDpi6bAAAWBZvSb34855.png" target="_blank"></a>

<a href="https://s5.51cto.com/wyfs02/M01/9B/B7/wKioL1lmMlewhffLAAAJ74AnXAQ995.png" target="_blank"></a>

方法2:利用傳參

if [ $# -ne 1 ];then

        echo "Usage:$0 IP port."

httpcode=`curl -I -s $1|head -1|cut -d " " -f2`

<a href="https://s2.51cto.com/wyfs02/M02/9B/B8/wKioL1lmOMSg6Iz6AAALjClRT6g702.png" target="_blank"></a>

<a href="https://s4.51cto.com/wyfs02/M01/9B/B9/wKiom1lmOSTxnkybAAANdoZY5QM869.png" target="_blank"></a>

方法3:利用read,界面比較友好

read -p "please input IP:" a

if [ -z $a ];then

        echo "Usage:$0 please input ip."

httpcode=`curl -I -s $a|head -1|cut -d " " -f2`

<a href="https://s3.51cto.com/wyfs02/M02/9B/B8/wKioL1lmO0WxJrA-AAAPqRodhRM887.png" target="_blank"></a>

方法4:利用函數

[ -f /etc/init.d/functions ] &amp;&amp; . /etc/init.d/functions || exit 1

httpcode=`curl -I -s $a |head -1|cut -d " " -f2`

        action "nginx is running." /bin/true

        action "nginx is not running." /bin/false

<a href="https://s1.51cto.com/wyfs02/M00/9B/B9/wKiom1lmPuqyiQgeAAAQodDkXI8010.png" target="_blank"></a>

     本文轉自品鑒初心51CTO部落格,原文連結:http://blog.51cto.com/wutengfei/1946907,如需轉載請自行聯系原作者

繼續閱讀