/
domain-checker.py
60 lines (48 loc) · 1.71 KB
/
domain-checker.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
#!/usr/bin/python
import pythonwhois
import sys
import datetime
import socket
import smtplib
domainnames = {
'example': {'com', 'net', 'org'},
}
# Days before expiry on which you would like to be reminded
periods = [30, 14, 7]
# Send warning to the emails
emails = ['user@example.net']
# SMTP authentication data
smtp_auth = {'host': 'mail.example.net', 'user': 'username', 'pass': 'password'}
def main():
messages = ''
problems = ''
for domain, exts in domainnames.items():
for ext in exts:
d = domain+"."+ext
try:
w = pythonwhois.get_whois(d)
if w:
if type(w['expiration_date'][0]) is not datetime.date:
days = (w['expiration_date'][0] - datetime.datetime.utcnow()).days
for p in periods:
if days < p:
messages = messages + "Domain "+d+" will expire in "+str(days)+" days!\n"
else:
problems = problems+"No expiration date found for: "+d+"\n"
else:
problems = problems+"Domain not found: "+d+"\n"
except Exception as e:
problems = problems+d+": "+str(e)+"\n"
if messages != '' and problems == '':
for email in emails:
send_mail(email, messages)
if problems != '' and messages != '':
for email in emails:
send_mail(email, messages+"\n I encountered some problems: \n"+problems)
def send_mail(email, message):
message = 'Subject: %s\n\n Hi there!\n\n %s \n\n Have a beautiful day, \n your Domain Notifier.' % ('IMPORTANT: Domain Notifier', message)
smtp.sendmail(smtp_auth['user'], email, message)
if __name__ == '__main__':
smtp = smtplib.SMTP(smtp_auth['host'], 25)
smtp.login(smtp_auth['user'], smtp_auth['pass'])
main()