钉钉机器人告警https证书过期
#!/bin/bash
#set -x
# 检查域名的 SSL 证书过期时间
check_ssl_expiry() {
domain=$1
end_date=$(echo | openssl s_client -servername $domain -connect $domain:443 2>/dev/null | openssl x509 -noout -enddate | cut -d= -f 2)
if [ -z "$end_date" ]; then
echo "无法获取证书信息。"
else
expiry_date=$(date -d "$end_date" '+%Y-%m-%d %H:%M:%S')
current_date=$(date '+%Y-%m-%d %H:%M:%S')
echo "域名: $domain"
echo "证书过期时间: $expiry_date"
if [[ $(date -d "$expiry_date" '+%s') -lt $(date -d "$current_date" '+%s') ]]; then
echo "证书已过期!"
else
days_until_expiry=$(( ($(date -d "$expiry_date" '+%s') - $(date -d "$current_date" '+%s')) / 86400 ))
echo "证书还未过期,剩余天数: $days_until_expiry"
if [ $days_until_expiry -lt 30 ]; then #在这填写ssl时间
echo "小于30天"
send_dingtalk_alert $domain $expiry_date
fi
fi
fi
}
# 发送钉钉告警
send_dingtalk_alert() {
domain=$1
expiry_date=$2
access_token="dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf725" #填写钉钉告警机器人的token
curl "https://oapi.dingtalk.com/robot/send?access_token=$access_token" \
-H 'Content-Type: application/json' \
-d '{"msgtype": "text", "text": {"content": "warning:域名 '$domain' 将在30天内过期. 域名 '$domain' 过期时间是 '$expiry_date' "}}'
}
# 检查多个域名的 SSL 证书过期时间
domains=(xxxx.cn yyyy.cn zzzz.cn aaaa.cn) #填写域名
for domain in "${domains[@]}"; do
check_ssl_expiry "$domain"
done