forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domaincheck.py
executable file
·67 lines (56 loc) · 2.08 KB
/
domaincheck.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
#!/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 = 1
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)
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 dates,
# for unknown reasons.
# Check whether they're all the same.
expdate = domain["expiration_date"][0].date()
for e in domain["expiration_date"][1:]:
if e.date() != expdate:
print("Yikes, %s != %s" % (str(e), str(expdate)))
else:
expdate = domain["expiration_date"].date()
if domain:
domainlist.append((name, expdate, 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]))