大家注意:因为微信最近又改了推送机制,经常有小伙伴说错过了之前被删的文章,比如前阵子冒着风险写的爬虫,再比如一些限时福利,错过了就是错过了。
所以建议大家加个星标,就能第一时间收到推送!
文档
https://docs.python.org/zh-cn/3/library/socket.html
https://docs.python.org/zh-cn/3/library/ssl.html
1、通过openssl证书获取
openssl x509 -in <cert>.pem -noout -dates
2、通过openssl域名获取
echo | openssl s_client -servername <doman> -connect <doman>:443 2>/dev/null | openssl x509 -noout -dates
3、通过脚本获取curl
# coding: utf-8
# 查询域名证书到期情况
import re
import subprocess
from datetime import datetime
def get_re_match_result(pattern, string):
match = re.search(pattern, string)
return match.group(1)
def parse_time(date_str):
return datetime.strptime(date_str, "%b %d %H:%M:%S %Y GMT")
def format_time(date_time):
return datetime.strftime(date_time, "%Y-%m-%d %H:%M:%S")
def get_cert_info(domain):
"""获取证书信息"""
cmd = f"curl -Ivs https://{domain} --connect-timeout 10"
exitcode, output = subprocess.getstatusoutput(cmd)
# 正则匹配
start_date = get_re_match_result('start date: (.*)', output)
expire_date = get_re_match_result('expire date: (.*)', output)
# 解析匹配结果
start_date = parse_time(start_date)
expire_date = parse_time(expire_date)
return {
'start_date': start_date,
'expire_date': expire_date
}
def get_cert_expire_date(domain):
"""获取证书剩余时间"""
info = get_cert_info(domain)
print(info)
expire_date = info['expire_date']
# 剩余天数
return (expire_date - datetime.now()).days
i