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
相关推荐
梦帮科技4 小时前
Web 漏洞攻防深度:WAF 规则逃逸技术(畸形分块传输、双重编码)与对抗加固
linux·前端·网络安全·ssl·web
2601_952047796 小时前
金蝶云星空物料主数据推送 MES:单一策略同步实战教程
机器人·钉钉
xiaogai_gai10 小时前
销售订单审核驳回钉钉提醒同步方案实战教程
钉钉
zxanz11 天前
该怎么去选择 SSL 证书来使用?有什么方法不
服务器·网络协议·ssl
mingbaiceping1 天前
复旦-华盛顿大学EMBA:2026年免联考申请条件详解
网络协议·https·ssl
QEasycloud2 天前
BOM接口抛转失败钉钉通知策略实战教程:MySQL异常日志到钉钉消息的端到端配置
android·mysql·钉钉
HelloWorld工程师2 天前
不改动任何历史配置,也能让 SSL 证书自动续期
linux·服务器·网络协议·https·ssl
zxanz12 天前
泛域名 SSL 证书怎么申请?需要准备什么?
网络·网络协议·ssl
zxanz13 天前
SSL 证书申请下来快吗?
网络·网络协议·ssl
zxanz13 天前
SSL 证书申请中,服务器验证跟 DNS 解析验证有什么区别?
服务器·网络协议·ssl