Package netaddr :: Module info
[frames] | no frames]

Source Code for Module netaddr.info

  1  #!/usr/bin/env python
 
  2  """
 
  3  Informational data on various network address types.
 
  4  """ 
  5  import pprint 
  6  
 
  7  from netaddr.address import CIDR, Wildcard 
  8  
 
  9  #-----------------------------------------------------------------------------
 
10 -def ipv4_cidr_prefixes():
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 #-----------------------------------------------------------------------------
25 -def ipv6_cidr_prefixes():
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 #----------------------------------------------------------------------------- 49 50 #----------------------------------------------------------------------------- 60 61 #-----------------------------------------------------------------------------
62 -def ipv4_iana_dict(fname):
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 #print prefix, designation, whois, status 82 d.setdefault(status, {}) 83 d[status].setdefault(designation, []) 84 d[status][designation].append(prefix) 85 return d
86 87 #-----------------------------------------------------------------------------
88 -def ipv4_wildcard_lookup(d):
89 """ 90 Returns a lookup that provides 91 """ 92 # FIXME - this whole bit of code is bugged! 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 #pprint.pprint(ipv4_cidr_prefixes()) 122 #pprint.pprint(ipv6_cidr_prefixes()) 123 #print_ipv4_cidr_prefixes() 124 #print_ipv6_cidr_prefixes() 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 # for block in sorted(r_lookup): 136 # print block 137