first version
This commit is contained in:
commit
488996222b
56
main.py
Normal file
56
main.py
Normal file
@ -0,0 +1,56 @@
|
||||
import ssl
|
||||
import socket
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
|
||||
MOSCOW_TZ = ZoneInfo("Europe/Moscow")
|
||||
|
||||
def get_cert_expiry_utc(host: str, port: int = 443) -> datetime:
|
||||
ctx = ssl.create_default_context()
|
||||
with ctx.wrap_socket(socket.socket(), server_hostname=host) as sock:
|
||||
sock.settimeout(5)
|
||||
sock.connect((host, port))
|
||||
cert = sock.getpeercert()
|
||||
exp_str = cert['notAfter']
|
||||
dt_utc = datetime.strptime(exp_str, '%b %d %H:%M:%S %Y %Z')
|
||||
return dt_utc.replace(tzinfo=ZoneInfo("UTC"))
|
||||
|
||||
def send_email(subject: str, body: str, to_addr: str) -> None:
|
||||
msg = EmailMessage()
|
||||
msg['Subject'] = subject
|
||||
msg['From'] = 'monitor@example.com'
|
||||
msg['To'] = to_addr
|
||||
msg.set_content(body)
|
||||
with smtplib.SMTP('smtp.example.com') as smtp:
|
||||
smtp.login('user', 'pass')
|
||||
smtp.send_message(msg)
|
||||
|
||||
if __name__ == '__main__':
|
||||
domain = 'example.com'
|
||||
|
||||
# 1. Берём время истечения в UTC
|
||||
expiry_utc = get_cert_expiry_utc(domain)
|
||||
|
||||
# 2. Конвертируем expiry в московское время для вывода
|
||||
expiry_moscow = expiry_utc.astimezone(MOSCOW_TZ)
|
||||
|
||||
# 3. Получаем текущее время в Москве
|
||||
now_moscow = datetime.now(MOSCOW_TZ)
|
||||
|
||||
# 4. Считаем дни до окончания
|
||||
days_left = (expiry_moscow.date() - now_moscow.date()).days
|
||||
|
||||
if days_left < 30:
|
||||
subject = f'Сертификат {domain} истекает через {days_left} дней'
|
||||
body = (
|
||||
f'Сертификат домена {domain} истечёт {expiry_moscow:%Y-%m-%d %H:%M:%S %Z}.\n'
|
||||
f'Осталось {days_left} дней (по московскому времени).'
|
||||
)
|
||||
#send_email(subject, body, 'admin@example.com')
|
||||
body = (
|
||||
f'Сертификат домена {domain} истечёт {expiry_moscow:%Y-%m-%d %H:%M:%S %Z}.\n'
|
||||
f'Осталось {days_left} дней (по московскому времени).'
|
||||
)
|
||||
print(body)
|
||||
Loading…
x
Reference in New Issue
Block a user