https证书有效期巡检 shell版 python版
python3版本:https://python.01314.cn/201812519.html
# coding: utf-8
# 查询域名证书到期情况
import re
import time
import subprocess
from datetime import datetime
from io import StringIO
def main(domain):
f = StringIO()
comm = f"curl -Ivs https://{domain} --connect-timeout 10"
result = subprocess.getstatusoutput(comm)
f.write(result[1])
m = re.search('start date: (.*?)\n.*?expire date: (.*?)\n.*?common name: (.*?)\n.*?issuer: CN=(.*?)\n', f.getvalue(), re.S)
start_date = m.group(1)
expire_date = m.group(2)
common_name = m.group(3)
issuer = m.group(4)
# time 字符串转时间数组
start_date = time.strptime(start_date, "%b %d %H:%M:%S %Y GMT")
start_date_st = time.strftime("%Y-%m-%d %H:%M:%S", start_date)
# datetime 字符串转时间数组
expire_date = datetime.strptime(expire_date, "%b %d %H:%M:%S %Y GMT")
expire_date_st = datetime.strftime(expire_date,"%Y-%m-%d %H:%M:%S")
# 剩余天数
remaining = (expire_date-datetime.now()).days
print ('域名:', domain)
print ('通用名:', common_name)
print ('开始时间:', start_date_st)
print ('到期时间:', expire_date_st)
print (f'剩余时间: {remaining}天')
print ('颁发机构:', issuer)
print ('*'*30)
time.sleep(0.5)
if __name__ == "__main__":
domains = ['www.01314.cn', 'www.51bbo.com']
for domain in domains:
main(domain)
shell版本:原文:https://blog.csdn.net/weixin_34327223/article/details/92892033?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2aggregatepagefirst_rank_ecpm_v1~rank_v31_ecpm-1-92892033.pc_agg_new_rank&utm_term=%E5%9F%9F%E5%90%8D%E8%AF%81%E4%B9%A6%E6%9C%89%E6%95%88%E6%9C%9F%E5%B7%A1%E6%A3%80&spm=1000.2123.3001.4430
#!/bin/bash
# 检测https证书有效期
source /etc/profile
while read line; do
echo "====================================================================================="
echo "当前检测的域名:" $line
end_time=$(echo | timeout 1 openssl s_client -servername $line -connect $line: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`
echo "剩余天数: " $days
[ $days -lt 30 ] && echo "https 证书有效期少于30天,存在风险"
done < /root/code/https_list
https_list文件内容:
#cat https_list
www.baidu.com
www.qq.com