Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #!/usr/bin/python
  2. # SergAT (https://serg.at)
  3. import os, glob, time, random
  4. from datetime import datetime
  5. from OpenSSL import crypto as c
  6.  
  7. RENEW_AT = 30
  8. RENEW_NOW = False
  9. CERT_LOCATION = '/etc/letsencrypt/live/'
  10.  
  11. def cert_expiration_days(path):
  12. certificate = c.load_certificate(c.FILETYPE_PEM, file(path).read())
  13. cur_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  14. exp_date = str(datetime.strptime(certificate.get_notAfter(),"%Y%m%d%H%M%SZ"))
  15. date_format = "%Y-%m-%d %H:%M:%S"
  16. a = datetime.strptime(cur_date, date_format)
  17. b = datetime.strptime(exp_date, date_format)
  18. delta = b - a
  19. return delta.days
  20.  
  21. if os.path.exists(CERT_LOCATION):
  22. certs = glob.glob(CERT_LOCATION + '*')
  23. for cert_path in certs:
  24. if os.path.isfile(cert_path + '/cert.pem'):
  25. if RENEW_AT > cert_expiration_days(cert_path + '/cert.pem'):
  26. RENEW_NOW = True
  27. break
  28.  
  29. if RENEW_NOW:
  30. time.sleep(random.randint(0,3600))
  31. os.system('/usr/bin/letsencrypt renew')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement