cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
507
Views
1
Helpful
1
Replies

DNAC API - get device IDs by list of hostnames with /network-device

vbui6
Level 1
Level 1
Hi all,
 
I'm trying to receive a list of network IDs with a single python query: dna/intent/api/v1/network-device based on a list of approx. 300+ hostnames. 
 
So far I tried to use a single array or a single string: 'hostname': ','.join(hostnames).
 
Is the API capable of processing multiple hostsnames? If yes, How is the Syntax? I want to prevent a solution, which needs to process a query for every single host.
 
Thank you
1 Accepted Solution

Accepted Solutions

Marcel Zehnder
Spotlight
Spotlight

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

 

View solution in original post

1 Reply 1

Marcel Zehnder
Spotlight
Spotlight

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