Able to print table for the network devices with hostname type UUID etc. Now want to select a particular host from the table and print the config of that host. How do I get the "ID" parameter for API url automatically .
ip = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/network-device/id/config"
if I have to add ID manually then it does not make sense.. I'm not able to figure that out.
Here is the code.
import requests
from tabulate import tabulate
import json
import sys
requests.packages.urllib3.disable_warnings()
url = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/ticket"
payload = {"username":"devnetuser","password":"Cisco123!"}
header = {"content-type":"application/json"}
output = requests.post(url,data=json.dumps(payload),headers=header,verify=False)
response_json = output.json()
ST = response_json['response']['serviceTicket']
print(ST)
headers = {"X-Auth-Token": ST}
ip = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/network-device"
response1 = requests.get(ip,headers=headers,verify=False)
network_Devices = response1.json()['response']
#print(json.dumps(network_Devices,indent=4))
devices_list = []
show_device_list = []
for i, item in enumerate(network_Devices):
devices_list.append([item["hostname"],item["managementIpAddress"],item["type"],item["instanceUuid"]])
show_device_list.append([i+1,item['hostname'],item['managementIpAddress'],item['type']])
#print (show_device_list)
print(tabulate(show_device_list,headers=['number','hostname','managementIpAddress','type','id'],tablefmt="rst"))
id = ""
id_idx = 3
while True:
user_input = input('enter your selection')
id = devices_list[int(user_input)-1][id_idx]
break;
url = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/ticket"
payload = {"username":"devnetuser","password":"Cisco123!"}
header = {"content-type":"application/json"}
output = requests.post(url,data=json.dumps(payload),headers=header,verify=False)
response_json = output.json()
ST = response_json['response']['serviceTicket']
print(ST)
headers = {"X-Auth-Token": ST}
ip = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/network-device/id/config"
response_config = requests.get(ip,headers=headers,verify=False)
config = response_config.json()
print(config['response'])
Use
ip = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/network-device/"+id+"/config"
not
ip = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/network-device/id/config"
"id" is a variable
***********************************
devices_list = []
show_device_list = []
for i, item in enumerate(network_Devices):
devices_list.append([item["hostname"],item["managementIpAddress"],item["type"],item["instanceUuid"]])
show_device_list.append([i,item['hostname'],item['managementIpAddress'],item['type']])
id = ""
id_idx = 3
while True:
user_input = input('enter your selection')
id = devices_list[int(user_input)][id_idx]
break;
ip = "https://devnetapi.cisco.com/sandbox/apic_em/api/v1/network-device/"+id+"/config"
device list is two dimensional
[['AHEC-2960C1', '165.10.1.31', 'Cisco Catalyst 2960C-8PC-L Switch', '2dc30cac-072e-4d67-9720-cc302d02695a'], ['AP7081.059f.19ca', '55.1.1.3', 'Cisco 3500I Unified Access Point', '17184480-2617-42c3-b267-4fade5f794a9'], ['Branch-Access1', '207.1.10.1', 'Cisco Catalyst 29xx Stack-able Ethernet Switch', '3ab14801-5c88-477d-bbb5-0aca5e5ba840'], ....]
id_idx = 3
while True:
user_input = input('enter your selection')
id = devices_list[int(user_input)][id_idx]
break;
user_input point to the record position so it should be
int(user_input)-1
id = device_list[int(user_input)-1][id_idx]
id_idx point to the 4th position (0 based) in the record which is the UUID.
Comments
0 comments
Please sign in to leave a comment.