09-18-2023 06:29 AM
Solved! Go to Solution.
09-18-2023 07:12 AM - edited 09-18-2023 07:13 AM
If you query the endpoint without the hostname parameter, you'll get all devices. With this info you can build for example a hostname to ID dict (assuming the hostname is unique):
import requests
requests.packages.urllib3.disable_warnings()
import json
dnac = "sandboxdnac.cisco.com"
user = "devnetuser"
password = "Cisco123!"
r = requests.post(f"https://{dnac}/dna/system/api/v1/auth/token", auth=(user, password), verify=False)
if r.ok:
token = r.json().get("Token")
r = requests.get(f"https://{dnac}/dna/intent/api/v1/network-device", headers={"X-Auth-Token": token}, verify=False)
if r.ok:
hostname2id = {}
for dev in r.json().get("response"):
print(f"hostname: {dev.get('hostname')} ==> ID: {dev.get('id')}")
hostname2id[dev.get("hostname")] = dev.get("id")
# print all
print(json.dumps(hostname2id, indent=1))
# get id for specific hostname
print(f"id for sw3 is {hostname2id.get('sw3')}")
else:
print("Error while getting infos from DNAC API")
exit(1)
Output:
{
"sw1": "32446e0a-032b-4724-93e9-acbbab47371b",
"sw2": "c069bc2c-bfa3-47ef-a37e-35e2f8ed3f01",
"sw3": "5f03d9a9-33df-450e-b0c3-6bcb5f721688",
"sw4": "826bc2f3-bf3f-465b-ad2e-e5701ff7a46c"
}
id for sw3 is 5f03d9a9-33df-450e-b0c3-6bcb5f721688
09-18-2023 07:12 AM - edited 09-18-2023 07:13 AM
If you query the endpoint without the hostname parameter, you'll get all devices. With this info you can build for example a hostname to ID dict (assuming the hostname is unique):
import requests
requests.packages.urllib3.disable_warnings()
import json
dnac = "sandboxdnac.cisco.com"
user = "devnetuser"
password = "Cisco123!"
r = requests.post(f"https://{dnac}/dna/system/api/v1/auth/token", auth=(user, password), verify=False)
if r.ok:
token = r.json().get("Token")
r = requests.get(f"https://{dnac}/dna/intent/api/v1/network-device", headers={"X-Auth-Token": token}, verify=False)
if r.ok:
hostname2id = {}
for dev in r.json().get("response"):
print(f"hostname: {dev.get('hostname')} ==> ID: {dev.get('id')}")
hostname2id[dev.get("hostname")] = dev.get("id")
# print all
print(json.dumps(hostname2id, indent=1))
# get id for specific hostname
print(f"id for sw3 is {hostname2id.get('sw3')}")
else:
print("Error while getting infos from DNAC API")
exit(1)
Output:
{
"sw1": "32446e0a-032b-4724-93e9-acbbab47371b",
"sw2": "c069bc2c-bfa3-47ef-a37e-35e2f8ed3f01",
"sw3": "5f03d9a9-33df-450e-b0c3-6bcb5f721688",
"sw4": "826bc2f3-bf3f-465b-ad2e-e5701ff7a46c"
}
id for sw3 is 5f03d9a9-33df-450e-b0c3-6bcb5f721688
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