forked from narbehaj/ssl-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual_cert_check.py
139 lines (91 loc) · 3.84 KB
/
manual_cert_check.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
##############################################################################################
# Modules
##############################################################################################
import csv
import time
from datetime import date
import schedule
import smtplib
from email.message import EmailMessage
from ssl_checker import SSLChecker
from email_settings import smtp_server, from_email, to_email, cc_email_1, cc_email_2
##############################################################################################
# Globals
##############################################################################################
## Create a list of target IP addresses from the csv.
with open('NetOpsCerts.csv') as f:
targets = [row['IP Address'] for row in csv.DictReader(f)]
SSLChecker = SSLChecker()
expiring_certs = []
##############################################################################################
# Functions
##############################################################################################
def get_expiring_certs():
args = {
'hosts': targets
}
results = SSLChecker.show_result(SSLChecker.get_args(json_args=args))
for key, value in results.items():
if int(value['days_left']) < 28:
with open('NetOpsCerts.csv') as f:
for row in csv.DictReader(f):
if value['host'] == row['IP Address']:
if 'sslvpn' in row['Hostname'] or 'myallenovery' in row['Hostname']:
expiring_certs.append((row['Supported Service'], value['days_left']))
else:
expiring_certs.append((row['Hostname'], value['days_left']))
return expiring_certs
##############################################################################################
def run_and_email():
"""
Calls the get_expiring_certs() function, writes the results to a log file and to an email.
"""
get_expiring_certs()
today = date.today()
msg = EmailMessage()
if expiring_certs:
results_file = f'netops_certs_{today}.txt'
with open(results_file, 'a') as f:
f.write('NET OPS CERTS EXPIRING < 28 DAYS\n')
f.write('='*len('NET OPS CERTS EXPIRING < 28 DAYS'))
f.write('\n')
for elem in expiring_certs: f.write(str(elem[0]) + ': ' + str(elem[1]) + ' Days\n')
f.write('\n\n')
with open(results_file) as rf:
msg.set_content(rf.read())
msg['Subject'] = f'Weekly Net Ops Certs Check - {today}'
msg['From'] = f'{from_email}'
msg['To'] = f'{to_email}'
msg['Cc'] = f'{cc_email_1}, {cc_email_2}'
s = smtplib.SMTP(f'{smtp_server}')
s.send_message(msg)
s.quit()
else:
email_content = f"""
No certs due to expire as of {today}.
All is well. Have a fantastic day.
"""
msg.set_content(email_content)
msg['Subject'] = f'Weekly Net Ops Certs Check - {today}'
msg['From'] = f'{from_email}'
msg['To'] = f'{to_email}'
msg['Cc'] = f'{cc_email_1}'
s = smtplib.SMTP(f'{smtp_server}')
s.send_message(msg)
s.quit()
del expiring_certs[:]
##############################################################################################
def scheduler(start_time):
"""
Small function to schedule when the script will be run.
: param start_time: The time of day the script will be run.
"""
schedule.every().tuesday.at(start_time).do(run_and_email)
while True:
schedule.run_pending()
time.sleep(1)
##############################################################################################
# Run
##############################################################################################
if __name__ == '__main__':
run_and_email()