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

ncclient connection not working on CSRv router

nwekechampion
Level 3
Level 3

Hi guys,

 

I get the below error when I try to make a connection to my CSRv router.

Traceback (most recent call last):
File "C:\Windows\system32\Netconf_env\Scripts\mdp_home_test\get_interface_list_home.py", line 56, in <module>
netconf_reply = m.get_config(source = 'running', filter = netconf_filter)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Windows\system32\Netconf_env\Lib\site-packages\ncclient\manager.py", line 212, in execute
return cls(self._session,
^^^^^^^^^^^^^^^^^^
File "C:\Windows\system32\Netconf_env\Lib\site-packages\ncclient\operations\retrieve.py", line 166, in request
return self._request(node)
^^^^^^^^^^^^^^^^^^^
File "C:\Windows\system32\Netconf_env\Lib\site-packages\ncclient\operations\rpc.py", line 341, in _request
raise self._reply.error
ncclient.operations.rpc.RPCError: {'type': 'protocol', 'tag': 'unknown-element', 'severity': 'error', 'info': '<?xml version="1.0" encoding="UTF-8"?><error-info xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><bad-element>filter</bad-element>\n</error-info>\n', 'path': '\n /rpc/get-config\n ', 'message': None}

 

Here is my code below:

#!/usr/bin/env python



import os
import sys
from ncclient import manager
import xmltodict
import xml.dom.minidom
from pprint import pprint


# Create an XML filter for targeted NETCONF queries
netconf_filter = """ <filter>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
        <interface/>
      </interfaces>
    </filter>
"""



# Open a connection to the network device using ncclient
with manager.connect(
        device_params={'name':'csr'},
        host= "192.168.0.124",
        port= 830,
        username= "admin",
        password= "Test123",
        hostkey_verify=False
        ) as m:
   
    print("Sending a <get-config> operation to the device.\n")
    # Make a NETCONF <get-config> query using the filter
    netconf_reply = m.get_config(source = 'running', filter = netconf_filter)

print("Here is the raw XML data returned from the device.\n")
# Print out the raw XML that returned
print(xml.dom.minidom.parseString(netconf_reply.xml).toprettyxml())
print("")

# Parse the returned XML to an Ordered Dictionary
netconf_data = xmltodict.parse(netconf_reply.xml)["rpc-reply"]["data"]
pprint(netconf_data)

interfaces = netconf_data["interfaces"]["interface"]

pprint(interfaces)

for interface in interfaces:
    print("Interface {} enabled status is {}".format(
            interface["name"],
            interface["enabled"]
            )
        )
print("\n")
1 Reply 1

Marcel Zehnder
Spotlight
Spotlight

Remove the filter tags from your payload and explicit define subtree filtering in the get_config method:

 

netconf_filter = """
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface/>
</interfaces>
"""

# Open a connection to the network device using ncclient
with manager.connect(
device_params={'name':'csr'},
host= "192.168.0.124",
port= 830,
username= "admin",
password= "Test123",
hostkey_verify=False
) as m:

  print("Sending a <get-config> operation to the device.\n")
  # Make a NETCONF <get-config> query using the filter
  netconf_reply = m.get_config(source = 'running', filter=("subtree", netconf_filter)