50 lines
1.2 KiB
Markdown
50 lines
1.2 KiB
Markdown
---
|
||
title: HTTTP探测脚本
|
||
categories:
|
||
- Shell
|
||
tags:
|
||
- 脚本
|
||
date: 2023-05-23 23:45:25
|
||
---
|
||
|
||
# HTTTP探测脚本
|
||
|
||
```
|
||
#!/bin/bash
|
||
|
||
# 设置探测目标和日志文件路径
|
||
target="网址"
|
||
log_file="/var/log/ccps_probe.log"
|
||
|
||
# 设置企业微信机器人的webhook地址和异常通知消息
|
||
webhook_url="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的企微机器人KEY"
|
||
error_message="xxx网站 探测失败!!! HTTP status code is not 200 for 3 consecutive times."
|
||
|
||
# 循环探测
|
||
while true
|
||
do
|
||
# 发送https请求并记录响应状态码
|
||
status_code=$(curl -o /dev/null -s -w "%{http_code}\n" $target)
|
||
|
||
# 记录探测日志
|
||
echo "$(date +"%Y-%m-%d %H:%M:%S") - HTTP status code: $status_code" >> $log_file
|
||
|
||
# 如果状态码不是200,计数器加1
|
||
if [ $status_code -ne 200 ]; then
|
||
count=$((count+1))
|
||
else
|
||
count=0
|
||
fi
|
||
|
||
# 如果计数器达到3,发送异常通知
|
||
if [ $count -eq 3 ]; then
|
||
curl -H "Content-Type: application/json" -d "{\"msgtype\": \"text\", \"text\": {\"content\": \"$error_message\"}}" $webhook_url
|
||
count=0
|
||
fi
|
||
|
||
# 等待10秒后再次探测
|
||
sleep 10
|
||
done
|
||
```
|
||
|