HTTPS证书过期检查脚本

场景

下面这个脚本,放到定时任务里跑,可以检查https证书过期时间,并告警

关键词:

  • SSL证书过期
  • HTTPS证书过期
  • 证书过期检查
  • 证书检查脚本
  • https expire check
  • ssl check
  • cert check

由于date命令兼容问题,该脚本不支持MacOS,在CentOS环境下测试有效

脚本全文

#!/usr/bin/env bash

#filename: https-cert-expire-check.sh
# this script is used to check the domain expired and alert
# 直接传入参数 print 可以打印域名,即过期时间

EXPIRE_ALERT_DAY=30  #if domain https cert expire day less than this, then alert to sae team
TMP_PATH=/tmp/https_expire.txt
DEPLOY_IP=192.168.1.10  #这里是告警用的,可以替换成实际的内网IP

SAE_HTTPS_DOMAINS=(
    https://www.baidu.com
    https://www.jd.com
)

if [ "$1" = "print" ]; then
    echo "Domain  Expire"
    for domain in ${SAE_HTTPS_DOMAINS[*]}; do
        curl -v "${domain}" &> ${TMP_PATH}
        EXPIRE_TIME_GMT=$(grep 'expire date' ${TMP_PATH} | awk -F ': ' '{print $2}')
        #change gmt time to beijing time
        EXPIRE_TIME_CST=$(date -d "${EXPIRE_TIME_GMT}" +%F)
        echo "${domain}  ${EXPIRE_TIME_CST}"
    done
    exit
fi

#change day to seconds
EXPIRE_ALERT_SECONDS=$(( EXPIRE_ALERT_DAY * 24 * 60 * 60 ))
for domain in ${SAE_HTTPS_DOMAINS[*]}; do
    curl -v "${domain}" &> ${TMP_PATH}
    EXPIRE_TIME_GMT=$(grep 'expire date' ${TMP_PATH} | awk -F ': ' '{print $2}')
    EXPIRE_TIME_CST_SECONDS=$(date -d "${EXPIRE_TIME_GMT}" +%s)
    CURRENT_TIME_CST_SECONDS=$(date +%s)

    EXPIRE_SECONDS=$(( EXPIRE_TIME_CST_SECONDS - CURRENT_TIME_CST_SECONDS  ))
    # after EXPIRE_SECONDS the domain https expired, if
    # this seconds less than EXPIRE_ALERT_SECONDS, alert
    if [ $EXPIRE_SECONDS -lt $EXPIRE_ALERT_SECONDS ]; then
        # echo "$domain EXPIRE_SECONDS:$EXPIRE_SECONDS EXPIRE_ALERT_SECONDS:$EXPIRE_ALERT_SECONDS"
        /etc/send_alert.py -s https-cert-expire-check -c "$DEPLOY_IP" -l error -S "$DEPLOY_IP" "${domain} https almost expired you should check"
    fi
done

# test manually you can do like this
# curl -v https://www.jd.com &> /tmp/tmp.txt && egrep 'expire date' /tmp/tmp.txt

运行截图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凯尔kyle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值