SSL证书到期自动巡检脚本-推送钉钉告警

1. 编写SSL证书巡检脚本

  cat   /hao/batch_check_ssl_expire.sh

#!/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 

test1.google.com
test2.google.com
test3.google.com
test4.google.com
test5.google.com

3. 配置计划任务

[root@localhost ~]# crontab   -l
30  09   *   *   *    /bin/bash  /hao/batch_check_ssl_expire.sh   >  /dev/null  2>&1

相关推荐

  1. SSL证书到期自动脚本-告警

    2024-07-10 09:08:05       9 阅读
  2. Linux服务监控自动脚本--告警

    2024-07-10 09:08:05       18 阅读
  3. Python 自动打卡脚本

    2024-07-10 09:08:05       67 阅读
  4. Stream模式程序环境部署

    2024-07-10 09:08:05       14 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 09:08:05       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 09:08:05       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 09:08:05       4 阅读
  4. Python语言-面向对象

    2024-07-10 09:08:05       4 阅读

热门阅读

  1. 如何才能在Linux下编写驱动程序

    2024-07-10 09:08:05       6 阅读
  2. Tomcat打破双亲委派模型的方式

    2024-07-10 09:08:05       12 阅读
  3. C++惯用法: 通过std::decltype来SFINAE掉表达式

    2024-07-10 09:08:05       7 阅读
  4. HTTP 范围Range请求

    2024-07-10 09:08:05       8 阅读
  5. React 开发报错整理

    2024-07-10 09:08:05       14 阅读
  6. 微软 Edge 浏览器全解析

    2024-07-10 09:08:05       7 阅读
  7. 静态搜索iOS动态链接函数的调用位置

    2024-07-10 09:08:05       10 阅读
  8. demon drone 200无人机标定流程

    2024-07-10 09:08:05       9 阅读
  9. Sql 导入到 Excel 工具

    2024-07-10 09:08:05       8 阅读