Initial commit

This commit is contained in:
sqozz 2019-10-14 14:34:34 +02:00
commit 8f5ad667aa

56
collectd-pyopenssl.py Executable file
View file

@ -0,0 +1,56 @@
#openssl stuff
import OpenSSL
import socket, ssl
#date stuff
import dateutil.parser as date_parser
from pytz import utc as UTC
from datetime import datetime
#collectd stuff
#import collectd
import ipaddress
import pdb
DOMAIN = "geekify.de"
address_family = "ipv4"
TIMEOUT_SECONDS = 1
def configure(config):
for c in config.children:
print(c)
today = datetime.now(UTC)
port = 443
if address_family == "ipv4":
af = socket.AF_INET
elif address_family == "ipv6":
af = socket.AF_INET6
else:
proto = None
try:
addrinfo = socket.getaddrinfo(DOMAIN, None, af, proto=socket.IPPROTO_TCP)
# addrinfo first contains an array of connections made, we want the first ([0]) and only one we made
# this tuple then contains (family, type, proto, canonname, sockaddr) - we want sockaddr == [4]
# this then can contain either (host, port) or (host, port, flowinfo, scopeid) depending on set address family. Since host is anyways at [0] we take this as final ip address
host = addrinfo[0][4][0]
except socket.gaierror:
host = DOMAIN
context = ssl.create_default_context()
with socket.create_connection((host, port), TIMEOUT_SECONDS) as sock:
with context.wrap_socket(sock, server_hostname=DOMAIN) as sslsock:
der_cert = sslsock.getpeercert(True)
pem_cert = ssl.DER_cert_to_PEM_cert(der_cert)
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_ASN1, der_cert)
exp = x509.get_notAfter().decode("utf-8")
expirary_date = date_parser.parse(exp)
delta = expirary_date - today
print(delta.total_seconds())
pdb.set_trace()