大家注意:因为微信最近又改了推送机制,经常有小伙伴说错过了之前被删的文章,比如前阵子冒着风险写的爬虫,再比如一些限时福利,错过了就是错过了。
所以建议大家加个星标,就能第一时间收到推送!

文档
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
if __name__ == "__main__":
Python获取SSL证书信息及到期时间全攻略

本文介绍了多种使用Python获取域名SSL证书信息和到期时间的方法,包括通过openssl、curl、socket、pyOpenSSL库以及Domain Admin可视化管理工具。提供详细代码示例和相关文档链接,适用于网络协议和Python开发人员。
最低0.47元/天 解锁文章
916

被折叠的 条评论
为什么被折叠?



