简体   繁体   English

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

[英]from ncclient import manager ; unable to call close_session()

I am trying to execute following code but I am getting these errors for "close_session()" function.我正在尝试执行以下代码,但出现“close_session()”function 的这些错误。 how to fix it?如何解决? I am new to python.我是 python 的新手。

I am learning netconf and trying to configure router using python netconf.我正在学习 netconf 并尝试使用 python netconf 配置路由器。 I have imported library and it seems I have done everything correctly.我已经导入了库,看来我已经正确完成了所有操作。

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

Following is the error msg:以下是错误消息:

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

I had the exact same issue and this is my conclusion:我有完全相同的问题,这是我的结论:

The issue could be that you use m.close_session() outside of with block.问题可能是您在with块之外使用m.close_session() In fact, you should not use close_session at all within with block, since cleanup is performed automatically.事实上,你根本不应该在with块中使用close_session ,因为清理是自动执行的。 Just debug line by line and see that when you exit with block, m.connected is set to false.只需逐行调试,看看当你with块退出时, m.connected设置为 false。

check your host and password for ncclient manager检查您的 ncclient 管理器的主机和密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM