08-30-2016 10:12 AM - edited 03-01-2019 04:31 AM
Doing the basic lab.
I'm able to print table for the network devices with hostname type UUID etc.
now I want to select a particular host from the table and print the config of that host.
the logic I'm trying to understand is , 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'])
any help would be greatly appreciated.
Solved! Go to Solution.
08-30-2016 11:13 AM
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
By the way you don't need to get ticket for every API cal,l just do it once in the beginning of your script is fine.
08-30-2016 11:13 AM
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
By the way you don't need to get ticket for every API cal,l just do it once in the beginning of your script is fine.
08-30-2016 11:30 AM
Yup.. works, thanks !, I was too hesitate to run the code by stressing too much that it would not work because does not look like valid URL but seems like it is absolutely fine to pass variable in the url.
.:|:.:|:.
CISCO Lalit Gupta | Systems Engineer - Sales| Single # Reach: +1 408 895 8379 | lagupta@cisco.com<mailto:lagupta@cisco.com>
09-01-2016 09:35 AM
I've follow up question on this lab.
can you explain the logic used to display the correct "id" in the url ?
How does every time its able to pull the right UUID to display the correct config.
I'm not able to decode this code even though its working fine.
id = devices_list[int(user_input)][id_idx]
how does it translate to right UUID all the time. ?
***********************************
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"
09-01-2016 10:42 AM
When we just do
resp= get(api="network-device") # The response (result) from "GET /network-device" request
status = resp.status_code
# print("Status: ",status)
response_json = resp.json() # Get the json-encoded content from response
device = response_json["response"] # network-device
we get many device records like the following
{
"platformId": "AIR-CAP3502I-A-K9",
"lineCardId": null,
"tunnelUdpPort": "16666",
"macAddress": "68:bc:0c:63:4a:b0",
"snmpLocation": "default location",
"lastUpdated": "2016-07-06 20:38:22",
"roleSource": "AUTO",
"tagCount": "0",
"series": "Cisco 3500I Series Unified Access Points",
"softwareVersion": "8.1.14.16",
"memorySize": "NA",
"location": null,
"family": "Unified AP",
"upTime": null,
"hostname": "AP7081.059f.19ca",
"apManagerInterfaceIp": "55.1.1.2",
"lastUpdateTime": 1467837502706,
"collectionStatus": "Managed",
"inventoryStatusDetail": "NA",
"serialNumber": "FGL1548S2YF",
"bootDateTime": null,
"reachabilityFailureReason": "NA",
"snmpContact": "",
"lineCardCount": null,
"locationName": null,
"type": "Cisco 3500I Unified Access Point",
"reachabilityStatus": "Reachable",
"interfaceCount": null,
"managementIpAddress": "55.1.1.3",
"role": "ACCESS",
"id": "17184480-2617-42c3-b267-4fade5f794a9",
"instanceUuid": "17184480-2617-42c3-b267-4fade5f794a9"
},
To extract the attribute we want we can iterate the record like this
for item in device:
print ([item["hostname"],item["managementIpAddress"],item["type"],item["instanceUuid"]])
But we also want to show user all the devices to let them select so we use
for i, item in enumerate(device):
device_list.append([item["hostname"],item["managementIpAddress"],item["type"],item["instanceUuid"]])
#Not showing id to user, it's just a hex string
device_show_list.append([i+1,item["hostname"],item["managementIpAddress"],item["type"]])
device_show_list.append is used for interact with user so we can have something like this
1 AHEC-2960C1 | 165.10.1.31 | Cisco Catalyst 2960C-8PC-L Switch | |
2 AP7081.059f.19ca | 55.1.1.3 | Cisco 3500I Unified Access Point | |
3 Branch-Access1 | 207.1.10.1 | Cisco Catalyst 29xx Stack-able Ethernet Switch | |
4 Branch-Router1 | 207.3.1.1 | Cisco 2911 Integrated Services Router G2 | |
5 Branch2-Router.yourdomain.com 218.1.100.100 Cisco 2911 Integrated Services Router G2 |
Mainly is adding the number 1,2,3... for user to select.
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.
Hope this helps.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide