12-03-2021 11:16 PM
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.
12-04-2021 01:09 AM
have you enabled the interface on the nexus OS side?
please look at the good steps :
12-05-2021 09:20 PM
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
12-06-2021 08:48 AM
as long as port interface enable to listen the request, it should work.,
@Sergiu.Daniluk also suggested some tips...cheers!
12-04-2021 04:07 AM - edited 12-05-2021 10:24 PM
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
12-05-2021 09:11 PM
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
12-05-2021 10:30 PM
Try sending the get to "restconf/data/openconfig-interfaces:interfaces"
Cheers,
Sergiu
05-07-2023 10:33 PM - edited 05-07-2023 10:35 PM
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:
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide