cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1922
Views
0
Helpful
7
Replies

Retrieving all interfaces of NX-OS device using RESTCONF and requests

abyf29041987
Level 1
Level 1

Hello,

I am a newbie and having a hard time to retrieve all interfaces of the nxos device using the RESTCONF. Running the code below returns ERROR code 400 and I can't figure out why. 

 

import requests
import pprint
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

s = requests.Session()
host = "131.226.217.151"
rest_path = "restconf/data/"
port = 443
user = "admin"
password = "Admin_1234!"

s.auth = (user,password)
s.verify = False

s.headers.update({"Content-Type":"application/yang-data+json", "Accept":"application/yang-data+json"})

url = f"https://{host}:{port}/{rest_path}/ietf-interfaces:interfaces"

resp = s.get(url)

if resp.status_code == 200:
         pprint.pprint(resp.json())
else:
         print(f"Failed to retrieve data from device.Status code: {resp.status_code}")

 

 

 

I came across this path on the internet "rest_path = "restconf/data/Cisco-NX-OS-device:System/", but this one is not also working. Generally speaking, how can I query the nxos device to get the proper path depending on the need?

 

Thank you in advance for your help.  

 

7 Replies 7

balaji.bandi
Hall of Fame
Hall of Fame

have you enabled the interface on the nexus OS side? 

 

please look at the good steps :

 

https://blog.networktocode.com/post/Exploring-IOS-XE-and-NX-OS-based-RESTCONF-Implementations-with-YANG-and-Openconfig/

 

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

@balaji.bandi 

Thanks a million for providing this good resource. I understood many details from the resource you provided. I will test with rest_path = 'restconf/data/openconfig-interfaces:interfaces/' as the restconf path and see if it will retrieve all interfaces and print them. 

 

What do you mean by enabling the interface on the nexus side? I enabled restconf on port 443 on the nexus, not interfaces. If I have to do it, thank you for letting me know how.

 

Regards

as long as port interface enable to listen the request, it should work.,

 

@Sergiu.Daniluk  also suggested some tips...cheers!

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Sergiu.Daniluk
VIP Alumni
VIP Alumni

Hi @abyf29041987 

 

The content-type and accept header field needs to be changed to a dot notation:

import requests

url = 'http://<IP>/restconf/data/Cisco-NX-OS-device:System/'

headers = { 
   'Content-Type': 'application/yang.data+json',
   'Accept': 'application/yang.data+json',
   'Cache-Control': 'no-cache'
}

response = requests.request('GET', url, headers=headers, auth=(USERID, PASSWORD))

print(response.text) 

Response:

{
      "System" : {
         "xmlns" : "http://cisco.com/ns/yang/cisco-nx-os-device",
         "acl-items" : {
            "ipv4-items" : {
               "name-items" : {
                  "ACL-list" : [
                     {
                        "name" : "acl_ldx",
                        "perACEStatistics" : 0,
<snip

Stay safe,

Sergiu

Thank you for pointing out that error. Thanks to this resource (https://blog.networktocode.com/post/Exploring-IOS-XE-and-NX-OS-based-RESTCONF-Implementations-with-YANG-and-Openconfig/) I understood why it is wrong to use it on a nxos type.

 

The example you illustrated here works fine but not handling interfaces. What I want is to retrieve and print all all interfaces of the nxos device using the RESTCONF and using this url ''http://<IP>/restconf/data/Cisco-NX-OS-device:System/' returns error 400. Still figuring out why.

 

Regards

 

Try sending the get to "restconf/data/openconfig-interfaces:interfaces"

 

Cheers,

Sergiu

odim-cisco
Level 1
Level 1

I am using cisco  nexus version 9.3(9) and none of the openconfig model is working for me below is sample snippet i am using.

import requests
from requests.auth import HTTPBasicAuth

# Set variables for the switch IP address and credentials
switch_ip = "x.x.x.x"
username = "admin"
password = "admin"

# Set variables for the interface configuration
interface_name = "Eth1/1"
interface_description = "This is a test interface"
interface_enabled = True
interface_speed = 10000
interface_mtu = 1500

# Set the URL for the OpenConfig interface configuration
url = f"https://{switch_ip}/restconf/data/openconfig-interfaces:interfaces/interface={interface_name}"

# Set the headers for the request
headers = {
"Content-Type": "application/yang-data+json",
"Accept": "application/yang-data+json"
}

# Set the payload for the request
payload = f'''
{{
"interface": "{interface_name}",
"config": {{
"name": "{interface_name}",
"description": "{interface_description}",
"enabled": {str(interface_enabled).lower()},
"type": "iana-if-type:ethernetCsmacd",
"mtu": {interface_mtu},
"openconfig-if-ethernet:ethernet": {{
"openconfig-if-ethernet:config": {{
"auto-negotiate": true,
"port-speed": {interface_speed}
}}
}}
}}
}}
'''

# Send the request to the switch
response = requests.patch(url, auth=HTTPBasicAuth(username, password), headers=headers, data=payload, verify=False)
print(response)
# Check the response status code
if response.status_code == 200:
print("Interface configuration successful")
else:
print("Interface configuration failed")

 

error:

<errors>
    <error>
        <error-type>protocol</error-type>
        <error-tag>unknown-namespace</error-tag>
        <error-severity>error</error-severity>
        <error-message xml:lang="en">Namespace=&quot;&quot;</error-message>
        <error-path>/filter</error-path>
    </error>
</errors>