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

How to ping via NETCONF on Cisco NCS540 using ncclient python library

Trying to ping do a simple ping ont a Cisco NCS540 IOSXR 7.6.2 , eventually pings from vrf as well, but for now I want to get this working.

 

I'm using the dispatch operation. Is there a argument i need to put beside xml_data object ??

 

xml_commands_send = connection.dispatch(send_xml_data)

 See the code below:

  

send_xml_data = '''
    <input>
        <destination>8.8.8.8</destination>
        <repeat-count>5</repeat-count>
        <interval>1</interval>
        <source>Loopback0</source>
    </input>
</ping>
'''
also tried this 
send_xml_data = '''
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
        <input>
            <destination>8.8.8.8</destination>
            <repeat-count>5</repeat-count>
            <interval>1</interval>
            <source>Loopback0</source>
        </input>
    </ping>
</rpc>
'''
 
>>> send_xml_data = '''
... <ping xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">
... <input>
... <destination>8.8.8.8</destination>
... <repeat-count>5</repeat-count>
... <interval>1</interval>
... <source>Loopback0</source>
... </input>
... </ping>
... '''
>>>
>>> xml_commands_send = connection.dispatch(send_xml_data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\manager.py", line 257, in execute
return cls(self._session,
^^^^^^^^^^^^^^^^^^
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\operations\retrieve.py", line 234, in request
node = new_ele(rpc_command)
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\xml_.py", line 292, in <lambda>
new_ele = lambda tag, attrs={}, **extra: etree.Element(qualify(tag), attrs, **extra)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src\lxml\etree.pyx", line 3049, in lxml.etree.Element
File "src\lxml\apihelpers.pxi", line 101, in lxml.etree._makeElement
File "src\lxml\apihelpers.pxi", line 1754, in lxml.etree._tagValidOrRaise
ValueError: Invalid tag name '\n<ping xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">\n <input>\n <destination>8.8.8.8</destination>\n <repeat-count>5</repeat-count>\n <interval>1</interval>\n <source>Loopback0</source>\n </input>\n</ping>\n'
>>> connections.connected
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'connections' is not defined. Did you mean: 'connection'?
>>> connection.connected
True
>>>
>>>
>>> xml_commands_send = connection.dispatch(send_xml_data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\manager.py", line 257, in execute
return cls(self._session,
^^^^^^^^^^^^^^^^^^
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\operations\retrieve.py", line 234, in request
node = new_ele(rpc_command)
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\xml_.py", line 292, in <lambda>
new_ele = lambda tag, attrs={}, **extra: etree.Element(qualify(tag), attrs, **extra)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src\lxml\etree.pyx", line 3049, in lxml.etree.Element
File "src\lxml\apihelpers.pxi", line 101, in lxml.etree._makeElement
File "src\lxml\apihelpers.pxi", line 1754, in lxml.etree._tagValidOrRaise
ValueError: Invalid tag name '\n<ping xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">\n <input>\n <destination>8.8.8.8</destination>\n <repeat-count>5</repeat-count>\n <interval>1</interval>\n <source>Loopback0</source>\n </input>\n</ping>\n'
>>>
 
 
2 Accepted Solutions

Accepted Solutions

Marcel Zehnder
Spotlight
Spotlight
from ncclient import manager, xml_

send_xml_data = '''
  <ping xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">
    <destination>
      <destination>8.8.8.8</destination>
      <repeat-count>5</repeat-count>
      <interval>1</interval>
      <source>Loopback0</source>
    </destination>
  </ping>'''

xml_commands_send = connection.rpc(xml_.to_ele(send_xml_data))

View solution in original post

GOT IT WORKING !!!!  SO HAPPY  thanks a million that was the issue 

send_xml_data = '''
    <destination>
        <destination>1.2.3.4</destination>
    </destination>
</ping>
'''
>>> xml_commands_send
<?xml version="1.0"?>
<rpc-reply message-id="urn:uuid:2d9b1e40-afed-49a9-96fd-78edc6fba0d7" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<ping-response xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">
<ipv4>
<destination>1.2.3.4</destination>
<repeat-count>5</repeat-count>
<data-size>100</data-size>
<timeout>2</timeout>
<pattern>abcd</pattern>
<rotate-pattern>false</rotate-pattern>
<replies>
<reply>
<reply-index>1</reply-index>
<result>U</result>
</reply>
<reply>
<reply-index>2</reply-index>
<result>U</result>
</reply>
<reply>
<reply-index>3</reply-index>
<result>U</result>
</reply>
<reply>
<reply-index>4</reply-index>
<result>U</result>
</reply>
<reply>
<reply-index>5</reply-index>
<result>U</result>
</reply>
</replies>
<hits>0</hits>
<total>5</total>
<success-rate>0</success-rate>
</ipv4>
</ping-response>
</rpc-reply>
 

View solution in original post

7 Replies 7

I also tried this  according to the instructions from the link below

LTRSDN-2260 (ciscolive.com)

xml_commands_send = connection.dispatch(send_xml_data, source=None, filter=None)  
 

Marcel Zehnder
Spotlight
Spotlight

Hi

Is  the following working?

 

xml_commands_send = connection.rpc(xml_.to_ele(send_xml_data))

 

You need to import xml_ from ncclient:

 

from ncclient import manager, xml_

 

 

tried  it. it see the output  below

 

>>> from ncclient import manager, xml_
>>>
>>> send_xml_data = '''
... <ping xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">
... <input>
... <destination>8.8.8.8</destination>
... <repeat-count>5</repeat-count>
... <interval>1</interval>
... <source>Loopback0</source>
... </input>
... </ping>
... '''
>>>
>>> xml_commands_send = connection.rpc(xml_.to_ele(send_xml_data))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\manager.py", line 257, in execute
return cls(self._session,
^^^^^^^^^^^^^^^^^^
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\operations\rpc.py", line 521, in request
return self._request(node)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\operations\rpc.py", line 375, in _request
raise self._reply.error
ncclient.operations.rpc.RPCError: <input> tag is not required.
>>>
>>> send_xml_data = '''
... <rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
... <ping xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">
... <input>
... <destination>8.8.8.8</destination>
... <repeat-count>5</repeat-count>
... <interval>1</interval>
... <source>Loopback0</source>
... </input>
... </ping>
... </rpc>
... '''
>>> xml_commands_send = connection.rpc(xml_.to_ele(send_xml_data))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\manager.py", line 257, in execute
return cls(self._session,
^^^^^^^^^^^^^^^^^^
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\operations\rpc.py", line 521, in request
return self._request(node)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\dgutierrez\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\ncclient\operations\rpc.py", line 375, in _request
raise self._reply.error
ncclient.operations.rpc.RPCError: {'type': 'protocol', 'tag': 'unknown-namespace', 'app_tag': None, '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">\n <bad-element>rpc</bad-element>\n <bad-namespace>urn:ietf:params:xml:ns:netconf:base:1.0</bad-namespace>\n </error-info>\n ', 'path': None, 'message': None}
>>>

Marcel Zehnder
Spotlight
Spotlight

I need to check the yang module for the payload. I let you know asap.

Marcel Zehnder
Spotlight
Spotlight
from ncclient import manager, xml_

send_xml_data = '''
  <ping xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">
    <destination>
      <destination>8.8.8.8</destination>
      <repeat-count>5</repeat-count>
      <interval>1</interval>
      <source>Loopback0</source>
    </destination>
  </ping>'''

xml_commands_send = connection.rpc(xml_.to_ele(send_xml_data))

GOT IT WORKING !!!!  SO HAPPY  thanks a million that was the issue 

send_xml_data = '''
    <destination>
        <destination>1.2.3.4</destination>
    </destination>
</ping>
'''
>>> xml_commands_send
<?xml version="1.0"?>
<rpc-reply message-id="urn:uuid:2d9b1e40-afed-49a9-96fd-78edc6fba0d7" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<ping-response xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ping-act">
<ipv4>
<destination>1.2.3.4</destination>
<repeat-count>5</repeat-count>
<data-size>100</data-size>
<timeout>2</timeout>
<pattern>abcd</pattern>
<rotate-pattern>false</rotate-pattern>
<replies>
<reply>
<reply-index>1</reply-index>
<result>U</result>
</reply>
<reply>
<reply-index>2</reply-index>
<result>U</result>
</reply>
<reply>
<reply-index>3</reply-index>
<result>U</result>
</reply>
<reply>
<reply-index>4</reply-index>
<result>U</result>
</reply>
<reply>
<reply-index>5</reply-index>
<result>U</result>
</reply>
</replies>
<hits>0</hits>
<total>5</total>
<success-rate>0</success-rate>
</ipv4>
</ping-response>
</rpc-reply>
 

No problem, happy I could help. However, it would be nice to mark my answer as the accepted solution and not your own post