来自 ncclient 导入管理器;无法调用 close_session()

Posted

技术标签:

【中文标题】来自 ncclient 导入管理器;无法调用 close_session()【英文标题】:from ncclient import manager ; unable to call close_session() 【发布时间】:2020-10-25 08:43:27 【问题描述】:

我正在尝试执行以下代码,但出现“close_session()”函数的这些错误。如何解决?我是 python 新手。

我正在学习 netconf 并尝试使用 python netconf 配置路由器。我已经导入了库,看来我做的一切都正确。

from ncclient import manager
from pprint import pprint
import xmltodict
import xml.dom.minidom
from router_info import router



print(router["host"])
print(router["port"])
print(router["username"])
print(router["password"])


netconf_filter = open("/Users/ljames/netconf/netconf-filter.xsd").read()

with manager.connect(host=router["host"], port=router["port"], username=router["username"], password=router["password"], hostkey_verify=False) as m:
   
      interface_netconf = m.get(netconf_filter)
     #   xmlDom = xml.dom.minidom.parseString(str(interface_netconf))
     #   print(xmlDom.toprettyxml(indent=''))
     #   print('*' * 25 + 'Break' + '*' * 25)

interface_python = xmltodict.parse(interface_netconf.xml)["rpc-reply"]["data"]
# pprint(interface_python)
name = interface_python['interfaces']['interface']['name']['#text']
print(name)

config = interface_python["interfaces"]["interface"]
op_state = interface_python["interfaces-state"]["interface"]

print("Start")
print(f"Name: config['name']['#text']")
print(f"Description: config['description']")
print(f"packets In op_state['statistics']['in-unicast-pkts']")
m.close_session() 

以下是错误信息:

mac@user % /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 /Users/ljames/netconf/netconf-5.py
ios-xe-mgmt-latest.cisco.com
10000
developer
C1sco12345
GigabitEthernet2
Start
Name: GigabitEthernet2
Description: YJ-interface
packets In 146
Traceback (most recent call last):
  File "/Users/ljames/netconf/netconf-5.py", line 36, in <module>
    m.close_session() 
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ncclient/manager.py", line 231, in execute
    return cls(self._session,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ncclient/operations/session.py", line 28, in request
    return self._request(new_ele("close-session"))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ncclient/operations/rpc.py", line 327, in _request
    self._session.send(req)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/ncclient/transport/session.py", line 167, in send
    raise TransportError('Not connected to NETCONF server')
ncclient.transport.errors.TransportError: Not connected to NETCONF server

【问题讨论】:

仅供参考,您的密码在帖子中 【参考方案1】:

我遇到了完全相同的问题,这是我的结论:

问题可能是您在 with 块之外使用了 m.close_session()。事实上,你根本不应该在with 块内使用close_session,因为清理是自动执行的。只需要逐行调试,看到退出with块时,m.connected设置为false。

【讨论】:

【参考方案2】:

检查您的 ncclient 管理器的主机和密码

【讨论】:

请详细说明。

以上是关于来自 ncclient 导入管理器;无法调用 close_session()的主要内容,如果未能解决你的问题,请参考以下文章

NetDevOps — ncclient

windows系统中的c/c++设计——cl与link的进阶(生成静态库并调用)

Python模块之ncclient

SSIS Excel 连接管理器无法连接到源

无法识别的选择器发送到来自 Facebook SDK 的类

无法调用 Tomcat 管理器:管道损坏