Netconf连接终止于Python脚本

时间:2017-01-06 03:25:40

标签: python

我的Python脚本在返回我传递给它的XML rpc请求之前终止了我的netconf会话。当我直接连接到路由器的Netconf会话时,我的XML rpc工作。有人可以帮我解决这个问题吗?我不想使用NCCLIENT库,我宁愿直接打开Netconf API的套接字。

路由器= 5.3.4 XRv Python = 2.7

Python代码:

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()

输出

    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&amp;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&amp;revision=2013-07-15</capability>
 </capabilities>
 <session-id>21415</session-id>
</hello>
]]>]]> 

Process finished with exit code 0

直接连接

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>
]]>]]>

2 个答案:

答案 0 :(得分:3)

根据python脚本的输出,您的路由器在发送RPC时尝试完成该过程。例如,以下是使用SSH查看会话的内容。

$ ssh admin@192.168.0.1 -p 830 -s netconf
admin@192.168.0.1's password:

路由器发送此信息:

<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>]]>]]>

然后你需要用这个回复

<?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>]]>]]>

在移动到RPC之前,您需要将其发送回路由器。

所有这一切,我强烈建议使用ncclient,因为它处理整个hello进程,以及代表您发送和接收RPC。你目前正在做的只是复制该库中的代码,但缺少ncclient包含的许多语法和验证。

答案 1 :(得分:0)

ncclinet python库可以为您提供更高级别的抽象请参阅https://pypi.python.org/pypi/ncclient