6

监控域名HTTPS证书过期时间

 3 years ago
source link: https://www.yp14.cn/2020/09/29/%E7%9B%91%E6%8E%A7%E5%9F%9F%E5%90%8DHTTPS%E8%AF%81%E4%B9%A6%E8%BF%87%E6%9C%9F%E6%97%B6%E9%97%B4/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

监控域名HTTPS证书过期时间

2020-09-29

随着各大浏览器对 http 请求标识为 不安全(见下图),现如今强烈推荐网站使用 https 请求。对于运维同学来说,SSL 证书有效期如何监控,不可能去记住每个域名证书到期日期,今天作者分享两个脚本并配合zabbix 来监控 SSL 证书到期日期。这样就不会因为 SSL 证书到期导致网站瘫痪。

http-ssl-1.png

  • 两个脚本都可以使用,任选一个就行。
  • 优化 openssl s_client 命令监测域名时会出现卡死(卡死原因:一般是网站挂掉导致没有响应),导致 zabbix agent 异常问题。
  • 本文不提供 zabbix 配置,具体谷歌搜索一下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#! /usr/bin/env bash

host=$1
port=${2:-"443"}

end_date=`timeout 6 openssl s_client -host $host -port $port -showcerts </dev/null 2>/dev/null |
sed -n '/BEGIN CERTIFICATE/,/END CERT/p' |
openssl x509 -text 2> /dev/null |
sed -n 's/ *Not After : *//p'`

if [ -n "$end_date" ];then
# 把时间转换为时间戳
end_date_seconds=`date '+%s' --date "$end_date"`
# 获取当前时间
now_seconds=`date '+%s'`
echo "($end_date_seconds-$now_seconds)/24/3600" | bc
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#! /usr/bin/env bash

host=$1
port=${2:-"443"}

end_date=`echo | timeout 6 openssl s_client -servername ${host} -connect ${host}:${port} 2>/dev/null |
openssl x509 -noout -dates | grep notAfter | awk -F "=" '{print $NF}'`

if [ -n "$end_date" ];then
# 把时间转换为时间戳
end_date_seconds=`date '+%s' --date "$end_date"`
# 获取当前时间
now_seconds=`date '+%s'`
echo "($end_date_seconds-$now_seconds)/24/3600" | bc
fi
---本文结束感谢您的阅读。微信扫描二维码,关注我的公众号---
ypxz-2.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK