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
相关推荐
Edward111111114 小时前
SSL/TSL配置 集群节点间通信加密还有客户端
linux·服务器·ssl
七七powerful6 小时前
AI+运维提效--SSL 证书监控系统v3.0版本开发完成
网络·网络协议·ssl
2601_955781988 小时前
OpenClaw 对接 钉钉机器人 全流程指南
机器人·钉钉·open claw安装
组合缺一8 小时前
OpenClaw vs SolonCode:绑定飞书与钉钉,到底谁更简单?
ai·钉钉·飞书·ai编程·数字员工·openclaw·soloncode
HelloWorld工程师1 天前
SSL证书在哪里可以免费且快速申请?
服务器·网络协议·ssl
带刺的坐椅1 天前
OpenClaw vs SolonCode:绑定飞书与钉钉,到底谁更简单?
钉钉·飞书·openclaw·soloncode
极客老王说Agent2 天前
实在Agent跨平台表格自动化实战:钉钉表格与飞书多维表数据无缝同步
自动化·钉钉·飞书
Dovis(誓平步青云)3 天前
《如何通过prometheus-webhook-dingtalk解决 Alertmanager 原生不支持钉钉 Webhook问题》
人工智能·生成对抗网络·钉钉·运维开发·prometheus
易生一世3 天前
JWT详解
json·证书·jwt·token·ai skills
Hy行者勇哥3 天前
Coze智能体企业端部署实操指南:企业微信与钉钉接入对比及标准化SOP
钉钉·企业微信