天天看点

shell脚本检查网站状态

检查网站状态通常使用wget或curl工具,下面分别使用这二种工具来做写检查网站的脚本。(学习自老男孩shell编程)

命令行:

1、curl得到返回值200,表示正常

[root@c7 ~]# curl -o /dev/null -s --connect-timeout 5 -w '%{http_code}' www.baidu.com

200[root@c7 ~]

2、wget得到0,表示正常

[root@c7 ~]# wget -T 5 -t 2 --spider www.baidu.com &>/dev/null

[root@c7 ~]# echo $?

脚本:

1、curl

[root@c7 shell]# cat check_web1.sh

#!/bin/bash

#

[ -f /etc/init.d/functions ] && . /etc/init.d/functions

array=(

http://www.china-cmd.org

http://www.cmdmedia.cn

http://www.icehtmc.com

https://mail.cmdmedia.cn

)

curl_ip() {

  CURL=$(curl -o /dev/null -s --connect-timeout 5 -w '%{http_code}' $1|egrep "200|302"|wc -l)

  return $CURL

}

main() {

   for n in ${array[*]}

   do 

      curl_ip $n

      if [ $? -eq 1 ];then

         action "curl $n" /bin/true

      else

         action "curl $n" /bin/false

         sleep 3

             CURL=$(curl_ip $n|egrep "200|302"|wc -l)

             if [ $CURL -eq 1 ];then

                action "Retry curl $n again" /bin/true

             else

                action "Retry curl $n again" /bin/false

             fi 

      fi

   done

main

图示:

<a href="https://s5.51cto.com/wyfs02/M00/8D/BB/wKiom1ioYOWCw9-QAAFDKf159i8710.jpg" target="_blank"></a>

2、wget

[root@c7 shell]# cat check_web2.sh

  wget -T 5 -t 2 --spider $1 &amp;&gt;/dev/null

  return $?

      if [ $? -eq 0 ];then

<a href="https://s5.51cto.com/wyfs02/M00/8D/B9/wKioL1ioYWHxSX3dAAFI8IzODi8221.jpg" target="_blank"></a>

学习自:

老男孩shell

      本文转自cix123  51CTO博客,原文链接:http://blog.51cto.com/zhaodongwei/1899145,如需转载请自行联系原作者