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

Cisco DNAC API - Get All Interfaces

Netmart
Level 1
Level 1

Hello, 

When running the script as shown below I do receive the following error.

And I am using the same API, "dnac.devices.get_all_interfaces()['response']"  in different script without issues.

Therefore, I assume that it must have to do with line of:

  port_list_2 = [item['portName'] for item in dnac.devices.get_interface_by_id(DeviceIdentifier)['response']]

 

Error:

File "/home/ciscoUser/DNAC_scripts/Dnac_numberOfIF_DeviceID_Pagination.py", line 28, in <module>

    port_list_2 = [item['portName'] for item in dnac.devices.get_interface_by_id(DeviceIdentifier)['response']]

  File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/api/v1_3_3/devices.py", line 2187, in get_interface_by_id

    json_data = self._session.get(endpoint_full_url, params=_params)

  File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/restsession.py", line 550, in get

    response = self.request('GET', url, erc, 0, params=params, **kwargs)

  File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/restsession.py", line 471, in request

    check_response_code(response, erc)

  File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/utils.py", line 209, in check_response_code

    raise ApiError(response)

dnacentersdk.exceptions.ApiError: [404] - Request data not found

 

Script

#import dnac sdk

from dnacentersdk import DNACenterAPI

#connection to dnac

from getpass import getpass

import json



username = input("Enter username:")

#password = input("Enter password:")

password = getpass(prompt='Enter password:\n') # the default prompt is 'Password

host = input('Enter full DNAC host name including domain:\n')

#DeviceIdentifier = input ("Device Identifier:")

base_url = "https://{}".format(host)

dnac = DNACenterAPI(username=username, password=password, base_url=base_url, version='1.3.3', verify=False)


response1 = dnac.devices.get_all_interfaces()['response']


#Use the 'dump function to convert the Python dictionary back into a JSON file object.

# Use indent and write function to write each entry as new line

'''

Example: cat dnacGetAllif.json | more

[

  {

    "vlanId": "0",

    "ifIndex": "6",

    "mtu": "1500",

    "adminStatus": "UP",

    "macAddress": "",

'''


with open("dnacGetAllif.json", "w") as outfile:

    json.dump(response1, outfile, indent=2)

    outfile.write("\n")  # Add newline cause Py JSON does not

1 Reply 1

 

port_list_2 = [item['portName'] for item in dnac.devices.get_interface_by_id(DeviceIdentifier)['response']]

 

This code snippet is trying to get a list of port names for the device with the specified ID. However, if the device interface with the specified ID does not exist, then the dnac.devices.get_interface_by_id() method will return an empty response. This will cause the for loop in the line of code above to fail, and the ApiError exception will be raised.

You could check to make sure that the device interface with the specified ID exists before trying to get a list of port names via a for loop, or use the get_device_interfaces() method instead of the get_interface_by_id() method.

Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io