Cisco Catalyst Center: Python & REST API Essentials

Some time ago, Cisco decided to rebrand the Cisco DNA Center into a brand new name – Cisco Catalyst Center. This move can be confusing for us – network engineers. Especially if we already have automation scripts written to ease our lives. In this article, we will look at how we can gather interesting information about devices from the Cisco Catalyst Center and compare the code to the older one developed for the Cisco DNA Center.

Environment

To test our code, we need an instance of Cisco Catalyst Center. Fortunately, Cisco has an always-on sandbox that we can use.

The code will be based on Python. We also need a requests library to make API calls. If you don’t have requests installed, you can install it using pip:

(.venv) radokochman@Hellfire:~/labs/cisco-catalyst-center-examples$ pip install requests

If our environment is ready, we can go straight to the code!

Code

Let’s take a look at the code first, then we will go through it part by part.

import requests
import json

USER = "devnetuser"
PASSWORD = "Cisco123!"
AUTH = requests.auth.HTTPBasicAuth(USER, PASSWORD)
AUTH_RESPONSE = requests.post(
    "https://sandboxdnac.cisco.com/dna/system/api/v1/auth/token",
    auth=AUTH,
    verify=False,
)

TOKEN = json.loads(AUTH_RESPONSE.text)["Token"]

HEADERS = {"X-Auth-Token": TOKEN}

BASE_URL = "https://sandboxdnac.cisco.com/dna/intent/api/v1"
RESOURCE = "/network-device"

call_response = requests.get(BASE_URL + RESOURCE, headers=HEADERS, verify=False)
devices = json.loads(call_response.text)["response"]

for device in devices:
    print(
        f"Hostname: {device['hostname']}, Type: {device['type']}, "
        f"MAC: {device['macAddress']}, Serial: {device['serialNumber']}"
    )

You can find the code in my Cisco Catalyst Center examples repository.

First, we’re importing modules we want to use later on. We will need a json and requests.

Next, we’re defining variables with credentials that will be used to get token from the Cisco Catalyst Center. Next, we’re sending a POST API call, and if we extract the response we will find a token, which needs to be present in the header under the X-Auth-Token key in the following API calls.

Now, we’re ready to send API calls that will return the information that we want. Since we’re interested in the details about onboarded devices to the Cisco Catalyst Center, we will use a /network-device endpoint.

It may be confusing for some of you at first. “How do I know what endpoint should I use?” or “How do I know what information will I get after sending a request to that endpoint?”. Those are perfectly valid questions if you’re not experienced with REST API operations. If you’re struggling, I recommend to take a look at the documentation. There is a lot of information that could save you a lot of time.

After receiving the response, we have all the device information in a list of dictionaries. Each dictionary represents a separate device. That’s why we’re iterating through the list, and for each device, we’re printing four parameters:

  • Hostname
  • Device type
  • MAC address
  • Serial number

Those are the parameters that I’ve chosen, but feel free to take a look at the documentation, and grab the information you need!

The script output looks like this:

Hostname: sw1, Type: Cisco Catalyst 9000 UADP 8 Port Virtual Switch, MAC: 52:54:00:01:c2:c0, Serial: 9SB9FYAFA2O
Hostname: sw2, Type: Cisco Catalyst 9000 UADP 8 Port Virtual Switch, MAC: 52:54:00:0e:1c:6a, Serial: 9SB9FYAFA21
Hostname: sw3, Type: Cisco Catalyst 9000 UADP 8 Port Virtual Switch, MAC: 52:54:00:0a:1b:4c, Serial: 9SB9FYAFA22
Hostname: sw4, Type: Cisco Catalyst 9000 UADP 8 Port Virtual Switch, MAC: 52:54:00:0f:25:4c, Serial: 9SB9FYAFA23

As a result, we have the collected information presented in a human-readable format.

Differences between Cisco DNA Center and Cisco Catalyst Center

With all the concerns regarding rebranding, the good news is – your scripts from Cisco DNA Center should still work! Back in 2020, I wrote this article about gathering information from the Cisco DNA Center, and they still work! It seems that the rebranding was mainly about the name, not the architecture of the whole solution.

Summary

Today’s script just scratched the surface of Cisco Catalyst Center capabilities, but even a couple of lines of code can bring us a huge value in our day-to-day work. I hope that the presented example will ease your journey through the automation of the Cisco Catalyst Center. With all the capabilities, you can collect tons of information from it. Expect more examples in the future!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *