<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Cisco DNAC API  - Get All Interfaces in Code Exchange</title>
    <link>https://community.cisco.com/t5/code-exchange/cisco-dnac-api-get-all-interfaces/m-p/4932214#M19</link>
    <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;When running the script as shown below I do receive the following error.&lt;/P&gt;&lt;P&gt;And I am using the same API, "dnac.devices.get_all_interfaces()['response']"&amp;nbsp; in different script without issues.&lt;/P&gt;&lt;P&gt;Therefore, I assume that it must have to do with line of:&lt;/P&gt;&lt;P&gt;&amp;nbsp; port_list_2 = [item['portName'] for item in dnac.devices.get_interface_by_id(DeviceIdentifier)['response']]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;File "/home/ciscoUser/DNAC_scripts/Dnac_numberOfIF_DeviceID_Pagination.py", line 28, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; port_list_2 = [item['portName'] for item in dnac.devices.get_interface_by_id(DeviceIdentifier)['response']]&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/api/v1_3_3/devices.py", line 2187, in get_interface_by_id&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; json_data = self._session.get(endpoint_full_url, params=_params)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/restsession.py", line 550, in get&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; response = self.request('GET', url, erc, 0, params=params, **kwargs)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/restsession.py", line 471, in request&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; check_response_code(response, erc)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/utils.py", line 209, in check_response_code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise ApiError(response)&lt;/P&gt;&lt;P&gt;dnacentersdk.exceptions.ApiError: [404] - Request data not found&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Script&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;#import dnac sdk&lt;/P&gt;&lt;P&gt;from dnacentersdk import DNACenterAPI&lt;/P&gt;&lt;P&gt;#connection to dnac&lt;/P&gt;&lt;P&gt;from getpass import getpass&lt;/P&gt;&lt;P&gt;import json&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;username = input("Enter username:")&lt;/P&gt;&lt;P&gt;#password = input("Enter password:")&lt;/P&gt;&lt;P&gt;password = getpass(prompt='Enter password:\n') # the default prompt is 'Password&lt;/P&gt;&lt;P&gt;host = input('Enter full DNAC host name including domain:\n')&lt;/P&gt;&lt;P&gt;#DeviceIdentifier = input ("Device Identifier:")&lt;/P&gt;&lt;P&gt;base_url = "https://{}".format(host)&lt;/P&gt;&lt;P&gt;dnac = DNACenterAPI(username=username, password=password, base_url=base_url, version='1.3.3', verify=False)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;response1 = dnac.devices.get_all_interfaces()['response']&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;#Use the 'dump function to convert the Python dictionary back into a JSON file object.&lt;/P&gt;&lt;P&gt;# Use indent and write function to write each entry as new line&lt;/P&gt;&lt;P&gt;'''&lt;/P&gt;&lt;P&gt;Example: cat dnacGetAllif.json | more&lt;/P&gt;&lt;P&gt;[&lt;/P&gt;&lt;P&gt;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "vlanId": "0",&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "ifIndex": "6",&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "mtu": "1500",&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "adminStatus": "UP",&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "macAddress": "",&lt;/P&gt;&lt;P&gt;'''&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;with open("dnacGetAllif.json", "w") as outfile:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; json.dump(response1, outfile, indent=2)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; outfile.write("\n") &amp;nbsp;# Add newline cause Py JSON does not&lt;/P&gt;</description>
    <pubDate>Mon, 02 Oct 2023 04:53:27 GMT</pubDate>
    <dc:creator>Netmart</dc:creator>
    <dc:date>2023-10-02T04:53:27Z</dc:date>
    <item>
      <title>Cisco DNAC API  - Get All Interfaces</title>
      <link>https://community.cisco.com/t5/code-exchange/cisco-dnac-api-get-all-interfaces/m-p/4932214#M19</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;When running the script as shown below I do receive the following error.&lt;/P&gt;&lt;P&gt;And I am using the same API, "dnac.devices.get_all_interfaces()['response']"&amp;nbsp; in different script without issues.&lt;/P&gt;&lt;P&gt;Therefore, I assume that it must have to do with line of:&lt;/P&gt;&lt;P&gt;&amp;nbsp; port_list_2 = [item['portName'] for item in dnac.devices.get_interface_by_id(DeviceIdentifier)['response']]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;File "/home/ciscoUser/DNAC_scripts/Dnac_numberOfIF_DeviceID_Pagination.py", line 28, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; port_list_2 = [item['portName'] for item in dnac.devices.get_interface_by_id(DeviceIdentifier)['response']]&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/api/v1_3_3/devices.py", line 2187, in get_interface_by_id&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; json_data = self._session.get(endpoint_full_url, params=_params)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/restsession.py", line 550, in get&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; response = self.request('GET', url, erc, 0, params=params, **kwargs)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/restsession.py", line 471, in request&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; check_response_code(response, erc)&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "/home/ciscoUser/.local/lib/python3.8/site-packages/dnacentersdk/utils.py", line 209, in check_response_code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise ApiError(response)&lt;/P&gt;&lt;P&gt;dnacentersdk.exceptions.ApiError: [404] - Request data not found&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Script&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;#import dnac sdk&lt;/P&gt;&lt;P&gt;from dnacentersdk import DNACenterAPI&lt;/P&gt;&lt;P&gt;#connection to dnac&lt;/P&gt;&lt;P&gt;from getpass import getpass&lt;/P&gt;&lt;P&gt;import json&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;username = input("Enter username:")&lt;/P&gt;&lt;P&gt;#password = input("Enter password:")&lt;/P&gt;&lt;P&gt;password = getpass(prompt='Enter password:\n') # the default prompt is 'Password&lt;/P&gt;&lt;P&gt;host = input('Enter full DNAC host name including domain:\n')&lt;/P&gt;&lt;P&gt;#DeviceIdentifier = input ("Device Identifier:")&lt;/P&gt;&lt;P&gt;base_url = "https://{}".format(host)&lt;/P&gt;&lt;P&gt;dnac = DNACenterAPI(username=username, password=password, base_url=base_url, version='1.3.3', verify=False)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;response1 = dnac.devices.get_all_interfaces()['response']&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;#Use the 'dump function to convert the Python dictionary back into a JSON file object.&lt;/P&gt;&lt;P&gt;# Use indent and write function to write each entry as new line&lt;/P&gt;&lt;P&gt;'''&lt;/P&gt;&lt;P&gt;Example: cat dnacGetAllif.json | more&lt;/P&gt;&lt;P&gt;[&lt;/P&gt;&lt;P&gt;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "vlanId": "0",&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "ifIndex": "6",&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "mtu": "1500",&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "adminStatus": "UP",&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; "macAddress": "",&lt;/P&gt;&lt;P&gt;'''&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;with open("dnacGetAllif.json", "w") as outfile:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; json.dump(response1, outfile, indent=2)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; outfile.write("\n") &amp;nbsp;# Add newline cause Py JSON does not&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 04:53:27 GMT</pubDate>
      <guid>https://community.cisco.com/t5/code-exchange/cisco-dnac-api-get-all-interfaces/m-p/4932214#M19</guid>
      <dc:creator>Netmart</dc:creator>
      <dc:date>2023-10-02T04:53:27Z</dc:date>
    </item>
    <item>
      <title>Re: Cisco DNAC API  - Get All Interfaces</title>
      <link>https://community.cisco.com/t5/code-exchange/cisco-dnac-api-get-all-interfaces/m-p/4932467#M23</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;port_list_2 = [item['portName'] for item in dnac.devices.get_interface_by_id(DeviceIdentifier)['response']]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This code snippet&amp;nbsp;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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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&amp;nbsp;use the get_device_interfaces() method instead of the get_interface_by_id() method.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Hope this helps.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 07:36:31 GMT</pubDate>
      <guid>https://community.cisco.com/t5/code-exchange/cisco-dnac-api-get-all-interfaces/m-p/4932467#M23</guid>
      <dc:creator>bigevilbeard</dc:creator>
      <dc:date>2023-10-02T07:36:31Z</dc:date>
    </item>
  </channel>
</rss>

