linux - How to get the expiration date of ssl certificate using python -
import socket import ssl
def ssl_expiry_datetime(hostname): ssl_date_fmt = r'%b %d %h:%m:%s %y %z' context = ssl.create_default_context() conn = context.wrap_socket( socket.socket(socket.af_inet), server_hostname=hostname, ) conn.settimeout(3.0) conn.connect((hostname, 443)) ssl_info = conn.getpeercert() return datetime.datetime.strptime(ssl_info['notafter'], ssl_date_fmt) def ssl_valid_time_remaining(hostname): expires = ssl_expiry_datetime(hostname) logger.debug( hostname, expires.isoformat() ) return expires - datetime.datetime.utcnow() def ssl_expires_in(hostname, buffer_days=14): remaining = ssl_valid_time_remaining(hostname) if remaining < datetime.timedelta(days=0): raise alreadyexpired("cert expired %s days ago" % remaining.days) elif remaining < datetime.timedelta(days=buffer_days): return true else: return false``
i want if ssl services has expired on domain should give output vulnerable.and if ssl services not expired should give output not vulnerable or date on ssl services of particular domain gonna expire.
Comments
Post a Comment