Last active
July 21, 2022 08:01
-
-
Save baikaresandip/50d1aa8e5ada39765ec6f0dfc552f533 to your computer and use it in GitHub Desktop.
SSL expiry date calculate in Python for multiple Domains
This file contains 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
import socket | |
import ssl | |
import datetime | |
import time | |
domains_url = [ | |
"google.com", | |
"github.com" | |
] | |
def ssl_expiry_datetime(hostname): | |
ssl_dateformat = r'%b %d %H:%M:%S %Y %Z' | |
context = ssl.create_default_context() | |
context.check_hostname = False | |
conn = context.wrap_socket( | |
socket.socket(socket.AF_INET), | |
server_hostname=hostname, | |
) | |
# 5 second timeout | |
conn.settimeout(5.0) | |
conn.connect((hostname, 443)) | |
ssl_info = conn.getpeercert() | |
# Python datetime object | |
return datetime.datetime.strptime(ssl_info['notAfter'], ssl_dateformat) | |
if __name__ == "__main__": | |
for value in domains_url: | |
now = datetime.datetime.now() | |
try: | |
expire = ssl_expiry_datetime(value) | |
diff = expire - now | |
print ("Domain name: {} Expiry Date: {} Expiry Day: {}\n".format(value,expire.strftime("%Y-%m-%d"),diff.days)) | |
except Exception as e: | |
print (e) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment