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
相关推荐
PinTrust SSL证书4 小时前
Sectigo(Comodo)域名型DV通配符SSL
网络·网络协议·http·网络安全·https·软件工程·ssl
就叫飞六吧17 小时前
钉钉组织架构同步到本地(排除管理员+管理员两版本)
钉钉
小小AK17 小时前
钉钉与金蝶云星空无缝集成方案
大数据·人工智能·钉钉
PinTrust SSL证书1 天前
Sectigo(Comodo)企业型OV通配符SSL
网络·网络协议·网络安全·小程序·https·ssl
中科三方2 天前
SSL证书、域名与IP地址:三者关系全面解析与常见误区澄清
网络·tcp/ip·ssl
PinTrust SSL证书3 天前
IP地址访问网站,怎么去除不安全提示?
网络协议·tcp/ip·安全·网络安全·https·ssl
PinTrust SSL证书5 天前
Geotrust企业型OV通配符SSL
网络协议·网络安全·小程序·https·云计算·ssl
墨香幽梦客5 天前
全站HTTPS化实战:SSL证书管理、自动续期与TLS 1.3性能优化详解
性能优化·https·ssl
yleihj9 天前
vCenter计算机SSL证书续期
服务器·网络协议·ssl
dtsola9 天前
小遥搜索v1.8.0版本更新【钉钉文档+知识库支持】
程序员·钉钉·ai搜索·ai创业·独立开发者·个人开发者·一人公司