[PYTHON] Obtaining SSL certificate expiration date

This is a function to get the expiration date of the SSL certificate. It's not JST that comes back, so I'm adjusting.

#Read ssl deadline
def get_ssl_end(domain):
    import ssl
    import OpenSSL
    import datetime
    from datetime import datetime as dt
    cert = ssl.get_server_certificate((domain, 443),
    ssl_version=ssl.PROTOCOL_SSLv23)
    x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
    return((dt.strptime(str(x509.get_notAfter())[2:16],'%Y%m%d%H%M%S')  + datetime.timedelta(hours=9)).strftime('%Y %m %d'))

Recommended Posts

Obtaining SSL certificate expiration date
Script to get the expiration date of the SSL certificate
I created a Slack bot that confirms and notifies AWS Lambda of the expiration date of an SSL certificate