A small utility to fetch information about IPv6 addresses and possibly also any associated IPv4 address.
Revisão | 6d535bbddf0c15ce53ac4f64a91a2f7d315d484c (tree) |
---|---|
Hora | 2017-06-06 15:56:53 |
Autor | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
Hopefully this is now Python 2/3 agnostic.
@@ -1,5 +1,6 @@ | ||
1 | 1 | #!/usr/bin/python |
2 | 2 | |
3 | +from __future__ import print_function | |
3 | 4 | import socket |
4 | 5 | import sys |
5 | 6 | import struct |
@@ -35,12 +36,12 @@ | ||
35 | 36 | |
36 | 37 | def analyze_ipv4(s, raw, addrtype): |
37 | 38 | numaddr = struct.unpack('!L', raw)[0] |
38 | - print "Got IPv4 address: %s\n - in raw hex 0x%08x" % (s, numaddr) | |
39 | + print("Got IPv4 address: %s\n - in raw hex 0x%08x" % (s, numaddr)) | |
39 | 40 | if addrtype == TYPE_TEREDONAT: |
40 | - print " - in obfuscated hex 0x%08x" % (numaddr ^ 0xffffffff,) | |
41 | + print(" - in obfuscated hex 0x%08x" % (numaddr ^ 0xffffffff,)) | |
41 | 42 | canon = socket.inet_ntop(socket.AF_INET, raw) |
42 | 43 | if canon != s: |
43 | - print " - Canonical %s" % (canon,) | |
44 | + print(" - Canonical %s" % (canon,)) | |
44 | 45 | else: |
45 | 46 | canon = s |
46 | 47 | revname = dns.reversename.from_address(canon) |
@@ -50,8 +51,8 @@ | ||
50 | 51 | except dns.exception.DNSException: |
51 | 52 | pass |
52 | 53 | if addrtype == TYPE_6TO4: |
53 | - print " - The 6to4 IPv6 prefix is 2002:%x:%x::/48" % \ | |
54 | - (numaddr >> 16, numaddr & 0xffff) | |
54 | + print(" - The 6to4 IPv6 prefix is 2002:%x:%x::/48" % \ | |
55 | + (numaddr >> 16, numaddr & 0xffff)) | |
55 | 56 | while (answer is None) and (len(revname) > 4): |
56 | 57 | revname = revname.parent() |
57 | 58 | try: |
@@ -59,25 +60,25 @@ | ||
59 | 60 | except dns.exception.DNSException: |
60 | 61 | pass |
61 | 62 | if answer is None: |
62 | - print " - *** Reverse lookup failed for %s ***" % (revname,) | |
63 | + print(" - *** Reverse lookup failed for %s ***" % (revname,)) | |
63 | 64 | else: |
64 | 65 | if len(revname) != 7: |
65 | - print " - *** The initial PTR lookup failed ***" | |
66 | - print " - %s" % (revname,) | |
66 | + print(" - *** The initial PTR lookup failed ***") | |
67 | + print(" - %s" % (revname,)) | |
67 | 68 | for result in answer.rrset: |
68 | - print " - %s: %s" % (dnstype.to_text(result.rdtype), result) | |
69 | + print(" - %s: %s" % (dnstype.to_text(result.rdtype), result)) | |
69 | 70 | |
70 | 71 | def analyze_ipv6(s, raw): |
71 | 72 | numaddr = struct.unpack('!QQ', raw) |
72 | 73 | numaddr = numaddr[0] * 2**64 + numaddr[1] |
73 | - print "Got IPv6 address: %s\n - in raw hex 0x%016x" % (s, numaddr) | |
74 | + print("Got IPv6 address: %s\n - in raw hex 0x%016x" % (s, numaddr)) | |
74 | 75 | canon = socket.inet_ntop(socket.AF_INET6, raw) |
75 | 76 | if canon != s: |
76 | - print " - Canonical %s" % (canon,) | |
77 | + print(" - Canonical %s" % (canon,)) | |
77 | 78 | else: |
78 | 79 | canon = s |
79 | 80 | if ((numaddr >> 125) & 0x7) != 1: |
80 | - print " - === Not a globally routable address" | |
81 | + print(" - === Not a globally routable address") | |
81 | 82 | return |
82 | 83 | revname = dns.reversename.from_address(canon) |
83 | 84 | answer = None |
@@ -93,37 +94,39 @@ | ||
93 | 94 | pass |
94 | 95 | if answer is not None: |
95 | 96 | if len(revname) != 35: |
96 | - print " - *** The initial PTR lookup failed ***" | |
97 | - print " - %s" % (revname,) | |
97 | + print(" - *** The initial PTR lookup failed ***") | |
98 | + print(" - %s" % (revname,)) | |
98 | 99 | for result in answer.rrset: |
99 | - print " - %s: %s" % (dnstype.to_text(result.rdtype), result) | |
100 | + print( " - %s: %s" % (dnstype.to_text(result.rdtype), result)) | |
100 | 101 | else: |
101 | - print " - *** Unable to reverse lookup any information ***" | |
102 | + print(" - *** Unable to reverse lookup any information ***") | |
102 | 103 | if (numaddr >> 112) == 0x2002: |
103 | 104 | raw4 = struct.pack('!L', (numaddr >> 80) & 0xffffffff) |
104 | 105 | s4 = socket.inet_ntop(socket.AF_INET, raw4) |
105 | - print " - 6to4 address %s" % (s4,) | |
106 | - print "-+-+-+-" | |
106 | + print(" - 6to4 address %s" % (s4,)) | |
107 | + print("-+-+-+-") | |
107 | 108 | analyze_ipv4(s4, raw4, TYPE_6TO4) |
108 | - print "-+-+-+-" | |
109 | + print("-+-+-+-") | |
109 | 110 | elif (numaddr >> 96) == 0x20010000: |
110 | 111 | rawnat4 = struct.pack('!L', (numaddr & 0xffffffff) ^ 0xffffffff) |
111 | 112 | rawserv4 = struct.pack('!L', (numaddr >> 64) & 0xffffffff) |
112 | 113 | snat4 = socket.inet_ntop(socket.AF_INET, rawnat4) |
113 | 114 | sserv4 = socket.inet_ntop(socket.AF_INET, rawserv4) |
114 | - print " - Teredo NAT (the public IPv4 address): %s" % (snat4,) | |
115 | - print " - Teredo server (assigner of the IPv6 address): %s" % (sserv4,) | |
116 | - print "-+-+-+- (Teredo NAT (the public IPv4 address of the system))" | |
115 | + print(" - Teredo NAT (the public IPv4 address): %s" % (snat4,)) | |
116 | + print(" - Teredo server (assigner of the IPv6 address): %s" % (sserv4,)) | |
117 | + print("-+-+-+- (Teredo NAT (the public IPv4 address of the system))") | |
117 | 118 | analyze_ipv4(snat4, rawnat4, TYPE_TEREDONAT) |
118 | - print "-+-+-+- (Teredo server (probably not important))" | |
119 | + print("-+-+-+- (Teredo server (probably not important))") | |
119 | 120 | analyze_ipv4(sserv4, rawserv4, TYPE_TEREDOSERVER) |
120 | 121 | |
121 | 122 | if __name__ == '__main__': |
122 | 123 | if len(sys.argv) > 1: |
123 | - print "===========" | |
124 | + print("===========") | |
124 | 125 | for x in sys.argv[1:]: |
125 | 126 | try: |
126 | 127 | analyze_addr(x) |
127 | - except BadAddress, a: | |
128 | - print >>sys.stderr, "%s doesn't appear to be a valid numeric IPv4 or IPv6 address." % (x, ) | |
129 | - print "===========" | |
128 | + except BadAddress as a: | |
129 | + print("%s doesn't appear to be a valid numeric IPv4 or IPv6 address." \ | |
130 | + % (x, ), | |
131 | + file=sys.stderr) | |
132 | + print("===========") |