#!/bin/bash
# 定义要监控的域名列表
DOMAINS=("example1.com" "example2.com" "example3.com")
# 设定警告阈值(天)
WARNING_DAYS=30
# 获取当前日期
CURRENT_DATE=$(date +"%Y-%m-%d")
# 输出头部信息
echo "SSL 证书过期时间监控报告 - $CURRENT_DATE"
echo "========================================="
# 遍历每个域名
for DOMAIN in "${DOMAINS[@]}"; do
# 获取证书到期日期
EXPIRATION_DATE=$(echo | openssl s_client -servername $DOMAIN -connect $DOMAIN:443 2>/dev/null | openssl x509 -noout -enddate | cut -d= -f2)
EXPIRATION_DATE_FORMATTED=$(date -d "$EXPIRATION_DATE" +"%Y-%m-%d")
# 计算剩余天数
DAYS_LEFT=$(date -d "$EXPIRATION_DATE_FORMATTED" +%s | xargs -I {} echo $(( ({} - $(date +%s)) / 86400 )))
# 输出结果
echo "域名: $DOMAIN"
echo "证书到期日期: $EXPIRATION_DATE_FORMATTED"
echo "剩余天数: $DAYS_LEFT 天"
# 如果剩余天数少于警告阈值,则发出警告
if [ "$DAYS_LEFT" -lt "$WARNING_DAYS" ] && [ "$DAYS_LEFT" -gt 0 ]; then
echo "警告: 证书将在 $DAYS_LEFT 天后过期!"
elif [ "$DAYS_LEFT" -le 0 ]; then
echo "严重警告: 证书已经过期!"
fi
echo "========================================="
done
标签:证书,时效,DAYS,echo,ssl,EXPIRATION,DATE,LEFT
From: https://www.cnblogs.com/f66666/p/18467480