Reputation: 11
My Python script is terminating my netconf session before returning my XML rpc request I'm passing to it. My XML rpc works when I connect directly to my router's Netconf session. Can someone please help me figure this out? I don't want to use the NCCLIENT library, I would rather open the socket to the Netconf API directly.
Router = 5.3.4 XRv Python = 2.7
Python Code:
import paramiko
import socket
import time
import sys
ROUTER_IP = 'x.x.x.x'
USERNAME = 'adrian'
PASSWORD = 'xxxxxx'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
Send_XML = """
<?xml version="1.0" encoding="UTF-8"?>
<rpc message-id="106" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<get-config>
<source>
<running/>
</source>
<filter>
<Configuration>
<InterfaceConfigurationTable/>
</Configuration>
</filter>
</get-config>
</rpc>
]]>]]>"""
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((ROUTER_IP, 830))
trans = paramiko.Transport(socket)
trans.connect(username=USERNAME, password=PASSWORD)
# CREATE CHANNEL FOR DATA COMM
ch = trans.open_session()
name = ch.set_name('netconf')
# Invoke NETCONF
ch.invoke_subsystem('netconf echo format')
# SEND COMMAND
ch.send(Send_XML)
# Recieve data returned
data = ch.recv(2048)
while data:
data = ch.recv(1024)
print data,
if data.find('</rpc-reply>') == 0:
# We have reached the end of reply
print "END!!"
break
ch.close()
trans.close()
socket.close()
Output
C:\Python27\python.exe "C:/Users/adrian/OneDrive/Python/DevNet/XR NCClient.py"
g/Cisco-IOS-XR-ha-eem-cfg?module=Cisco-IOS-XR-ha-eem-cfg&revision=2013-07-22</capability>
<capability>http://cisco.com/ns/yang/Cisco-IOS-XR-ha-eem-oper?
.
.
.
<capability>urn:ietf:params:xml:ns:yang:ietf-yang-types?module=ietf-yang-types&revision=2013-07-15</capability>
</capabilities>
<session-id>21415</session-id>
</hello>
]]>]]>
Process finished with exit code 0
Direct Connect
ssh x.x.x.x -p 830 netconf echo format
<?xml version="1.0" encoding="UTF-8"?>
<rpc message-id="106" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<get-config>
<source>
<running/>
</source>
<filter>
<Configuration>
<InterfaceConfigurationTable/>
</Configuration>
</filter>
</get-config>
</rpc>
]]>]]>
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply message-id="106" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<data>
<Configuration>
<InterfaceConfigurationTable MajorVersion="5" MinorVersion="4">
<InterfaceConfiguration>
<Naming>
.
.
.
</InterfaceConfigurationTable>
</Configuration>
</data>
</rpc-reply>
]]>]]>
Upvotes: 1
Views: 1840
Reputation: 802
ncclinet python library could give you a a bit higher level of abstraction See https://pypi.python.org/pypi/ncclient
Upvotes: 0
Reputation: 31
Based on the output from the python script, your router is attempting to complete the process when you are sending the RPC. For example, here is what the opening of a session looks like with SSH.
$ ssh [email protected] -p 830 -s netconf
[email protected]'s password:
The router sends this:
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.1</capability>
<capability>urn:ietf:params:netconf:capability:candidate:1.0</capability>
<capability>urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring</capability>
<capability>urn:ietf:params:xml:ns:yang:ietf-interfaces</capability>
[output omitted and edited for clarity]
</capabilities>
<session-id>19150</session-id></hello>]]>]]>
Then you need to reply with this
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<capabilities>
<capability>urn:ietf:params:netconf:base:1.0</capability>
</capabilities>
</hello>]]>]]>
You would need to send the back to the router before moving onto your RPC.
All that said, I would HIGHLY suggest using ncclient as it handles the entire hello process, as well as sending and recieving the RPCs on your behalf. What you are currently doing is just replicating the code in that library, but missing a lot of the syntax and verificaitons that ncclient includes.
Upvotes: 3