钉钉机器人告警https证书过期时间

钉钉机器人告警https证书过期

#!/bin/bash
#set -x
# 检查域名的 SSL 证书过期时间
check_ssl_expiry() {
    domain=$1
    end_date=$(echo | openssl s_client -servername $domain -connect $domain:443 2>/dev/null | openssl x509 -noout -enddate | cut -d= -f 2)

    if [ -z "$end_date" ]; then
        echo "无法获取证书信息。"
    else
        expiry_date=$(date -d "$end_date" '+%Y-%m-%d %H:%M:%S')
        current_date=$(date '+%Y-%m-%d %H:%M:%S')

        echo "域名: $domain"
        echo "证书过期时间: $expiry_date"

        if [[ $(date -d "$expiry_date" '+%s') -lt $(date -d "$current_date" '+%s') ]]; then
            echo "证书已过期!"
        else
            days_until_expiry=$(( ($(date -d "$expiry_date" '+%s') - $(date -d "$current_date" '+%s')) / 86400 ))
            echo "证书还未过期,剩余天数: $days_until_expiry"

            if [ $days_until_expiry -lt 30 ]; then   #在这填写ssl时间
                 echo "小于30天"
                 send_dingtalk_alert $domain $expiry_date
            fi
        fi
    fi
}

# 发送钉钉告警
send_dingtalk_alert() {
    domain=$1
    expiry_date=$2
    access_token="dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf725"   #填写钉钉告警机器人的token

    curl "https://oapi.dingtalk.com/robot/send?access_token=$access_token" \
        -H 'Content-Type: application/json' \
        -d '{"msgtype": "text", "text": {"content": "warning:域名 '$domain' 将在30天内过期. 域名 '$domain' 过期时间是 '$expiry_date' "}}'
}

# 检查多个域名的 SSL 证书过期时间
domains=(xxxx.cn yyyy.cn zzzz.cn aaaa.cn)       #填写域名

for domain in "${domains[@]}"; do
    check_ssl_expiry "$domain"
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值