forked from AndreiD/SlackUptimeMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcertificateexpiration.py
More file actions
35 lines (25 loc) · 854 Bytes
/
Copy pathcertificateexpiration.py
File metadata and controls
35 lines (25 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import datetime
import ssl
import OpenSSL
import socket
"""Checks server SSL certificate for expiry."""
def check():
hostname = "your_server.com"
port = 443
num_days = 7
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:
print("Not alerting. You have " + str(remaining) + " days")
check()