14

Listing Cisco DNA Center Devices Using the API (DevNet)

 3 years ago
source link: https://rowelldionicio.com/listing-cisco-dna-center-devices-api-devnet/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Listing Cisco DNA Center Devices Using the API (DevNet)

November 19, 2020 By Rowell Leave a Comment

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. 

Watch this video on YouTube

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.

image-32.png?resize=1024%2C858&ssl=1

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=

image-29.png?resize=2618%2C928&ssl=1

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.

image-30.png?resize=1024%2C385&ssl=1

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.

getDevicesDNAC.py
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"]))

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK