#!/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}" ]; thenecho "错误:配置文件 ${readFile} 不存在"exit 1 figrep -v '^#' ${readFile} | while read line; do# 跳过空行if [[ -z "${line// }" ]]; thencontinuefi# 处理域名和端口if [[ "${line}" == *":"* ]]; then# 包含端口号get_domain=$(echo "${line}" | awk -F ':' '{print $1}')get_port=$(echo "${line}" | awk -F ':' '{print $2}')else# 不包含端口号,使用默认443get_domain=${line}get_port=443fi# 去除域名和端口中的空格get_domain=$(echo "${get_domain}" | tr -d ' ')get_port=$(echo "${get_port}" | tr -d ' ')# 验证端口号是否为数字if ! [[ "${get_port}" =~ ^[0-9]+$ ]]; thenecho "警告:域名 ${get_domain} 的端口 ${get_port} 不是有效数字,使用默认端口443"get_port=443fiecho "正在检查 ${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}" ]]; thenecho "错误:无法获取 ${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证书检查失败,请手动检查"'"}}'continuefiEND_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 ]; thenecho "警告:${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 doneecho "SSL证书检查完成!"