Skip to content

Instantly share code, notes, and snippets.

@mrpatrick
Last active December 17, 2015 03:29
Show Gist options
  • Select an option

  • Save mrpatrick/5543670 to your computer and use it in GitHub Desktop.

Select an option

Save mrpatrick/5543670 to your computer and use it in GitHub Desktop.
Print how many days left until an SSL certificate file expires. Usage: ssl_expire_days.py CERTFILE
import time
import datetime
import subprocess
import sys
cert = sys.argv[1]
date_format = "%m/%d/%Y"
p = subprocess.Popen("openssl x509 -noout -in " + cert + " -enddate | sed 's/^[a-zA-Z]*=//g' | xargs --null date +" + date_format\
+ " -d",stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
if output:
output = output.rstrip("\n")
d0 = datetime.datetime.today()
d1 = datetime.datetime(*(time.strptime(output, date_format)[0:3]))
delta = d1 - d0
print(delta.days)
else:
print("Usage: python ssl_expires_days.py certfile");
@mrpatrick
Copy link
Copy Markdown
Author

Now works with both python 2.4 and 2.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment