1
2 """
3 Informational data on various network address types.
4 """
5 import pprint
6
7 from netaddr.address import CIDR, Wildcard
8
9
11 """
12 Returns a recordset (list of dicts) of host/network breakdown for IPv4
13 using all of the various CIDR prefixes.
14 """
15 table = []
16 prefix = 32
17 while prefix >= 0:
18 cidr = CIDR('0.0.0.0/%d' % prefix)
19 table.append(dict(prefix=str(cidr), hosts=cidr.size(),
20 networks=2 ** cidr.prefixlen()))
21 prefix -= 1
22 return table
23
24
26 """
27 Returns a recordset (list of dicts) of host/network breakdown for IPv6
28 using all of the various CIDR prefixes.
29 """
30 table = []
31 prefix = 128
32 while prefix >= 0:
33 cidr = CIDR('::/%d' % prefix)
34 table.append(dict(prefix=str(cidr), hosts=cidr.size(),
35 networks=2 ** cidr.prefixlen()))
36 prefix -= 1
37 return table
38
39
41 """
42 Prints a table to stdout of host/network breakdown for IPv4 using CIDR
43 notation.
44 """
45 print '%-10s %-15s %-15s' % ('Prefix', 'Hosts', 'Networks')
46 print '-'*10, '-'*15, '-'*15
47 for record in ipv4_cidr_prefixes():
48 print '%(prefix)-10s %(hosts)15s %(networks)15s' % record
49
50
52 """
53 Prints a table to stdout of host/network breakdown for IPv6 using CIDR
54 notation.
55 """
56 print '%-10s %-40s %-40s' % ('Prefix', 'Hosts', 'Networks')
57 print '-'*10, '-'*40, '-'*40
58 for record in ipv6_cidr_prefixes():
59 print '%(prefix)-10s %(hosts)40s %(networks)40s' % record
60
61
63 """
64 Parses the IANA IPv4 address space text file.
65
66 Returns a dictionary in the format :-
67
68 { '<status>' : { '<designation>' : ['<prefix>'] } }
69 """
70 d = {}
71 for line in open(fname):
72 line = line.strip()
73 if line == '':
74 continue
75 prefix = line[0:8].strip()
76 designation = line[8:45].strip()
77 date = line[45:55].strip()
78 whois = line[55:75].strip()
79 status = line[75:94].strip().lower()
80 if '/' in prefix:
81
82 d.setdefault(status, {})
83 d[status].setdefault(designation, [])
84 d[status][designation].append(prefix)
85 return d
86
87
89 """
90 Returns a lookup that provides
91 """
92
93 for status in d:
94 designations = d[status]
95 for designation in designations:
96 prefixes = designations[designation]
97 wildcards = []
98 if len(prefixes) == 0:
99 continue
100 elif len(prefixes) == 1:
101 (octet, masklen) = prefix.split('/')
102 wc = Wildcard('%d.*.*.*' % int(octet))
103 wildcards.append(str(wc))
104 else:
105 for i, prefix in enumerate(prefixes):
106 (octet, masklen) = prefix.split('/')
107 if i == 0:
108 wc = Wildcard('%d.*.*.*' % int(octet))
109 else:
110 if wc[0][0] == int(octet) - 1:
111 wc[-1][0] = int(octet)
112 else:
113 wildcards.append(str(wc))
114 wc = Wildcard('%d.*.*.*' % int(octet))
115
116 designations[designation] = wildcards
117
118
119 if __name__ == '__main__':
120 import pprint
121
122
123
124
125
126 ipv4_db = r'Z:\src\python\my_modules\netaddr\trunk\data\databases\iana-ipv4-address-space.txt'
127 iana_dict = ipv4_iana_dict(ipv4_db)
128
129 pprint.pprint(iana_dict)
130 print '-'*80
131
132 ipv4_wildcard_lookup(iana_dict)
133 pprint.pprint(iana_dict)
134
135
136
137