shell脚本-批量检查网站是否异常

shell脚本-批量检查网站是否异常

  • 200 - 请求成功
  • 301 - 资源(网页等)被永久转移到其它URL
  • 404 - 请求的资源(网页等)不存在
  • 500 - 内部服务器错误

通过获取状态码检查网站是否正常 但是这种方式不太好直接取出状态码

[root@localhost ~]# curl www.baidu.com -I
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 11 Jul 2023 13:46:57 GMT
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

1.通过以下命令只获取这个状态码

[root@localhost ~]# curl -o /dev/null  -s -w "%{http_code}" http://www.baidu.com
200

2.创建一个存储url的文件 最后一个错的url用来测试

[root@localhost scripts]# vim url.info 
www.baidu.com
www.cnblogs.com
www.bilibili.com
www.asdjfkaj.com

3.编辑脚本文件

[root@localhost scripts]# vim 8.sh
#!/bin/bash
URL_INFO=url.info
URL_LIST=`awk '/^[^#]/{print $1}' $URL_INFO`
for URL in $URL_LIST;do
        FAIL_COUNT=0
        for ((i=0;i<3;i++));do
                HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" $URL`
                # if [[ $HTTP_CODE == 200 || 301 ]]
                if [ $HTTP_CODE -eq 200 -o 301 ];then
                        echo "ok"
                        break
                else
                        let FAIL_COUNT++
                fi
        done
        if [ $FAIL_COUNT -eq 3 ];then
                echo "Warning: $URL REQUEST FAILURE!"
        fi
done

4.测试

[root@localhost ~]# bash /opt/scripts/8.sh 
ok
ok
ok
Warning: www.asdjfkaj.com REQUEST FAILURE!
posted @ 2023-07-12 01:36  FouroFour  阅读(314)  评论(0编辑  收藏  举报