巡检-域名-https证书过期-钉钉通知
ding_https_check.sh
通过脚本巡检即将到期的https域名证书,并发送钉钉群
只需要修改钉钉机器人tocken
巡检内容地址为:/data/https_list,格式为域名+空格备注(liwork.cn 博客网站)
效果示意:
执行命令:
sh https_check.sh
代码内容
#!/bin/bash
# 检测https证书有效
echo '开始检查 HTTPS 证书有效期'
echo '' > /data/https_lists
source /etc/profile
# 定义错误的域名
errorDominStr=""
notes=""
while read line; do
domain=$(echo $line | awk '{print $1}') # 提取域名
note=$(echo $line | awk '{$1="";print $0}') # 提取备注信息,排除第一个字段(域名)
echo "====================================================================================="
echo "当前检测的域名:" $domain
end_time=$(echo | timeout 8 openssl s_client -servername $domain -connect $domain:443 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | awk -F '=' '{print $2}')
([ $? -ne 0 ] || [[ $end_time == '' ]]) && echo '该域名链接不上,跳到下一个域名' && continue
end_times=$(date -d "$end_time" +%s)
current_times=$(date +%s)
let left_time=$end_times-$current_times
days=$(expr $left_time / 86400)
echo "剩余天数: " $days
if [ $days -lt 10 ]; then
echo "HTTPS 证书有效期少于10天,存在风险"
echo $domain >> /data/https_lists
errorDominStr="$errorDominStr \n $domain"
# 使用UTC时间计算到期时间,但实际发出的内容中不显示UTC
notes="$notes \n $domain 证书将于 $(date -u -d +"$left_time seconds" '+%Y-%m-%d %H:%M:%S') 到期,备注信息:$note"
fi
done < /data/https_list
# 在钉钉通知消息中包含备注信息
if [ "$errorDominStr" = "" ]; then
echo "不包含准备过期的域名"
else
curl 'https://oapi.dingtalk.com/robot/send?access_token=***' \
-H 'Content-Type: application/json' \
-d '{"msgtype": "text","text": {"content":"以下域名证书即将过期,请注意及时更新证书:\n'"$notes"' "}}'
fi
评论
其他文章