forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomaincheck.py
More file actions
executable file
·75 lines (64 loc) · 2.42 KB
/
domaincheck.py
File metadata and controls
executable file
·75 lines (64 loc) · 2.42 KB
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
#!/usr/bin/env python
from __future__ import print_function
import sys
try:
import whois
# python-whois from pypi, not whois from pypi or python-whois from debian
# https://bitbucket.org/richardpenman/pywhois
except:
print("Couldn't import whois. Try: pip3 install python-whois")
sys.exit(1)
import socket # for the socket.timeout exception
import datetime
from dateutil.relativedelta import relativedelta
format="%25s %10s %3s %s"
RETRIES = 3
def get_domain(domainname):
for i in range(RETRIES):
try:
domain = whois.whois(name)
return domain
except socket.timeout:
print("%s: timed out, retrying" % domainname)
except ConnectionResetError:
print("%s: ConnectionResetError, retrying" % domainname)
except whois.parser.PywhoisError:
print("%s: No such domain" % domainname)
return None
except Exception as e:
print("%s: unexpected Exception on" % domainname)
print(e)
print("Retrying...")
print("Giving up on %s after %d timeouts" % (domainname, RETRIES))
return None
if __name__ == '__main__':
domainlist = []
for name in sys.argv[1:]:
domain = get_domain(name)
if not domain:
print("Can't get info for %s" % name)
continue
if not domain["expiration_date"]:
print("Can't get expiration date for %s" % name)
continue
elif hasattr(domain["expiration_date"], "__len__"):
# Sometimes python-whois returns a list of two dates,
# for undocumented reasons.
# Typically they're one day apart, with the second date
# being later, but that's probably not safe to count on.
# Find the earliest date in the list.
expdate = min(domain["expiration_date"])
else:
expdate = domain["expiration_date"]
if domain:
domainlist.append((name, expdate.date(), domain.registrar))
domainlist.sort(key = lambda a: a[1])
two_months_from_now = datetime.datetime.today() + relativedelta(months=2)
two_months_from_now = two_months_from_now.date()
print(format % ("Domain", "Expires", "", "Registrar"))
for d in domainlist:
if d[1] < two_months_from_now:
alert = "***"
else:
alert = ""
print(format % (d[0], d[1].strftime('%Y-%m-%d'), alert, d[2]))