Linux Shell脚本专栏_批量检测网站是否异常脚本_08

1. 脚本制作

#!/bin/bash  
URL_LIST=$@
for URL in $URL_LIST; do
    FAIL_COUNT=0
    for ((i=1;i<=3;i++)); do
        HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $URL)
        if [ $HTTP_CODE -eq 200 ]; then
            echo "$URL OK"
            break
        else
            echo "$URL retry $FAIL_COUNT"
            let FAIL_COUNT++
        fi
    done
    if [ $FAIL_COUNT -eq 3 ]; then
        echo "Warning: $URL Access failure!"
    fi
done

2. 运行脚本

[root@localhost app]# ./8.sh 
www.baidu.com OK
www.ctnrs.com retry 0
www.ctnrs.com retry 1
www.ctnrs.com retry 2
Warning: www.ctnrs.com Access failure!
[root@localhost app]#

3. 脚本进化动态参数传递

#!/bin/bash  
URL_LIST=$@
for URL in $URL_LIST; do
    FAIL_COUNT=0
    for ((i=1;i<=3;i++)); do
        HTTP_CODE=$(curl -o /dev/null --connect-timeout 3 -s -w "%{http_code}" $URL)
        if [ $HTTP_CODE -eq 200 ]; then
            echo "$URL OK"
            break
        else
            echo "$URL retry $FAIL_COUNT"
            let FAIL_COUNT++
        fi
    done
    if [ $FAIL_COUNT -eq 3 ]; then
        echo "Warning: $URL Access failure!"
    fi
done

4. 运行脚本

[root@localhost app]# ./8.sh www.baidu.com www.ctnrs.com
www.baidu.com OK
www.ctnrs.com retry 0
www.ctnrs.com retry 1
www.ctnrs.com retry 2
Warning: www.ctnrs.com Access failure!

5. 脚本分解

[root@localhost app]# curl -I www.baidu.com
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: Mon, 24 Feb 2020 13:54:03 GMT
Etag: "575e1f5c-115"
Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

[root@localhost app]# 
[root@localhost app]# curl -o /de/dev/null -s -w "%{http_code}" http://www
200
[root@localhost app]#
发布了858 篇原创文章 · 获赞 114 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/weixin_40816738/article/details/104487070