批量检测网站是否异常并邮件通知
[root@dy1 ~]# curl -o /de/dev/null -s -w "%{http_code}" www.baidu.com
200[root@dy1 ~]# curl -o /de/dev/null -s -w "%{http_code}" www.baidu.com
200[root@dy1 ~]# curl -o /de/dev/null -s -w "%{http_code}" www.zhanghehe.cn
301[root@dy1 ~]#
访问失败,也又可能和网络等等原因有关。所以我们要进行次数判断,超出一定的次数。那就发送邮件
#!/bin/bash
URL_LIST="www.baidu.com www.ctnrs.com www.der-matech.net.cn www.der-matech.com.cn www.der-matech.cn www.der-matech.top www.der-matech.org"
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!"
echo "网站$URL坏掉,请及时处理" | mail -s "$URL网站高危" 1794748404@qq.com
fi
done