通过shell监控域名ssl证书过期时间

#!/bin/bash
source /etc/profile

# 检测https证书有效期
# 设置过期前多少天提醒
remind_time=30
# 设置监测域名,多个域名用英文逗号隔开[,]
domains="www.test.com,www.baidu.com"

domains_list=`echo $domains | awk -F',' '{print $0}' | sed "s/,/ /g"`
for domain in $domains_list
  do
    end_time=$(echo | timeout 1 openssl s_client -servername $domain -connect $domain:443 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | awk -F '=' '{print $2}')
    ([ $? -ne 0 ] || [[ $end_time == '' ]]) &&  exit 10
    end_times=`date -d "$end_time" +%s `
    current_times=`date -d "$(date -u '+%b %d %T %Y GMT') " +%s `
    let left_time=$end_times-$current_times
    days=`expr $left_time / 86400`

    if [ $days -lt $remind_time ]; then
      echo "域名:" $domain "SSL证书剩余:" $days "过期"
    fi
  done
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,请参考以下的 shell 脚本,可以根据域名扫描 nginx 证书是否过期: ```shell #!/bin/bash # 指定需要扫描的证书路径 cert_path="/etc/nginx/ssl" # 指定需要扫描的域名 domains=("example.com" "example.org") # 获取当前日期 current_date=$(date +%s) # 循环遍历域名列表 for domain in "${domains[@]}"; do # 构建证书文件路径 cert_file="${cert_path}/${domain}.crt" # 检查证书文件是否存在 if [ -f "$cert_file" ]; then # 提取证书过期日期 expiration_date=$(openssl x509 -enddate -noout -in "$cert_file" | cut -d "=" -f 2) # 转换过期日期为时间戳 expiration_timestamp=$(date -d "$expiration_date" +%s) # 计算剩余天数 remaining_days=$((($expiration_timestamp - $current_date) / 86400)) # 打印证书路径和剩余天数 echo "域名: $domain" echo "证书路径: $cert_file" echo "剩余天数: $remaining_days" echo else echo "域名: $domain" echo "证书文件不存在" echo fi done ``` 使用方法: 1. 将上述脚本保存为 `check_cert_by_domain.sh` 2. 使用终端进入脚本所在目录 3. 运行 `chmod +x check_cert_by_domain.sh` 给予脚本执行权限 4. 根据需要修改 `cert_path` 和 `domains` 变量,指定证书路径和需要扫描的域名列表 5. 运行 `./check_cert_by_domain.sh` 执行脚本 脚本会根据域名列表扫描对应的证书文件,并打印域名证书路径和剩余天数。如果证书文件不存在,则会输出相应的提示信息。你可以根据实际情况修改 `cert_path` 和 `domains` 变量来指定需要扫描的证书路径和域名列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值