-
Notifications
You must be signed in to change notification settings - Fork 13
/
arpaname
executable file
·41 lines (38 loc) · 1.33 KB
/
arpaname
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
#!/usr/bin/env python3
# arpaname -- convert IP addresses to their reverse-lookup domain names
#
# This is an improved version of BIND's 'arpaname' with support for CIDR
# prefixes, e.g. `arpaname 2001:db8::/48` outputs the domain for that network.
import argparse
import ipaddress
parser = argparse.ArgumentParser()
parser.add_argument("address", nargs="+")
args = parser.parse_args()
for arg in args.address:
if "/" in arg:
net = ipaddress.ip_network(arg)
rev = net.reverse_pointer.split(".")
if net.version == 6:
full, bits = divmod(net.prefixlen, 4)
full += 2
if bits:
full += 1
start = int(rev[-full], 16)
count = 2 ** (4 - bits)
for i in range(count):
rev[-full] = "%x" % (start + i)
print(".".join(rev[-full:]))
else:
print(".".join(rev[-full:]))
else:
full, bits = divmod(net.prefixlen, 8)
full += 2
if bits:
exit("arpaname: %s: not implemented (bits=%r)" % (rev, bits))
else:
if net.prefixlen == 32:
rev[0] = "0"
print(".".join(rev[-full:]))
else:
net = ipaddress.ip_address(arg)
print(net.reverse_pointer)