certificateexpiration.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:SlackUptimeMonitor 作者: AndreiD 项目源码 文件源码
def check():
    hostname = "your_server.com"
    port = 443

    num_days = 7
    cert = ssl.get_server_certificate(
        (hostname, port), ssl_version=ssl.PROTOCOL_TLSv1)
    x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
    expiry_date = x509.get_notAfter()
    print(str(expiry_date))
    assert expiry_date, "Cert doesn't have an expiry date."

    ssl_date_fmt = r'%Y%m%d%H%M%SZ'
    expires = datetime.datetime.strptime(str(expiry_date)[2:-1], ssl_date_fmt)
    remaining = (expires - datetime.datetime.utcnow()).days

    if remaining <= num_days:
        print("ALERTING!")
    else:
        print("Not alerting. You have " + str(remaining) + " days")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号