使用bash脚本检测网站SSL证书是否过期

#!/bin/bash

# 加载环境变量
. /etc/profile
. ~/.bash_profile
. /etc/bashrc

# 脚本所在目录即脚本名称
script_dir=$( cd "$( dirname "$0"  )" && pwd )
script_name=$(basename ${0})
readFile="${script_dir}/domain_ssl.info"

# 检查配置文件是否存在
if [ ! -f "${readFile}" ]; then
    echo "错误:配置文件 ${readFile} 不存在"
    exit 1
fi

grep -v '^#' ${readFile} | while read line; do
    # 跳过空行
    if [[ -z "${line// }" ]]; then
        continue
    fi
    
    # 处理域名和端口
    if [[ "${line}" == *":"* ]]; then
        # 包含端口号
        get_domain=$(echo "${line}" | awk -F ':' '{print $1}')
        get_port=$(echo "${line}" | awk -F ':' '{print $2}')
    else
        # 不包含端口号,使用默认443
        get_domain=${line}
        get_port=443
    fi
    
    # 去除域名和端口中的空格
    get_domain=$(echo "${get_domain}" | tr -d ' ')
    get_port=$(echo "${get_port}" | tr -d ' ')
    
    # 验证端口号是否为数字
    if ! [[ "${get_port}" =~ ^[0-9]+$ ]]; then
        echo "警告:域名 ${get_domain} 的端口 ${get_port} 不是有效数字,使用默认端口443"
        get_port=443
    fi
    
    echo "正在检查 ${get_domain}:${get_port} 的SSL证书..."
    
    # 使用openssl获取域名的证书情况,然后获取其中的到期时间
    END_TIME=$(echo | openssl s_client -servername ${get_domain} -connect ${get_domain}:${get_port} 2>/dev/null | openssl x509 -noout -dates | grep 'After' | awk -F '=' '{print $2}' | awk -F ' +' '{print $1,$2,$4 }')
    
    # 检查是否成功获取到证书信息
    if [[ -z "${END_TIME}" ]]; then
        echo "错误:无法获取 ${get_domain}:${get_port} 的SSL证书信息"
        # 发送错误通知(钉钉、飞书)
        curl -s -o /dev/null 'https://oapi.dingtalk.com/robot/send?access_token=xxxx' -H 'Content-Type: application/json' -d '{"msgtype": "text", "text": {"content": "'"${get_domain}:${get_port} SSL证书检查失败,请手动检查"'"}}'
        curl -s -o /dev/null -X POST -H "Content-Type: application/json" 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' -d '{"msg_type":"text","content":{"text":"'"${get_domain}:${get_port} SSL证书检查失败,请手动检查"'"}}'
        continue
    fi
    
    END_TIME1=$(date +%s -d "$END_TIME") # 将日期转化为时间戳
    NOW_TIME=$(date +%s -d "$(date "+%Y-%m-%d %H:%M:%S")") # 将当前的日期也转化为时间戳
    RST=$(($(($END_TIME1 - $NOW_TIME))/(60*60*24))) # 到期时间减去目前时间再转化为天数
    
    echo "域名:${get_domain}:${get_port} | 证书有效天数剩余:${RST} 天"
    
    # 证书过期提醒
    if [ $RST -lt 10 ]; then
        echo "警告:${get_domain}:${get_port} 证书将在 ${RST} 天后过期!"
        # 钉钉通知
        curl -s -o /dev/null 'https://oapi.dingtalk.com/robot/send?access_token=xxxx' \
            -H 'Content-Type: application/json' \
            -d '{"msgtype": "text", "text": {"content": "'"${get_domain}:${get_port} HTTPS证书有效期少于10天(剩余${RST}天),存在风险,请及时更新证书"'"}}'
        
        # 飞书通知
        curl -s -o /dev/null -X POST \
            -H "Content-Type: application/json" \
            'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' \
            -d '{"msg_type":"text","content":{"text":"https://'"${get_domain}:${get_port}"' SSL证书有效期为'"${RST}"'天,请注意及时更新"}}'
    fi
done

echo "SSL证书检查完成!"
posted @ 2025-09-17 12:31  sherlock-merlin  阅读(5)  评论(0)    收藏  举报