def get_connectivity(url, method="lldp"): password = os.environ['NXAPI_PSW'] nxapi = NXAPI() nxapi.set_target_url(url) nxapi.set_username("admin") nxapi.set_password(password) nxapi.set_msg_type("cli_show") nxapi.set_out_format("json") nxapi.set_cmd("show switchname") headers, resp = nxapi.send_req() resp_obj = json.loads(resp) switchname = resp_obj["ins_api"]["outputs"]["output"]["body"]["hostname"] if method == "cdp": nxapi.set_cmd("show cdp neighbor") headers, resp = nxapi.send_req() resp_obj = json.loads(resp) neighbors = resp_obj["ins_api"]["outputs"]["output"]["body"][ "TABLE_cdp_neighbor_brief_info"]["ROW_cdp_neighbor_brief_info"] connectivity_list = list() for i in range(0, len(neighbors)): remote_switch = neighbors[i]["device_id"] index = remote_switch.find("(") if index != -1: remote_switch = remote_switch[0:index] one_neighbor = '{0};{1};{2};{3}'.format(switchname, neighbors[i]["intf_id"], remote_switch, neighbors[i]["port_id"]) connectivity_list.append(one_neighbor) else: nxapi.set_cmd("show lldp neighbor") headers, resp = nxapi.send_req() resp_obj = json.loads(resp) neighbors = resp_obj["ins_api"]["outputs"]["output"]["body"][ "TABLE_nbor"]["ROW_nbor"] connectivity_list = list() for i in range(0, len(neighbors)): remote_switch = neighbors[i]["chassis_id"] index = remote_switch.find("(") if index != -1: remote_switch = remote_switch[0:index] one_neighbor = '{0};{1};{2};{3}'.format(switchname, neighbors[i]["l_port_id"], remote_switch, neighbors[i]["port_id"]) connectivity_list.append(one_neighbor) return connectivity_list
def get_connectivity(url, method="cdp"): nxapi = NXAPI() nxapi.set_target_url(url) nxapi.set_username("admin") nxapi.set_password("insieme") nxapi.set_msg_type("cli_show") nxapi.set_out_format("json") nxapi.set_cmd("show switchname") headers, resp = nxapi.send_req() resp_obj = json.loads(resp) switchname = resp_obj["ins_api"]["outputs"]["output"]["body"]["hostname"] if method == "cdp": nxapi.set_cmd("show cdp neighbor") headers, resp = nxapi.send_req() resp_obj = json.loads(resp) neighbors = resp_obj["ins_api"]["outputs"]["output"]["body"]["TABLE_cdp_neighbor_brief_info"][ "ROW_cdp_neighbor_brief_info" ] connectivity_list = list() for i in range(0, len(neighbors)): remote_switch = neighbors[i]["device_id"] index = remote_switch.find("(") if index != -1: remote_switch = remote_switch[0:index] one_neighbor = "{0};{1};{2};{3}".format( switchname, neighbors[i]["intf_id"], remote_switch, neighbors[i]["port_id"] ) connectivity_list.append(one_neighbor) else: nxapi.set_cmd("show lldp neighbor") headers, resp = nxapi.send_req() resp_obj = json.loads(resp) neighbors = resp_obj["ins_api"]["outputs"]["output"]["body"]["TABLE_nbor"]["ROW_nbor"] connectivity_list = list() for i in range(0, len(neighbors)): remote_switch = neighbors[i]["chassis_id"] index = remote_switch.find("(") if index != -1: remote_switch = remote_switch[0:index] one_neighbor = "{0};{1};{2};{3}".format( switchname, neighbors[i]["l_port_id"], remote_switch, neighbors[i]["port_id"] ) connectivity_list.append(one_neighbor) return connectivity_list
def get_routes(url='', username='', password=''): ''' Retrieves a collection of route entries from the FIB of an NXAPI-enabled switch ''' thisnxapi = NXAPI() thisnxapi.set_target_url(url) thisnxapi.set_username(username) thisnxapi.set_password(password) thisnxapi.set_msg_type('cli_show') thisnxapi.set_cmd('show ip route') returndata = thisnxapi.send_req() #print returnData[1] #Uncomment to print the entire XML return doc = xmltodict.parse(returndata[1]) #TODO: need to make more dynamic, for VRFs and IPv6 address families prefixtable = doc['ins_api']['outputs']['output']['body']['TABLE_vrf'] \ ['ROW_vrf']['TABLE_addrf']['ROW_addrf']['TABLE_prefix'] for key in prefixtable: docsub = prefixtable[key] routes = [] for prefix_row in docsub: #executes once for every prefix this_prefix = process_prefix(prefix_row) routes.append(this_prefix) # Print out routes for route in routes: print "The route to ", route.ipprefix, " has ", \ len(route.nexthops), " next-hop solutions" for nexthop in route.nexthops: print "via ", nexthop.ipnexthop, "out of", nexthop.ifname
def process_nexthop(next_hop): '''Processes nexthop data structure''' if not 'ipnexthop' in next_hop: # Ignore prefixes with no next hop - not attached? return None nexthop_obj = NextHop() for key, val in next_hop.iteritems(): # use setattr to set all of the object attributes setattr(nexthop_obj, key, val) return nexthop_obj def process_prefix(prefix_row): '''Processes prefix data structure''' prefix_obj = Prefix() for key, val in prefix_row.iteritems(): # Check for TABLE_path (nested next_hop structure) if key == 'TABLE_path': # Next hop is embedded in ['TABLE_path']['ROW_path'] nexthop_obj = process_nexthop(val['ROW_path']) if not nexthop_obj == None: prefix_obj.nexthops.append(nexthop_obj) else: # Swap hyphen for underscore in field names key = key.replace('-', '_') # use setattr to set all of the object attributes setattr(prefix_obj, key, val) return prefix_obj def get_routes(url='', username='', password=''): ''' Retrieves a collection of route entries from the FIB of an NXAPI-enabled switch ''' thisnxapi = NXAPI() thisnxapi.set_target_url(url) thisnxapi.set_username(username) thisnxapi.set_password(password) thisnxapi.set_msg_type('cli_show') thisnxapi.set_cmd('show ip route') returndata = thisnxapi.send_req() #print returnData[1] #Uncomment to print the entire XML return doc = xmltodict.parse(returndata[1]) #TODO: need to make more dynamic, for VRFs and IPv6 address families prefixtable = doc['ins_api']['outputs']['output']['body']['TABLE_vrf'] \ ['ROW_vrf']['TABLE_addrf']['ROW_addrf']['TABLE_prefix'] for key in prefixtable: docsub = prefixtable[key] routes = [] for prefix_row in docsub: #executes once for every prefix this_prefix = process_prefix(prefix_row) routes.append(this_prefix) # Print out routes for route in routes: print "The route to ", route.ipprefix, " has ", \ len(route.nexthops), " next-hop solutions" for nexthop in route.nexthops: print "via ", nexthop.ipnexthop, "out of", nexthop.ifname if __name__ == '__main__': get_routes('http://10.2.1.8/ins', 'admin', 'Cisco.com')