-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathCheck SSL certificate expiration
50 lines (45 loc) · 1.75 KB
/
Check SSL certificate expiration
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
import sys
import OpenSSL
import ssl
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class SslCheck:
def compose_mail(self,alert_text):
# Compose From,To,Subject header and Body
fromaddr = 'from address'
toaddr = 'to address'
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = 'SSL Expire Alert'
body = alert_text
msg.attach(MIMEText(body,'plain'))
# Connect Your SMTP server.Here i use gmail smtp server.
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(fromaddr,'password')
text = msg.as_string()
server.sendmail(fromaddr,toaddr,text)
server.quit()
def ssl_expire(self,cert):
load_cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,cert)
expire_date = datetime.datetime.strptime(load_cert.get_notAfter().decode(),'%Y%m%d%H%M%SZ')
today = datetime.datetime.today()
delta = expire_date - today
alert_text = 'Your SSL certificate will expire {} days'.format(delta.days)
if delta.days <= 90:
SslCheck.compose_mail(self,alert_text)
else:
print(sys.exc_info()[1])
# ssl certificate expiration check Using website pem
def web_pem(self,site,port):
cert=ssl.get_server_certificate((site,port))
SslCheck.ssl_expire(self,cert)
# ssl certificate expiration check Using host pem
def host_pem(self,cert):
SslCheck.ssl_expire(self,cert)
sslvalid = SslCheck()
sslvalid.web_pem('www.google.com', 443)
#sslvalid.host_pem('/home/joe/Program/Python/Micheal/cert/cilogon-globus.pem')