cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements
244
Views
0
Helpful
6
Replies
abyf29041987
Beginner

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

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.  

 

6 REPLIES 6
balaji.bandi
VIP Master

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 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 Community for Help

Sergiu.Daniluk
VIP Advisor

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

Create
Recognize Your Peers
Content for Community-Ad