Created
January 19, 2016 16:27
-
-
Save davebiffuk/da7b6e4c559171c27bcf to your computer and use it in GitHub Desktop.
print number of days' validity left for SSL certificate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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