def testCollapsing(self): ip1 = ipaddr.IPv4('1.1.0.0/24') ip2 = ipaddr.IPv4('1.1.1.0/24') ip3 = ipaddr.IPv4('1.1.2.0/24') ip4 = ipaddr.IPv4('1.1.3.0/24') ip5 = ipaddr.IPv4('1.1.4.0/24') # stored in no particular order b/c we want CollapseAddr to call [].sort ip6 = ipaddr.IPv4('1.1.0.0/22') # check that addreses are subsumed properlly. collapsed = ipaddr.collapse_address_list( [ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual( collapsed, [ipaddr.IPv4('1.1.0.0/22'), ipaddr.IPv4('1.1.4.0/24')]) # test that two addresses are supernet'ed properlly collapsed = ipaddr.collapse_address_list([ip1, ip2]) self.assertEqual(collapsed, [ipaddr.IPv4('1.1.0.0/23')]) ip_same1 = ip_same2 = ipaddr.IPv4('1.1.1.1/32') self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ip_same1]) ip1 = ipaddr.IPv6('::2001:1/100') ip2 = ipaddr.IPv6('::2002:1/120') ip3 = ipaddr.IPv6('::2001:1/96') # test that ipv6 addresses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3]) self.assertEqual(collapsed, [ip3])
def finalize(self, extra=None): # count space for cc in self.prefixes4.keys(): pfx4_summarized = ipaddr.collapse_address_list(self.prefixes4[cc]) for p in pfx4_summarized: v4blocks = (int(p.broadcast+1) - int(p.network)) / 256 self.totals4[cc] = self.totals4.get(cc, 0) + v4blocks self.allccs4 = self.allccs4 + v4blocks for cc in self.prefixes6.keys(): pfx6_summarized = ipaddr.collapse_address_list(self.prefixes6[cc]) for p in pfx6_summarized: v6blocks = pow(2,32) / pow(2, p.prefixlen) # v6blocks = 0 self.totals6[cc] = self.totals6.get(cc, 0) + v6blocks self.allccs6 = self.allccs6 + v6blocks # summarize print "\n" for x in self.ccs: print "prefixes included in roas for country code %s: %s, equivalent to %s /24s and %s /32s" % ( x, self.counts.get(x,0), self.totals4.get(x, 0), self.totals6.get(x,0) ) print "\n" print "Total IPv4 /24s for all CCs: %s" % self.allccs4 print "Total IPv6 /32s for all CCs: %s" % self.allccs6
def testCollapsing(self): ip1 = ipaddr.IPv4('1.1.0.0/24') ip2 = ipaddr.IPv4('1.1.1.0/24') ip3 = ipaddr.IPv4('1.1.2.0/24') ip4 = ipaddr.IPv4('1.1.3.0/24') ip5 = ipaddr.IPv4('1.1.4.0/24') # stored in no particular order b/c we want CollapseAddr to call [].sort ip6 = ipaddr.IPv4('1.1.0.0/22') # check that addreses are subsumed properlly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual(collapsed, [ipaddr.IPv4('1.1.0.0/22'), ipaddr.IPv4('1.1.4.0/24')]) # test that two addresses are supernet'ed properlly collapsed = ipaddr.collapse_address_list([ip1, ip2]) self.assertEqual(collapsed, [ipaddr.IPv4('1.1.0.0/23')]) ip_same1 = ip_same2 = ipaddr.IPv4('1.1.1.1/32') self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ip_same1]) ip1 = ipaddr.IPv6('::2001:1/100') ip2 = ipaddr.IPv6('::2002:1/120') ip3 = ipaddr.IPv6('::2001:1/96') # test that ipv6 addresses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3]) self.assertEqual(collapsed, [ip3])
def finalize(self, extra=None): # count space for cc in self.prefixes4.keys(): pfx4_summarized = ipaddr.collapse_address_list(self.prefixes4[cc]) for p in pfx4_summarized: v4blocks = (int(p.broadcast + 1) - int(p.network)) / 256 self.totals4[cc] = self.totals4.get(cc, 0) + v4blocks self.allccs4 = self.allccs4 + v4blocks for cc in self.prefixes6.keys(): pfx6_summarized = ipaddr.collapse_address_list(self.prefixes6[cc]) for p in pfx6_summarized: v6blocks = pow(2, 32) / pow(2, p.prefixlen) # v6blocks = 0 self.totals6[cc] = self.totals6.get(cc, 0) + v6blocks self.allccs6 = self.allccs6 + v6blocks # summarize print "\n" for x in self.ccs: print "prefixes included in roas for country code %s: %s, equivalent to %s /24s and %s /32s" % ( x, self.counts.get(x, 0), self.totals4.get( x, 0), self.totals6.get(x, 0)) print "\n" print "Total IPv4 /24s for all CCs: %s" % self.allccs4 print "Total IPv6 /32s for all CCs: %s" % self.allccs6
def get_net_in_area(): ''' return a dict of kind: { area: [ipaddr('prefix/lenght'),] } ''' dict_net_in_area = dict() # { area: [ipaddr('prefix/lenght'),] } parse = c.CiscoConfParse(OSW_CFG_TXT) ospf_obj_list = parse.find_objects(r'^router ospf') #parse.commit() #L3intf_obj_list = parse.find_objects_with_child(r'^interface *.Ethernet', 'ip address') for line in ospf_obj_list[0].ioscfg: hl = line.split() if hl[0] == 'network': net = hl[1] net_hostmask = hl[2] area = hl[4] if area not in dict_net_in_area: dict_net_in_area[area] = [ ipaddr.IPNetwork(net + '/' + net_hostmask) ] else: dict_net_in_area[area].append( ipaddr.IPNetwork(net + '/' + net_hostmask)) for area in dict_net_in_area: dict_net_in_area[area] = ipaddr.collapse_address_list( dict_net_in_area[area]) return dict_net_in_area
def ipobjects_to_ipranges(addresses): """ Convert a list of IPNetwork objects into a list of strings containing ip ranges as text strings, e.g., "123.123.123.123-123.123.123.234/32" or "123.123.123.123/32". """ # collaps and sort addresses, catch an empty list addresses = list(addresses) if not addresses: return () addresses = collapse_address_list(addresses) addresses = sorted(addresses) addresses = iter(addresses) # prepare conversion loop ranges = list() fmt = lambda s, e, p: ("{0}/{2}" if s == e else "{0}-{1}/{2}").format(s, e, p) start = next(addresses) end = start.broadcast # last address of current range start = start.network # first address of current range for current in addresses: # combine consecutive ranges if possible if (end+1 < current.network): ranges.append(fmt(start, end, 32)) start = current.network end = current.broadcast # add final range ranges.append(fmt(start, end, 32)) return tuple(ranges)
def ipranges_to_ipobjects(ranges): """ Parse a list of ip ranges (texts, e.g. "12.12.12.12-12.12.12.34/32") and return a (compressed) list of IPNetwork objects. Everything beyond a hash ('#') is ignored. Malformed lines are also ignored. """ re_ip4range = re.compile(r""" ^ \s* (?:(?P<start>\d+\.\d+\.\d+\.\d+)-)? (?P<end>\d+\.\d+\.\d+\.\d+)(?:/(?P<mask>\d+))? \s* (?:\#.*)? $ """, re.VERBOSE).match addresses = list() for iprange in ranges: # just skip this line if the regex doesn't match iprange = re_ip4range(iprange) if not iprange: continue iprange = iprange.groupdict() # extract end and (optional) start as IPAddresses end = IPAddress(iprange["end"]) address = IPAddress(iprange["start"] or end) assert address <= end # turn the mask into a string-format-function mask_fmt = "{{}}/{}".format(iprange["mask"] or str(end.max_prefixlen)).format # computation of the size of each subnet is hard, so we prefer while over for while address <= end: addresses.append(IPNetwork(mask_fmt(str(address)))) address += int(addresses[-1].broadcast)-int(addresses[-1].network)+1 return tuple(collapse_address_list(addresses))
def printtree(printtree, node, indent=''): print indent, '-', socket.inet_ntoa(struct.pack('!L',node.vif_addr)), '\t', node.country, node.city, node.site, '\t', node.hostname for child in node.children: childips = map(ipaddr.IPAddress, child.childips) childnets = ipaddr.collapse_address_list(childips) cip = ipaddr.IPAddress(child.vif_addr) for cnet in childnets: print indent, '|- R', cnet, '->', cip printtree(printtree, child, indent+' | ')
def testCollapsing(self): # test only IP addresses including some duplicates ip1 = ipaddr.IPv4Address("1.1.1.0") ip2 = ipaddr.IPv4Address("1.1.1.1") ip3 = ipaddr.IPv4Address("1.1.1.2") ip4 = ipaddr.IPv4Address("1.1.1.3") ip5 = ipaddr.IPv4Address("1.1.1.4") ip6 = ipaddr.IPv4Address("1.1.1.0") # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual(collapsed, [ipaddr.IPv4Network("1.1.1.0/30"), ipaddr.IPv4Network("1.1.1.4/32")]) # test a mix of IP addresses and networks including some duplicates ip1 = ipaddr.IPv4Address("1.1.1.0") ip2 = ipaddr.IPv4Address("1.1.1.1") ip3 = ipaddr.IPv4Address("1.1.1.2") ip4 = ipaddr.IPv4Address("1.1.1.3") ip5 = ipaddr.IPv4Network("1.1.1.4/30") ip6 = ipaddr.IPv4Network("1.1.1.4/30") # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip5, ip1, ip2, ip3, ip4, ip6]) self.assertEqual(collapsed, [ipaddr.IPv4Network("1.1.1.0/29")]) # test only IP networks ip1 = ipaddr.IPv4Network("1.1.0.0/24") ip2 = ipaddr.IPv4Network("1.1.1.0/24") ip3 = ipaddr.IPv4Network("1.1.2.0/24") ip4 = ipaddr.IPv4Network("1.1.3.0/24") ip5 = ipaddr.IPv4Network("1.1.4.0/24") # stored in no particular order b/c we want CollapseAddr to call [].sort ip6 = ipaddr.IPv4Network("1.1.0.0/22") # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual(collapsed, [ipaddr.IPv4Network("1.1.0.0/22"), ipaddr.IPv4Network("1.1.4.0/24")]) # test that two addresses are supernet'ed properly collapsed = ipaddr.collapse_address_list([ip1, ip2]) self.assertEqual(collapsed, [ipaddr.IPv4Network("1.1.0.0/23")]) # test same IP networks ip_same1 = ip_same2 = ipaddr.IPv4Network("1.1.1.1/32") self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ip_same1]) # test same IP addresses ip_same1 = ip_same2 = ipaddr.IPv4Address("1.1.1.1") self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ipaddr.IPNetwork("1.1.1.1/32")]) ip1 = ipaddr.IPv6Network("::2001:1/100") ip2 = ipaddr.IPv6Network("::2002:1/120") ip3 = ipaddr.IPv6Network("::2001:1/96") # test that ipv6 addresses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3]) self.assertEqual(collapsed, [ip3]) # the toejam test ip1 = ipaddr.IPAddress("1.1.1.1") ip2 = ipaddr.IPAddress("::1") self.assertRaises(TypeError, ipaddr.collapse_address_list, [ip1, ip2])
def GetDetailRow(self, result): """Returns an associative list of items for display in a detail table. Args: result: A dict returned by the server. Returns: A list. """ data = [] # Add the IP addresses ips = [ipaddr.IPv4Address(ip) for ip in result.get('externalIpAddresses', [])] if ips: blocks = sorted(ipaddr.collapse_address_list(ips)) ip_info = [('count', len(ips))] if blocks: ip_info.append(('blocks', blocks)) data.append(('ips', ip_info)) # Add the quotas quota_info = [] for quota in result.get('quotas', []): quota_info.append((quota['metric'].lower().replace('_', '-'), '%s/%s' % (str(quota['usage']), str(quota['limit'])))) data.append(('usage', quota_info)) # Add the metadata if result.get('commonInstanceMetadata', []): metadata_container = result.get('commonInstanceMetadata', []) if 'kind' in metadata_container: metadata_container = metadata_container.get('items', []) metadata_info = [] for metadata_entry in metadata_container: metadata_info.append(( metadata_entry.get('key'), metadata_entry.get('value'))) data.append(('common-instance-metadata', metadata_info)) data.append(('common-instance-metadata-fingerprint', result.get('commonInstanceMetadata', {}).get('fingerprint'))) usage_export = result.get('usageExportLocation', []) if usage_export: data.append(('usageExportLocation', [('bucketName', usage_export['bucketName']), ('reportNamePrefix', usage_export['reportNamePrefix'])])) return data
def maketree(maketree, node, parent=None, parentIp=None): routes = [] ctaps = [] for i,child in enumerate(node.children): childips = map(ipaddr.IPAddress, child.childips) childnets = ipaddr.collapse_address_list(childips) cip = ipaddr.IPAddress(child.vif_addr) pip = ipaddr.IPAddress(node.vif_addr+1+i) for cnet in childnets: routes.append((cnet.ip.exploded, cnet.prefixlen, cip.exploded)) ctaps.append( maketree(maketree, child, node, pip) ) if parentIp: routes.append((self.vnet,24,parentIp)) if not parent: label = "root" else: label = None # NS node, first leaf if not ns_chosen and not node.children: ns_chosen.append(True) label = "ns_root" ips = [ ipaddr.IPAddress(node.vif_addr+i) for i in xrange(1+len(node.children)) ] node1, iface1, tap1, tap1ip, _ = self.make_pl_tapnode(pl, ips, inet, hostname = node.hostname, routes = routes, mcastrouter = bool(node.children), mcast = True, label = label, types = ( [ "TapInterface" ] * len(ips) if parent else [ "TunInterface" ] + [ "TapInterface" ] * (len(ips)-1) ) ) for tap, ctap in zip(tap1[1:], ctaps): tap.connector("udp").connect(ctap.connector("udp")) # Store leaves if not node.children: leaves.append((node, node1)) self.add_net_monitor(pl, node1) self.add_vlc_dumper(pl, node1) self.add_vlc_restreamer(pl, node1) #if not parent: # taplabels = [ # t.get_attribute_value("label") # for t in tap1[1:] # ] # self.add_vlc_source(pl, node1, taplabels) return tap1[0]
def compute_aggregate_helper(parent_str, unreachable_children_lst_str): if not unreachable_children_lst_str: return [parent_str] parent_pfx = IPv4Network(parent_str) child_pfxes = [IPv4Network(child_str) for child_str in unreachable_children_lst_str] len_diff = max([child_pfx.prefixlen for child_pfx in child_pfxes]) - parent_pfx.prefixlen assert(len_diff > 0) subnets = set(parent_pfx.subnet(prefixlen_diff=len_diff)) - set(child_pfxes) return [str(pfx) for pfx in collapse_address_list(subnets)]
def measure_reachability(self): ''' Measure reachability ''' try: total = float(2**32) count = 0 to_be_collapsed = [] for k, _ in self.routing_table.iteritems(): to_be_collapsed.append(ipaddr.IPv4Network(k)) after_collapsed = ipaddr.collapse_address_list(to_be_collapsed) for each_network in after_collapsed: count += each_network.numhosts return count/total except Exception as e: print(e) return None
def compute_aggregate_helper(parent_str, unreachable_children_lst_str): if not unreachable_children_lst_str: return [parent_str] parent_pfx = IPv4Network(parent_str) child_pfxes = [ IPv4Network(child_str) for child_str in unreachable_children_lst_str ] len_diff = max([child_pfx.prefixlen for child_pfx in child_pfxes]) - parent_pfx.prefixlen assert (len_diff > 0) subnets = set( parent_pfx.subnet(prefixlen_diff=len_diff)) - set(child_pfxes) return [str(pfx) for pfx in collapse_address_list(subnets)]
def collapse_routing_table(self): ''' Collapse routing table ''' try: start = time.time() unique_route = set() # used to get screen unite 'san' combination, def of san see below time_stamps = {} # used to update timestamp to_be_collapsed = {} # used to store the to_be_collapsed list of CIDR network objects for k, v in self.routing_table.iteritems(): # k is CIDR, v is the other attributes (Dict) san = (v['source_as'], v['as_path'], v['next_hop']) # san: (source_as, as_path, next_hop) tuple if san not in unique_route: unique_route.add(san) to_be_collapsed[san] = [ipaddr.IPv4Network(k)] # collapse function need the network object as args time_stamps[san] = v['timestamp'] else: # san is in the set to_be_collapsed[san].append(ipaddr.IPv4Network(k)) # add this CIDR to the to do list if v['timestamp'] > time_stamps[san]: # update timestamp if current is larger time_stamps[san] = v['timestamp'] new_routing_table = {} for san, cidr in to_be_collapsed.iteritems(): # collapse CIDR after_collapsed_cidr = ipaddr.collapse_address_list(cidr) # collapsed CIDR list (may have more than one CIDR inside) for each in after_collapsed_cidr: # for each CIDR, rebuild the routing table cidr_str = str(each.ip)+'/'+str(each.prefixlen) # convert CIDR to string # get timestamp, and unpack 'san' new_routing_table[cidr_str] = {'timestamp': time_stamps[san], 'source_as': san[0], 'as_path': san[1], 'next_hop': san[2]} self.routing_table = new_routing_table end = time.time() print("*******************Time used: ****************************") print(end-start) print("*******************Time used****************************") return True except Exception as e: print(e) return False
def CustomizePrintResult(self, result, table): """Customized result printing for this type. Args: result: json dictionary returned by the server table: the pretty printing table to be customized Returns: None. """ # Add the IP addresses ips = [ipaddr.IPv4Address(ip) for ip in result.get('externalIpAddresses', [])] blocks = sorted(ipaddr.collapse_address_list(ips)) table.AddRow(('', '')) table.AddRow(('ips', '')) table.AddRow((' count', len(ips))) if blocks: table.AddRow((' blocks', blocks[0])) for block in blocks[1:]: table.AddRow(('', block)) # Add the quotas table.AddRow(('', '')) table.AddRow(('usage', '')) for quota in result.get('quotas', []): table.AddRow((' %s' % quota['metric'].lower().replace('_', '-'), '%s/%s' % (str(quota['usage']), str(quota['limit'])))) # Add the metadata if result.get('commonInstanceMetadata', []): table.AddRow(('', '')) table.AddRow(('common-instance-metadata', '')) metadata_container = result.get('commonInstanceMetadata', []) if 'kind' in metadata_container: metadata_container = metadata_container.get('items', []) for metadata_entry in metadata_container: table.AddRow((' %s' % metadata_entry.get('key', ''), self._PresentElement(metadata_entry.get('value', ''))))
def testCollapsing(self): # test only IP addresses including some duplicates ip1 = ipaddr.IPv4Address('1.1.1.0') ip2 = ipaddr.IPv4Address('1.1.1.1') ip3 = ipaddr.IPv4Address('1.1.1.2') ip4 = ipaddr.IPv4Address('1.1.1.3') ip5 = ipaddr.IPv4Address('1.1.1.4') ip6 = ipaddr.IPv4Address('1.1.1.0') # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list( [ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual(collapsed, [ ipaddr.IPv4Network('1.1.1.0/30'), ipaddr.IPv4Network('1.1.1.4/32') ]) # test a mix of IP addresses and networks including some duplicates ip1 = ipaddr.IPv4Address('1.1.1.0') ip2 = ipaddr.IPv4Address('1.1.1.1') ip3 = ipaddr.IPv4Address('1.1.1.2') ip4 = ipaddr.IPv4Address('1.1.1.3') ip5 = ipaddr.IPv4Network('1.1.1.4/30') ip6 = ipaddr.IPv4Network('1.1.1.4/30') # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list( [ip5, ip1, ip2, ip3, ip4, ip6]) self.assertEqual(collapsed, [ipaddr.IPv4Network('1.1.1.0/29')]) # test only IP networks ip1 = ipaddr.IPv4Network('1.1.0.0/24') ip2 = ipaddr.IPv4Network('1.1.1.0/24') ip3 = ipaddr.IPv4Network('1.1.2.0/24') ip4 = ipaddr.IPv4Network('1.1.3.0/24') ip5 = ipaddr.IPv4Network('1.1.4.0/24') # stored in no particular order b/c we want CollapseAddr to call [].sort ip6 = ipaddr.IPv4Network('1.1.0.0/22') # check that addreses are subsumed properly. collapsed = ipaddr.collapse_address_list( [ip1, ip2, ip3, ip4, ip5, ip6]) self.assertEqual(collapsed, [ ipaddr.IPv4Network('1.1.0.0/22'), ipaddr.IPv4Network('1.1.4.0/24') ]) # test that two addresses are supernet'ed properly collapsed = ipaddr.collapse_address_list([ip1, ip2]) self.assertEqual(collapsed, [ipaddr.IPv4Network('1.1.0.0/23')]) # test same IP networks ip_same1 = ip_same2 = ipaddr.IPv4Network('1.1.1.1/32') self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ip_same1]) # test same IP addresses ip_same1 = ip_same2 = ipaddr.IPv4Address('1.1.1.1') self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), [ip_same1]) ip1 = ipaddr.IPv6Network('::2001:1/100') ip2 = ipaddr.IPv6Network('::2002:1/120') ip3 = ipaddr.IPv6Network('::2001:1/96') # test that ipv6 addresses are subsumed properly. collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3]) self.assertEqual(collapsed, [ip3]) # the toejam test ip1 = ipaddr.IPAddress('1.1.1.1') ip2 = ipaddr.IPAddress('::1') self.assertRaises(TypeError, ipaddr.collapse_address_list, [ip1, ip2])
asns = asns.split() assert asns except Exception: if DEBUG: print_exc() sys.exit(1) asns = [asn.replace('AS', '') for asn in asns] nets = list() collapsed = None for asn in asns: try: result = db.get_as_prefixes(asn) if result: for net in result: if IPNetwork(net).version == 4 and IPNetwork( net).version == af: nets.append(IPv4Network(net)) if IPNetwork(net).version == 6 and IPNetwork( net).version == af: nets.append(IPv6Network(net)) collapsed = [str(net) for net in collapse_address_list(nets)] except Exception: if DEBUG: print_exc() pass if collapsed: print '\n'.join(collapsed)
def build_send_rteserv_cmd(self): """build and send the Cisco rteserver cmd to list the subnets for the next ASN When finally a cmd prompt is rec'd on the telnet session output, this means that all the subnets have been listed for the current AS. A throttling fn now kicks in to count the number of "Enter" rec'd. This slows things down, ensures that all output has been rec'd for the current AS, and to generally reduces load on the public routeserver. When processing can continue again, the list of subnet objects is sent to Google's "collapse_address_list" method to condense the list to the smallest number of supernets. Finally the condensed list of subnets are inserted one by one into the binary lookup tree. Next, a new Cisco cmd is built for the next ASN in the target list. Everything starts again for this new ASN. When all the ASNs have been processed, an "exit" cmd is sent to the public rteserver to tear down the telnet session. """ # Wait a bit before sending the next list cmd. This avoids # flooding the routeserver. More importantly, our "enter" cmd loop # can result in a number of cmd prompts once the current list cmd # has finished. This in turn will kick off a bunch of new list # cmds (one for each "enter" seen). So we slooowww things down a bit. # if countdown has not finished yet, # then just wait for next cmd prompt if self.factory.cmd_prompt_cnt > 0: self.factory.cmd_prompt_cnt -= 1 else: # have seen enough cmd prompts so build and send the next # list cmd # reinitialize the counter self.factory.cmd_prompt_cnt = cfg.rs_as_cmd_throttle # Have finished listing the subnets for the current AS collapsed_addr_lst = collapse_address_list(self.my_addr_list) self.my_addr_list = [] myas = cfg.as_search_list[self.factory.as_ptr] if cfg.debug >= cfg.DEBUG_ON_LIST: log.msg("rtesrv: List of subnets for AS {0}: \n {1}".format( myas, collapsed_addr_lst )) # Insert the Google subnet Ipaddr objects in the binary lookup tree for subnet in collapsed_addr_lst: self.factory.Tree.insert(subnet, myas) # point to next AS in the list self.factory.as_ptr += 1 # If all the AS's have been listed then just end the session by # sending "exit" if self.factory.as_ptr >= len(cfg.as_search_list): mycmd = "exit" else: # build the cisco cmd to list the subnets in the next AS mycmd = cfg.RTESRV_CMD.replace( 'nnnn', cfg.as_search_list[self.factory.as_ptr] ) if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("rtesrv: rteserv cmd: {0}".format(mycmd)) # send the cmd self.sendLine(mycmd)
def network(self): "Return the list of networks that cover our list of ip-addys." return ipaddr.collapse_address_list(self._addys)
def build_send_rteserv_cmd(self): """build and send the Cisco rteserver cmd to list the subnets for the next ASN When finally a cmd prompt is rec'd on the telnet session output, this means that all the subnets have been listed for the current AS. A throttling fn now kicks in to count the number of "Enter" rec'd. This slows things down, ensures that all output has been rec'd for the current AS, and to generally reduces load on the public routeserver. When processing can continue again, the list of subnet objects is sent to Google's "collapse_address_list" method to condense the list to the smallest number of supernets. Finally the condensed list of subnets are inserted one by one into the binary lookup tree. Next, a new Cisco cmd is built for the next ASN in the target list. Everything starts again for this new ASN. When all the ASNs have been processed, an "exit" cmd is sent to the public rteserver to tear down the telnet session. """ # Wait a bit before sending the next list cmd. This avoids # flooding the routeserver. More importantly, our "enter" cmd loop # can result in a number of cmd prompts once the current list cmd # has finished. This in turn will kick off a bunch of new list # cmds (one for each "enter" seen). So we slooowww things down a bit. # if countdown has not finished yet, # then just wait for next cmd prompt if self.factory.cmd_prompt_cnt > 0: self.factory.cmd_prompt_cnt -= 1 else: # have seen enough cmd prompts so build and send the next # list cmd # reinitialize the counter self.factory.cmd_prompt_cnt = cfg.rs_as_cmd_throttle # Have finished listing the subnets for the current AS collapsed_addr_lst = collapse_address_list(self.my_addr_list) self.my_addr_list = [] myas = cfg.as_search_list[self.factory.as_ptr] if cfg.debug >= cfg.DEBUG_ON_LIST: log.msg("rtesrv: List of subnets for AS {0}: \n {1}".format( myas, collapsed_addr_lst)) # Insert the Google subnet Ipaddr objects in the binary lookup tree for subnet in collapsed_addr_lst: self.factory.Tree.insert(subnet, myas) # point to next AS in the list self.factory.as_ptr += 1 # If all the AS's have been listed then just end the session by # sending "exit" if self.factory.as_ptr >= len(cfg.as_search_list): mycmd = "exit" else: # build the cisco cmd to list the subnets in the next AS mycmd = cfg.RTESRV_CMD.replace( 'nnnn', cfg.as_search_list[self.factory.as_ptr]) if cfg.debug >= cfg.DEBUG_VERBOSE: log.msg("rtesrv: rteserv cmd: {0}".format(mycmd)) # send the cmd self.sendLine(mycmd)