本网站可以通过分类标签帮助你快速筛选出你想看的文章,记住地址:www.Facec.cc

判断域名证书是否到期-sh脚本

#############################################################

#!/bin/bash

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

# 被检测的域名和端口
url=`cat domains.txt`
for localhost in $url
do
domain_name=$localhost

domain_port="443"

advance_warning_days="10"

# 获取脚本所在目录、脚本名称

script_dir=$( cd "$( dirname "$0"  )" && pwd )

script_name=$(basename ${0})

# 机器人
robot_webhook_url="机器人通知地址"


# 创建钉钉机器人告警函数

send_robot_warning() {
  curl -X POST "${robot_webhook_url}" \
    -H 'Content-Type: application/json' \
    -d ' {"msg_type": "text", "content": {"text": "'"$1"'"}}'
}

# 用openssl获取域名的证书到期日期

cert_end_time=$(echo | openssl s_client -servername ${domain_name} -connect ${domain_name}:${domain_port} 2>/dev/null | openssl x509 -noout -dates |grep 'After'| awk -F '=' '{print $2}'| awk -F ' +' '{print $1,$2,$4 }' )

# 将证书到期日期转化为时间戳

cert_end_timestamp=$(date +%s -d "$cert_end_time")

# 将当前日期转化为时间戳

now_timestamp=$(date +%s -d "$(date "+%Y-%m-%d %H:%M:%S")")

# 到期时间减去目前时间,再转化为天数

rest_time=$(($(($cert_end_timestamp - $now_timestamp))/(60*60*24)))

echo "${domain_name} 证书有效天数剩余:${rest_time}"

if [ "${rest_time}" -lt "${advance_warning_days}" ];then
   send_robot_warning "${domain_name}证书有效天数剩余:${rest_time} SSL证书有效期少于${advance_warning_days}天,存在过期风险,请关注!"
fi
done
#############################################################
# linux  

评论