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
相关推荐
zzc9212 小时前
TLSv1.2协议与TCP/UDP协议传输数据内容差异
网络·测试工具·安全·wireshark·ssl·密钥·tlsv1.2
蜡笔小柯南4 天前
解决:nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module
nginx·http·ssl
张飞的猪大数据6 天前
通过Certbot自动申请更新HTTPS网站的SSL证书
网络协议·https·ssl
未名编程6 天前
【已解决】报错:WARNING: pip is configured with locations that require TLS/SSL
网络协议·ssl·pip
不会吉他的肌肉男不是好的挨踢男6 天前
Linux生成自签名 SSL 证书(适用于测试或内部使用)
linux·运维·ssl
一念&10 天前
SSL/TLS,信息安全的守护者
网络·网络协议·ssl
Lazy Dave10 天前
gmssl私钥文件格式
网络安全·ssl·openssl
Linux运维技术栈10 天前
解决 Nginx 反代中 proxy_ssl_name 环境变量失效问题:网页能打开但登录失败
运维·nginx·ssl
冬停10 天前
解决 Qt 网络 SSL 报错:`cannot call unresolved function` 的终极指南
网络·qt·ssl
白白白鲤鱼11 天前
Vue2项目—基于路由守卫实现钉钉小程序动态更新标题
服务器·前端·spring boot·后端·职场和发展·小程序·钉钉