-
Notifications
You must be signed in to change notification settings - Fork 7
/
certificate_varify.py
63 lines (55 loc) · 2.08 KB
/
certificate_varify.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import ssl
import socket
import datetime
import boto3
client = boto3.client("ses", region_name="us-east-1")
print(f"Program to check SSL certificate validity and expiration date\n")
##opening file
with open("server_ip.txt") as ip_file:
##check certificate expiration
for ip in ip_file:
try:
host, port = ip.strip().split(":")
print(f"\nChecking certifcate for server {host}")
context = ssl.create_default_context()
with socket.create_connection((host, port)) as sock:
with context.wrap_socket(sock, server_hostname=host) as ssock:
certificate = ssock.getpeercert()
certExpires = datetime.datetime.strptime(
certificate["notAfter"], "%b %d %H:%M:%S %Y %Z"
)
daysToExpiration = (certExpires - datetime.datetime.now()).days
print(f"Expires on: {certExpires} in {daysToExpiration} days")
##preparing mailbody
mailbody = (
"Server name: "
+ host
+ ", expires in "
+ str(daysToExpiration)
+ " days."
)
except:
print(f"error on connection to Server, {host}")
##sending ses email
if daysToExpiration < 45:
response = client.send_email(
Destination={
"ToAddresses": ["[email protected]"],
},
Message={
"Body": {
"Text": {
"Charset": "UTF-8",
"Data": "The following requires attention; "
+ mailbody
+ "\nThank you.",
},
},
"Subject": {
"Charset": "UTF-8",
"Data": "Certificate Expiring Soon",
},
},
Source="[email protected]",
)
print(f"\nCert check complete!")