-
Notifications
You must be signed in to change notification settings - Fork 0
/
certbot-renewal.py
75 lines (65 loc) · 2.57 KB
/
certbot-renewal.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
import requests
import subprocess
import re
from datetime import datetime, timedelta
# Discord webhook URL
webhook_url = "https://discord.com/api/webhooks/<embed>>"
# Discord embed message
def send_discord_message(title, description, expiration_date):
if title == "Certificate Renewed":
color = 3066993 # Green
elif title == "Certificate Expiring Soon":
color = 15844367 # Yellow
else:
title = "Certificate Expired!"
color = 15158332 # Red
embed = {
"title": title,
"description": description,
"color": color,
"fields": [
{
"name": "Expiration Date",
"value": expiration_date
}
],
"thumbnail": {
"url": "https://i.imgur.com/aj1M1iz.png"
}
}
json = {'embeds': [embed]}
response = requests.post(webhook_url, json=json)
print(response.status_code)
print(response.content)
# Check if certificate is expiring soon or has expired and send notification
def check_certificate_expiry(expiration_date):
expiry_datetime = datetime.strptime(expiration_date, "%b %d %H:%M:%S %Y %Z")
warning_period = timedelta(days=15) # Notify if certificate expires within 15 days
if expiry_datetime - datetime.now() <= timedelta(0):
send_discord_message("Certificate Expired", f"Wildcard certificate for {base_domain} has expired.", expiration_date)
elif expiry_datetime - datetime.now() <= warning_period:
send_discord_message("Certificate Expiring Soon", f"Wildcard certificate for {base_domain} will expire on {expiration_date}.", expiration_date)
else:
send_discord_message("Certificate Renewed", f"Wildcard certificate for {base_domain} has been renewed.", expiration_date)
# Get current expiration date
subdomain = ""
base_domain = "whatever.com"
if subdomain:
url = f'https://{subdomain}.{base_domain}'
else:
url = f'https://{base_domain}'
output = subprocess.check_output(['curl', url, '-vI', '--stderr', '-']).decode()
expiration_date = re.search('expire date: (.*)', output).group(1)
print(expiration_date)
# Read previous expiration date from file
try:
with open('expiration_date.txt', 'r') as file:
previous_expiration_date = file.read()
except FileNotFoundError:
previous_expiration_date = ''
# Compare expiration dates and send notification if necessary
if expiration_date != previous_expiration_date:
check_certificate_expiry(expiration_date)
# Save new expiration date
with open('expiration_date.txt', 'w') as file:
file.write(expiration_date)