• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rowell Dionicio

Get Techie With It

  • Home
  • About
  • Resources
    • Archives
    • Book List
    • YouTube
  • Learn
    • DevNet Associate
    • PCNSA Certified
  • Blog
  • Contact
  • Show Search
Hide Search

Listing Cisco DNA Center Devices Using the API (DevNet)

November 19, 2020 By Rowell 6 Comments

DNA Center (DNAC) is an essential component of Cisco’s intent-based networking. It serves as the central point for network management and network provisioning. There are different functions of DNA Center such as network assurance which provides you with the overall health of the network. 

Included with DNA Center are APIs for further extensibility and network automation. DNAC’s capabilities include intent and integration APIs. 

Intent APIs provide a method of access to automation and assurance workflows. These actions can include configuring interfaces consistently across network devices or configuring security policies network wide.

Integration APIs integrate other applications with DNA Center such as a ticket system or change management and approvals through WebEx Teams.

DNA Center and DevNet Associate Objective

An objective of the DevNet Associate is to construct code to obtain a list of network devices using DNA Center. 

But don’t worry if you don’t have access to DNA Center. We have the DevNet Sandbox to help us out. 

DNA Center Always-On Lab

The DevNet Sandbox is perfect for learning how to use Python against the DNA Center lab. All the information is located on https://developer.cisco.com.

But here are the details of the DNA Center lab environment:

Server URL: https://sandboxdnac.cisco.com
username: devnetuser
password: Cisco123!

Start in Postman

We’ll begin in Postman to interact with DNA Center. Because DNA Center API uses token-based authentication we need to generate a token. This token will be used for our API calls. 

Taking a look at the DNA Center API, we create our token by creating a POST method using the URL, https://sandboxdnac.cisco.com/dna/system/api/v1/auth/token. 

We must add our authentication token to the header, which I grabbed from the DevNet Sandbox. Click on Headers and create an Authorization key with the value of Basic ZGV2bmV0dXNlcjpDaXNjbzEyMyE=

Once you click on Send your should get a response containing our token.

Save that token because we’ll need to include it in our GET requests.

We need to browse the DNA Center API documentation to find out how we’re going to list the devices. Conveniently, there’s an API call just for retrieving the device list.

With Postman, we’ll create a GET request to the url based on the API documentation for getting the device list. That URL is https://sandboxdnac.cisco.com/dna/intent/api/v1/network-device.

Before sending the request, we must add our token to the header. Add a new key, X-AUTH-TOKEN, and paste the token from earlier into the Value field.

The response from DNA Center lists the devices in a pretty JSON format. We can start constructing our Python script with our current scenario by clicking on Code and copying the generated code to Atom.

Python Script

In Atom, we’re going to import the json library. I’m going to take the response and deserialize it so we can get it in json format. This is a method  I’ve learned in previous lessons from DevNet labs. 

The response we receive from DNA Center is a dictionary. We have a key, response, with a value of all the data we need, which is contained in a list. I’m going to pass that value into its own variable with an intent to iterate through the list.

Now that we have our data in a list, I can iterate through the data and create a list of all the devices in DNA Center with a for loop.

import requests
import json

# API URL TO GET A LIST OF NETWORK DEVICES
url = "https://sandboxdnac.cisco.com/dna/intent/api/v1/network-device"

payload = {}

# WE MUST ADD OUR AUTHENTICATION TOKEN TO THE HEADERS
headers = {
    'X-Auth-Token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiI1ZTlkYmI3NzdjZDQ3ZTAwNGM2N2RkMGUiLCJhdXRoU291cmNlIjoiaW50ZXJuYWwiLCJ0ZW5hbnROYW1lIjoiVE5UMCIsInJvbGVzIjpbIjVkYzQ0NGQ1MTQ4NWM1MDA0YzBmYjIxMiJdLCJ0ZW5hbnRJZCI6IjVkYzQ0NGQzMTQ4NWM1MDA0YzBmYjIwYiIsImV4cCI6MTYwNTA1MjYzOSwiaWF0IjoxNjA1MDQ5MDM5LCJqdGkiOiI3MmM5ZDY0Yy0xNjBhLTRlYzAtYTJmYi1mMzQyOGJlY2I4N2QiLCJ1c2VybmFtZSI6ImRldm5ldHVzZXIifQ.GCiuhxuWOPFmqjFSa6PRnqztDlGTTAjV22HdinMY27CAGAelCRcZx1sw9idhzesv538cIFx6XxHdffipIroBGu-a1IG0L6YRnECsMYBG4F_uDLuXPSgXJfZ0hkXB6qawXdtLJtzB9-bQ7hXAn9H_EdD_fW5nX7znTKImuiik70xMo9P0Rb2bOUPn7h0qT6hmwGfa7IMwk11sU-UBm73vt8-c6-2bqWBGiP2DcOcD_r6sIwpUPX4Go9B2fmA5AV4O5Aepa2sMVIfSYrRZv9FP1YwejbOXPq1mck_h31J1nRki8iRwgfTk0n0QoLNgvO2KVPTIxQQqe6rbEHrZYqFiMg'
 }
 
response = requests.request("GET", url, verify=False, headers=headers, data=payload)

# TAKE THE RESPONSE AND TURN IT INTO PYTHON OBJECTS
raw_output = json.loads(response.text)

# raw_output IS A DICTIONARY. TAKE THE VALUE OF response AND ASSIGN TO devices
devices = raw_output["response"]
 
# ITERATE THROUGH THE LIST TO PRINT OUT HOSTNAMES OF DEVICES
for device in devices:
    print("Hostname: {}".format(device["hostname"]))

Share this:

  • Facebook
  • LinkedIn
  • Twitter

Related

Filed Under: Coding, DevNet Associate Tagged With: api, dna center, python

About Rowell

Wi-Fi expert. Coffee addict ☕️. Tech nerd. Business owner.

Reader Interactions

Comments

  1. BP says

    February 3, 2021 at 12:32 pm

    These are great tutorials, but to take things to the next level, I was wondering if you would be willing to do one on the Meraki API that would show us how to do nested REST calls. Each of the tutorials take it as far as making a REST call and then iterating through that array of items to print them, which is awesome – however I want to iterate through each item and use a data point in that item to fetch additional data for that item. One scenario – I manage a “device per network” deployment for remote call agents. Management wants to pull average Latency, Jitter, and Performance Score for each device on each network. For this I believe I would pull the list of networks from organizations, and the list of devices per network, and the perfScore per device. Is this possible?

    Reply
  2. Rowell says

    February 12, 2021 at 5:30 pm

    Sounds like a good challenge! I’ll have to look into this and see when I can dedicate time to tackle it.

    Reply
  3. Atul Parikh says

    April 7, 2021 at 5:09 pm

    How to get hostname for more than 500 devices? It is pulling only first 500 devices

    Reply
    • Rowell says

      April 8, 2021 at 12:09 pm

      Hi Atul,

      I couldn’t figure it out but I was able to find this on a forum: https://community.cisco.com/t5/cisco-digital-network/dna-center-api-500-record-limit/td-p/4125116

      Reply
      • Atul Parikh says

        April 8, 2021 at 12:34 pm

        api_path = “/dna/intent/api/v1/network-device/{index}/{count}”

        Reply
  4. Hugo Romero says

    September 6, 2021 at 11:06 pm

    New to API, How do I generate the authentication token to the header in my own environment (Authorization key with the value of Basic XXXXXXXX)?

    Reply

Leave a Reply Cancel reply

Primary Sidebar

Recent Posts

  • Passed Palo Alto Networks Certified Security Administrator (PCNSA)
  • 5 Years Running
  • Q4 2021 and Yearly Income Report
  • I PASSED JNCIA-MistAI
  • Admins and Role-Based Access Control – PCNSA

Categories

  • bschool
  • Certifications
  • Coding
  • DevNet Associate
  • Events
  • Lab
  • Networking
  • Personal
  • Podcasting
  • Professional
  • Reviews
  • Security
  • Short Stories
  • Uncategorized
  • Wireless

Archives

  • May 2022
  • January 2022
  • December 2021
  • November 2021
  • August 2021
  • July 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • November 2018
  • September 2018
  • August 2018

Copyright © 2022 · Written by Rowell Dionicio · You're awesome.

 

Loading Comments...