def draw(): """ Select a device/interface and demonstrate.""" print(plain(doc(interface_configuration))) foundInterface = False G = nx.Graph() for device_name in sorted(inventory_connected()): G.add_node(device_name) print("%s:" % device_name) mgmt_name = management_interface(device_name) for interface_name in sorted(interface_names(device_name)): # Choose interface on 'data plane' not 'control plane'. if interface_name == mgmt_name: continue else: match(device_name, interface_name) foundInterface = True demonstrate(device_name, interface_name) to_interface = interface_configuration(device_name, interface_name)[1].split(" ")[1] print("interface:%s" % to_interface) G.add_edge(device_name, to_interface) if not foundInterface: print("There are no suitable network interfaces for this device.") print(G.nodes()) print(G.edges()) # nx.draw(G) nx.draw_networkx(G, with_labels=True) plt.show()
def cdp_neighbors_json(device_name): response = odl_http_get( url_neighbors, {"node-id": device_name}, "application/json", expected_status_code=(200, 404) ) j = json.loads(response.text) container = j["cdp"]["nodes"]["node"][0] mgmt_name = management_interface(device_name) neighbor_set = set() if "neighbors" in container: neighbors = container["neighbors"] # print(json.dumps(neighbors, indent=2)) if "summaries" in neighbors: summary = neighbors["summaries"]["summary"] for device_info in summary: neighbor_id = device_info["device-id"] neighbor_interface = device_info["cdp-neighbor"][0]["port-id"] device_interface = device_info["interface-name"] if device_interface in mgmt_name: continue neighbor_info = ( device_name, device_interface.encode("utf8"), neighbor_id.encode("utf8"), neighbor_interface.encode("utf8"), ) neighbor_set.add(neighbor_info) return neighbor_set
def main(): ''' Select a device and demonstrate.''' print(plain(doc(acl_apply_packet_filter))) inventory = inventory_acl() if not inventory: print('There are no ACL capable devices to examine. Demonstration cancelled.') else: for device_name in inventory: mgmt_name = management_interface(device_name) acl_names = acl_list(device_name) if not acl_names: print('Skip device with no ACLs: ', device_name) continue else: random.shuffle(acl_names) acl_name = acl_names[0] for ic in interface_configuration_tuple(device_name): if ic.name == mgmt_name: continue print('Consider %s %s in=%s, out=%s' % (device_name, ic.name, ic.packet_filter_inbound, ic.packet_filter_outbound)) if not ic.packet_filter_outbound: demonstrate(device_name, ic.name, 'outbound', acl_name) return EX_OK if not ic.packet_filter_inbound: demonstrate(device_name, ic.name, 'inbound', acl_name) return EX_OK print('There are no network interfaces available to apply an ACL. Demonstration cancelled.') return EX_TEMPFAIL
def main(): ''' Select a device and demonstrate.''' print(plain(doc(acl_apply_packet_filter))) inventory = inventory_acl() if not inventory: print( 'There are no ACL capable devices to examine. Demonstration cancelled.' ) else: for device_name in inventory: mgmt_name = management_interface(device_name) acl_names = acl_list(device_name) if not acl_names: print('Skip device with no ACLs: ', device_name) continue else: random.shuffle(acl_names) acl_name = acl_names[0] for ic in interface_configuration_tuple(device_name): if ic.name == mgmt_name: continue print('Consider %s %s in=%s, out=%s' % (device_name, ic.name, ic.packet_filter_inbound, ic.packet_filter_outbound)) if not ic.packet_filter_outbound: demonstrate(device_name, ic.name, 'outbound', acl_name) return os.EX_OK if not ic.packet_filter_inbound: demonstrate(device_name, ic.name, 'inbound', acl_name) return os.EX_OK print( 'There are no network interfaces available to apply an ACL. Demonstration cancelled.' ) return os.EX_TEMPFAIL
def link_info(): #def main(): ii = 0 link_info_dic = {} for device_name in inventory_connected(): response = odl_http_get(url_neighbors, {'node-id': device_name}, 'application/json', expected_status_code=(200, 404)) l = json.loads(response.text) container_l = l["cdp"]["nodes"]["node"][0] mgmt_name = management_interface(device_name) if "neighbors" in container_l: neighbors = container_l["neighbors"] # print(json.dumps(neighbors, indent=2)) if "summaries" in neighbors: summary = neighbors["summaries"]["summary"] for device_info in summary: neighbor_id = device_info["device-id"] if neighbor_id == "kcy": neighbor_id = 'iosxrv-1' elif neighbor_id == "lax": neighbor_id = 'iosxrv-2' elif neighbor_id == "min": neighbor_id = 'iosxrv-3' elif neighbor_id == "por": neighbor_id = 'iosxrv-4' elif neighbor_id == "san": neighbor_id = 'iosxrv-5' elif neighbor_id == "sea": neighbor_id = 'iosxrv-6' elif neighbor_id == "sfc": neighbor_id = 'iosxrv-7' else: neighbor_id = 'iosxrv-8' device_interface = device_info["interface-name"] if device_interface in mgmt_name: continue link_info = (device_name, neighbor_id.encode('utf8')) link_info_re = (neighbor_id.encode('utf8'), device_name) jj = 0 flag = 1 while ii > jj: if link_info_dic[jj] == link_info or link_info_dic[ jj] == link_info_re: flag = 0 jj = jj + 1 else: jj = jj + 1 if flag == 1: link_info_dic[ii] = link_info ii = ii + 1 else: jj = 0 # return link_info_set return link_info_dic
def link_info(): # def main(): ii = 0 link_info_dic = {} for device_name in inventory_connected(): response = odl_http_get( url_neighbors, {"node-id": device_name}, "application/json", expected_status_code=(200, 404) ) l = json.loads(response.text) container_l = l["cdp"]["nodes"]["node"][0] mgmt_name = management_interface(device_name) if "neighbors" in container_l: neighbors = container_l["neighbors"] # print(json.dumps(neighbors, indent=2)) if "summaries" in neighbors: summary = neighbors["summaries"]["summary"] for device_info in summary: neighbor_id = device_info["device-id"] if neighbor_id == "kcy": neighbor_id = "iosxrv-1" elif neighbor_id == "lax": neighbor_id = "iosxrv-2" elif neighbor_id == "min": neighbor_id = "iosxrv-3" elif neighbor_id == "por": neighbor_id = "iosxrv-4" elif neighbor_id == "san": neighbor_id = "iosxrv-5" elif neighbor_id == "sea": neighbor_id = "iosxrv-6" elif neighbor_id == "sfc": neighbor_id = "iosxrv-7" else: neighbor_id = "iosxrv-8" device_interface = device_info["interface-name"] if device_interface in mgmt_name: continue link_info = (device_name, neighbor_id.encode("utf8")) link_info_re = (neighbor_id.encode("utf8"), device_name) jj = 0 flag = 1 while ii > jj: if link_info_dic[jj] == link_info or link_info_dic[jj] == link_info_re: flag = 0 jj = jj + 1 else: jj = jj + 1 if flag == 1: link_info_dic[ii] = link_info ii = ii + 1 else: jj = 0 # return link_info_set return link_info_dic
def main(): ''' Select a device/interface and demonstrate.''' print(plain(doc(interface_configuration_tuple))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Choose interface on 'data plane' not 'control plane'. if interface_name == mgmt_name: continue return demonstrate(device_name, interface_name) print("There are no suitable network devices. Demonstration cancelled.")
def main(): ''' Select a device/interface and demonstrate.''' print(plain(doc(interface_configuration))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Choose interface on 'data plane' not 'control plane'. if interface_name == mgmt_name: continue return demonstrate(device_name, interface_name) print("There are no suitable network devices. Demonstration cancelled.")
def main(): print(plain(doc(interface_configuration_update))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Avoid modifying the management interface. if interface_name == mgmt_name: continue demonstrate(device_name, interface_name) return EX_OK print("There are no suitable network devices. Demonstration cancelled.") return EX_TEMPFAIL
def main(): print(plain(doc(interface_configuration_update))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Avoid modifying the management interface. if interface_name == mgmt_name: continue demonstrate(device_name, interface_name) return os.EX_OK print("There are no suitable network devices. Demonstration cancelled.") return os.EX_TEMPFAIL
def sample_interface(): 'Return a tuple of (device_name, interface_name) if possible; otherwise raise LookupError.' for device_name in inventory_connected(): print device_name management_name = management_interface(device_name) print management_name if management_name == None: continue # Uncertain which interface is for management. for interface_name in interface_names(device_name): print interface_name if interface_name == management_name: continue return (device_name, interface_name) raise LookupError("Unable to find a suitable sample device and interface in the inventory.")
def main(): ''' Select a device/interface and demonstrate.''' print(plain(doc(interface_configuration))) foundInterface = False for device_name in sorted(inventory_connected()): print("%s:" % device_name) mgmt_name = management_interface(device_name) for interface_name in sorted(interface_names(device_name)): # Choose interface on 'data plane' not 'control plane'. if interface_name == mgmt_name: continue else: foundInterface = True demonstrate(device_name, interface_name) if not foundInterface: print("There are no suitable network interfaces for this device.")
def sample_interface(): 'Return a tuple of (device_name, interface_name) if possible; otherwise raise LookupError.' for device_name in inventory_connected(): print device_name management_name = management_interface(device_name) print management_name if management_name == None: continue # Uncertain which interface is for management. for interface_name in interface_names(device_name): print interface_name if interface_name == management_name: continue return (device_name, interface_name) raise LookupError( "Unable to find a suitable sample device and interface in the inventory." )
def sample_destination(device_name): """ Determine a suitable destination for a static route from the specified network device. """ # Implementation: use 'next subnet' of any data interface on the specified device. mgmt_name = management_interface(device_name) interface_list = interface_names(device_name) for interface_name in interface_list: if interface_name == mgmt_name: # Do not configure static routes on the control plane. continue config = interface_configuration_tuple(device_name, interface_name) if config.address is None: # Skip network interface with unassigned IP address. continue interface_network = to_ip_network(config.address, config.netmask) destination_network = next_subnet(interface_network) return destination_network return None
def sample_destination(device_name): """ Determine a suitable destination for a static route from the specified network device. """ # Implementation: use 'next subnet' of any data interface on the specified device. mgmt_name = management_interface(device_name) interface_list = interface_names(device_name) for interface_name in interface_list: if interface_name == mgmt_name: # Do not configure static routes on the control plane. continue config = interface_configuration_tuple(device_name, interface_name) if config.address is None: # Skip network interface with unassigned IP address. continue interface_network = to_ip_network(config.address, config.netmask) destination_network = next_subnet(interface_network) return destination_network return None
def main(): print(plain(doc(interface_configuration_update))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Choose interface on 'data plane' not 'control plane'. if interface_name == mgmt_name: continue interface_config = interface_configuration(device_name, interface_name) if not interface_config.address: continue if not interface_config.shutdown: continue demonstrate(device_name, interface_config) return os.EX_OK print("There are no suitable network devices/interfaces. Demonstration cancelled.") return os.EX_TEMPFAIL
def demonstrate(device_name): ''' Apply function 'static_route_create' to the specified device for a new destination.''' mgmt_name = management_interface(device_name) interface_list = interface_names(device_name) for interface_name in interface_list: if interface_name == mgmt_name: # Do not configure static routes on the control plane. continue config = interface_configuration_tuple(device_name, interface_name) if config.address is None: # Skip network interface with unassigned IP address. continue print() interface_network = to_ip_network(config.address, config.netmask) next_hop_address = match(device_name, interface_network) if next_hop_address is None: print('No end-point for %s/%s/%s/%s' % (device_name, interface_name, config.address, config.netmask)) continue destination_network = static_route_fixture.new_destination(device_name, interface_network) print('static_route_create(%s, %s, %s)' % (device_name, destination_network, next_hop_address)) static_route_create(device_name, destination_network, next_hop_address) return True return False
def cdp_neighbors_json(device_name): response = odl_http_get(url_neighbors, {'node-id': device_name}, 'application/json', expected_status_code=(200, 404)) j = json.loads(response.text) container = j["cdp"]["nodes"]["node"][0] mgmt_name = management_interface(device_name) neighbor_set = set() if "neighbors" in container: neighbors = container["neighbors"] # print(json.dumps(neighbors, indent=2)) if "summaries" in neighbors: summary = neighbors["summaries"]["summary"] for device_info in summary: neighbor_id = device_info["device-id"] neighbor_interface = device_info["cdp-neighbor"][0]["port-id"] device_interface = device_info["interface-name"] if device_interface in mgmt_name: continue neighbor_info = (device_name, device_interface.encode('utf8'), neighbor_id.encode('utf8'), neighbor_interface.encode('utf8')) neighbor_set.add(neighbor_info) return neighbor_set
def demonstrate(device_name): ''' Apply function 'management_interface' to the specified device.''' print('management_interface(' + device_name, end='): ') print(management_interface(device_name))
def demonstrate(device_name): """ Apply function 'management_interface' to the specified device.""" print("management_interface(" + device_name, end="): ") print(management_interface(device_name))
def demonstrate(device_name): ''' Apply function 'management_interface' to the specified device.''' print('management_interface(' + device_name, end='): ') print(management_interface(device_name))