-
Notifications
You must be signed in to change notification settings - Fork 0
/
domaincheck.py
68 lines (60 loc) · 2.01 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
68
# domaincheck.py
# look up information about domain(s) and display report
import socket
import click
import whois
@click.command()
@click.argument('hostnames', nargs=-1)
@click.option(
'--hostsfile',
type=click.File('r'),
required=False,
)
def check_host_names(hostsfile, hostnames):
"""
Check IP addresses, domain registration, nameservers, and zone records
"""
if hostnames:
add_s = ''
host_count = len(hostnames)
if host_count > 1: add_s = 's'
args_listed = ('Hostname{} specified: {}'.format(add_s, host_count))
click.echo(args_listed)
get_host_data(hostnames)
elif hostsfile:
hosts_in_file = hostsfile.readlines()
hosts_in_file = [x.strip() for x in hosts_in_file]
# click.echo(hosts_in_file)
get_host_data(hosts_in_file)
else: # no hostnamespassed as argument or filename
click.echo('Please provide at least one domain name to check!')
def get_host_data(host_list):
host_data = ''
for i in host_list:
ip_addr = socket.gethostbyname(i)
whois_data = whois.whois(i)
# click.echo(whois_data)
# click.echo('{} - {}'.format(i, ip_addr))
reg = whois_data['registrar']
expires = whois_data['expiration_date']
# get the lowercase domain name
dnames = whois_data['domain_name']
if isinstance(dnames, list):
dnames = dnames[0]
dnames = dnames.lower()
# get the first nameserver
nservers = whois_data['name_servers']
ns0 = nservers[0].lower()
click.echo(
'{}\t{}\t{}\t{}\t{}\t{}'.format(
i,
ip_addr,
dnames,
reg,
expires,
ns0,
)
)
return host_data
if __name__ == '__main__':
check_host_names()