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]#
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章