Created
December 1, 2020 02:50
-
-
Save ts1/282383057a36f2d662b11c19a09166bc to your computer and use it in GitHub Desktop.
Returns the expiration date of the SSL certificate in the number of days remaining, useful for Zabbix external scripts.
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| import socket, ssl, sys, time, re | |
| if len(sys.argv) < 2: | |
| print('Usage: %s {HOST|URL} [PORT]' % sys.argv[0]) | |
| exit(1) | |
| host = sys.argv[1] | |
| if '://' in host: | |
| protocol, host, _ = re.search(r'(.*?)://(.*?)(/|$)', host).groups() | |
| if protocol == 'http': | |
| print('99999999') | |
| exit() | |
| if len(sys.argv) >= 3: | |
| port = int(sys.argv[2]) | |
| else: | |
| port = 443 | |
| ctx = ssl.create_default_context() | |
| rawsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| sslsock = ctx.wrap_socket(rawsock, server_hostname=host) | |
| try: | |
| sslsock.connect((host, port)) | |
| except ssl.SSLError as e: | |
| print(e, file=sys.stderr) | |
| exit(1) | |
| cert = sslsock.getpeercert() | |
| expire = ssl.cert_time_to_seconds(cert['notAfter']) | |
| seconds = expire - time.time() | |
| print(seconds / (60 * 60 * 24)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment