Skip to content

Instantly share code, notes, and snippets.

@davebiffuk
Created January 19, 2016 16:27
Show Gist options
  • Save davebiffuk/da7b6e4c559171c27bcf to your computer and use it in GitHub Desktop.
Save davebiffuk/da7b6e4c559171c27bcf to your computer and use it in GitHub Desktop.
print number of days' validity left for SSL certificate
#!/usr/bin/python3
# vi:ts=4:sw=4:ai:cindent
# reads the a .pem file (or stdin) and emits the number of days'
# validity remaining
import ssl
import OpenSSL.crypto
from datetime import datetime
import sys
with open(sys.argv[1], 'rt') if len(sys.argv) > 1 else sys.stdin as f:
st_cert = f.read()
c=OpenSSL.crypto
cert=c.load_certificate(c.FILETYPE_PEM, st_cert)
# print(cert.get_subject().CN)
# ASN1 GENERALIZEDTIME:
# YYYYMMDDhhmmssZ
na = cert.get_notAfter()
if(na == None):
raise Exception("no expiration date")
na = na.decode()
# print(na)
expire_date = datetime.strptime(na, "%Y%m%d%H%M%SZ")
expire_in = (expire_date - datetime.now()).days
if expire_in < 0:
expire_in = 0
print(expire_in)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment