通过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

你可能感兴趣的:(ssl,服务器,linux)