SSL证书到期自动巡检脚本-推送钉钉告警

1. 编写SSL证书巡检脚本

cat /hao/batch_check_ssl_expire.sh

bash 复制代码
#!/bin/bash

# 域名列表文件绝对路径
domains_file="/hao/domains.txt"

#钉钉webhook
webhook_url="https://oapi.dingtalk.com/robot/send?access_token=9999999999999999999999999999999999999999999999999999"


#钉钉推送告警信息
dingding_push(){
      curl      -H "Content-Type: application/json" \
                -d '{
                    "msgtype": "text",
                    "text": {
                        "content": "【重要告警】: 【'"$domain"'】 的SSL证书将在【'"$remaining_days"'】 天内过期。"
                    }
                }' $webhook_url
}

# 检查每个域名的SSL证书
while read -r domain; do
    # 获取当前日期的Unix时间戳(秒)
    current_timestamp=$(date +%s)

    # 使用openssl s_client命令获取证书信息,并通过grep和awk提取到期日期
    expires=$(echo | openssl s_client -connect "$domain":443 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | cut -d'=' -f2)

    if [ -z "$expires" ]; then
        echo "无法获取SSL证书信息,请检查域名 ($domain) 或网络连接。"
        continue
    fi

    # 将到期日期转换为Unix时间戳
    expires_timestamp=$(date -d "$expires" +%s)

    # 计算剩余天数
    remaining_days=$(( (expires_timestamp - current_timestamp) / 86400 ))

    # 将剩余天数信息推送到钉钉告警
    if [ $remaining_days -le 10 ]; then
        dingding_push
    elif [ $remaining_days -le 15 ]; then
        dingding_push
    else
        echo "$domain 的SSL证书有效,距离过期还有$remaining_days 天。"
    fi
done < "$domains_file"

2. 创建域名列表文件

cat /hao/domains.txt

bash 复制代码
test1.google.com
test2.google.com
test3.google.com
test4.google.com
test5.google.com

3. 配置计划任务

bash 复制代码
[root@localhost ~]# crontab   -l
30  09   *   *   *    /bin/bash  /hao/batch_check_ssl_expire.sh   >  /dev/null  2>&1
相关推荐
Lsetea4 天前
Nginx报No required SSL certificate was sent:mTLS客户端证书排查
运维·nginx·https·ssl·openssl
其实防守也摸鱼6 天前
DeepSeek Harness 开源贡献手记:从入门到合入主线
服务器·数据库·windows·https·ssl
其实防守也摸鱼6 天前
堆叠注入(Stacked Injection)详解
服务器·数据库·windows·https·ssl
JamesShi1687 天前
华为云证书续费操作全流程,针对购买华为云企业门户建站的用户!
ssl
2601_967019517 天前
SSL 证书不止加密,还影响网站信任与 SEO
ssl
Xxtaoaooo7 天前
Prometheus 告警怎么推到钉钉?从单群通知到跨网络 Alertmanager 实战
钉钉·webhook·cpolar
2601_967019518 天前
证书兼容性排查:SSL 部署后浏览器不安全告警分析
ssl
2601_967019519 天前
没有 SSL 证书的网站,会有哪些安全与流量损失?
ssl
一个心烑10 天前
配置项目git的 SSL 证书
git·网络协议·ssl
IT摆渡者12 天前
1panel 配置申请手动解析网站SSL证书
服务器·网络协议·ssl