使用 python3 检测 SSL 证书的过期时间

创建
阅读 176

安装 python3

sudo apt install python3

安装 pip3

sudo apt install python3-pip

安装 openssl 依赖

sudo pip3 install -i https://mirrors.aliyun.com/pypi/simple/ pyOpenSSL

执行

python3 site.py

site.py

import ssl
import OpenSSL
import datetime

def get_cert_expiration(hostname):

    # sni 站点
    port = 443
    conn = ssl.create_connection((hostname, port))
    context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    sock = context.wrap_socket(conn, server_hostname=hostname)
    cert = ssl.DER_cert_to_PEM_cert(sock.getpeercert(True))
    x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
    expiration = datetime.datetime.strptime(
        x509.get_notAfter().decode("utf-8"), "%Y%m%d%H%M%SZ")

    return str(expiration)

sites = [
    "www.yidiankuaile.com",
    "www.yidiankuaile.net",
    "yidiankuaile.oonnnoo.com",
    "lidong.me",
    "www.oonnnoo.com",
    "static.lidong.me",
    "static.oonnnoo.com",
]

for site in sites:
    print(site, get_cert_expiration(site))

本文链接 https://www.yidiankuaile.com/post/python3-https-expiration-date

最后更新
留言
0 条留言