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
相关推荐
yinmaisoft4 小时前
JNPF 钉钉双向同步攻略:组织 / 用户一键打通,触发事件自动联动
前端·低代码·钉钉
Xyz996_7 小时前
Zabbix监控告警邮箱提醒,钉钉提醒
运维·钉钉·zabbix
跟‘码’死磕1 天前
springboot集成钉钉群内发送消息
java·spring boot·钉钉
岚天start1 天前
[K8S监控]-K8S容器pod异常状态监控脚本并推送钉钉告警
容器·kubernetes·钉钉·shell·告警
风123456789~3 天前
【健康管理】第13章 医学伦理与职业道德
笔记·证书·健康管理
墨着染霜华3 天前
钉钉群机器人 Markdown :颜色渲染 + 链接跳转 + 图片嵌入的正确实现
钉钉
帅哥哥珍藏版3 天前
利用机器人Webhook向群推送告警通知
钉钉·飞书·企业微信
Arwen3034 天前
等保2.0三级认证内网IP SM2 SSL证书
网络协议·tcp/ip·ssl
Arwen3034 天前
内网IP证书:安全加密全指南
网络·网络协议·tcp/ip·ssl
Arwen3034 天前
国密SSL内网IP部署全攻略
网络·网络协议·tcp/ip·ssl