#!/usr/bin/python3# -*- coding: utf-8 -*-# Fork from https://github.com/AndreiDimport datetimeimport sslimport OpenSSLimport socketimport sysfrom sys import argv# check input argsif len(sys.argv)==1: sys.exit("Check that no input arguments are empty!\npython cert.py snt.sk 443 7")print("____________________________________________________________________________________________________________________________")print("Name: cert.py ")print("Version: 1.20 ")print("Date: 06.11.2019 ")print("Purpose: Checks server SSL certificate for expiry. ") print("OS: Windows 10 / Linux ") print("Requirements: Python 3.x ")print("Usage: python cert.py domain port num_days ")print("Example: python cert.py google.com 443 7 ")print("Author: Peter Donner ")print("License: (c) 2019 - https://idomaster.com/profile/peter-dorner ")print("____________________________________________________________________________________________________________________________")print(" ")def check(): hostname = argv[1] port = argv[2] num_days = int(argv[3]) cert = ssl.get_server_certificate((hostname, port), ssl_version=ssl.PROTOCOL_TLSv1) x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) expiry_date = x509.get_notAfter() #print(str(expiry_date)) assert expiry_date, "Cert doesn't have an expiry date." ssl_date_fmt = r'%Y%m%d%H%M%SZ' expires = datetime.datetime.strptime(str(expiry_date)[2:-1], ssl_date_fmt) remaining = (expires - datetime.datetime.utcnow()).days if remaining <= num_days: print("ALERTING!") else: total = remaining - num_days print("Not alerting. You have " + str(total) + " days.") print("") after = datetime.datetime.strptime(x509.get_notAfter().decode('ascii'), '%Y%m%d%H%M%SZ') before = datetime.datetime.strptime(x509.get_notBefore().decode('ascii'), '%Y%m%d%H%M%SZ') print("Valid from: " + str(before) + "") print("Valid to: " + str(after) + "")check()
I agree to the Idomaster Terms and Conditions, Privacy Policy
Description