ncclient: Unable to send an RPC

I was trying to send an RPC to the NETCONF server using one of the examples that you provide. Here’s my code:

#!/usr/bin/env python

from ncclient import manager

def connect(host, port, user, password, source):
    print('establishing connection...')
    conn=manager.connect(host=host,
                         port=port,
                         username=user,
                         password=password,
                         timeout=10,
                         hostkey_verify=False,
                         device_params={'name':'default'})
    try:
        print("connection has been established")
        print("server capabilities:")

        for c in conn.server_capabilities:
            print(c)

        rpc = """
        <get-chassis-inventory>
            <detail/>
        </get-chassis-inventory>"""

        print('sending an RPC')
        result = conn.rpc(rpc)
        print(result)
    except Exception as rpcException:
        print("An exception occurred while sending an RPC: " + str(rpcException))
    finally:
        print("closing connection...")
        try:
            conn.close_session()
        except Exception as sessionException:
            print("An exception occurred while closing the session :" + str(sessionException))
        print("connection has been closed")


if __name__ == '__main__':
    connect('localhost', 1830, 'user', 'pass', 'candidate')

Calling conn.rpc(rpc) throws the following exception:

Invalid tag name u'\n <get-chassis-inventory>\n <detail/>\n </get-chassis-inventory>'

which doesn’t make a lot of sense. Am I doing something wrong? From the exception message it’s very unclear what I’m supposed to do.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16

Most upvoted comments

Ah found it.

from ncclient.xml_ import to_ele
...
conn.dispatch(to_ele("<my-rpc><string/></my-rpc>"))