ncclient.transport.errors.SessionCloseError报错问题
import time
import paramiko
from ncclient import manager
from ncclient import operations
from ncclient.manager import Manager
设备参数
ip = ‘192.168.56.100’
ssh_user = ‘python’
ssh_password = ‘Huawei12#$’
netconf_port = ‘830’
netconf_user = ‘netconf’
netconf_password = ‘Huawei12#$’
filename = ‘netconf.txt’
定义类ssh,用于配置设备NETCONF
class ssh():
def ssh_connect(ip, username, password):
ssh = paramiko.client.SSHClient()
ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
ssh.connect(hostname=ip, port=22, username=username, password=password)
print(ip + ‘ login successfully’)
return ssh
def ssh_config(file, ip, username, password):
a = ssh.ssh_connect(ip, username, password)
cli = a.invoke_shell()
cli.send('N\n')
time.sleep(0.5)
cli.send('screen-length 0 temporary\n')
time.sleep(0.5)
f = open(file, 'r')
config_list = f.readlines()
for i in config_list:
cli.send(i)
time.sleep(0.5)
dis_this = cli.recv(999999).decode()
print(dis_this)
a.close()
定义函数huawei_connect,用于建立NETCONF连接
def huawei_connect(host, port, user, password):
return manager.connect(host=host,
port=port,
username=user,
password=password,
hostkey_verify=False,
device_params={‘name’: ‘huawei’},
allow_agent=False,
look_for_keys=False)
NETCONF发送XML数据,配置设备接口IP地址
CREATE_INTERFACE = ‘’’
GE1/0/2
disable
GE1/0/2
Config by NETCONF
255.255.255.0
main
192.168.2.1
‘’’
主函数,顺序执行
if name == ‘main‘:
ssh.ssh_config(filename, ip, ssh_user, ssh_password)
time.sleep(5)
m: Manager = huawei_connect(ip, netconf_port, netconf_user, netconf_password)
m.edit_config(target=’running’, config=CREATE_INTERFACE)
问题:近期在学习华为网络自动化编程netconf使用。遇到ncclient.transport.errors.SessionCloseError: Unexpected session close报错问题,请好心人帮忙解答下关于引起该问题原因及解决方法?
分析:问题出在调用m.edit_config(target=’running’,config=CREATE_INTERFACE)语句时会话意外中断。
我查了ncclient用法,在ncclient中错误有定义该问题,但是没说解决方案,故此求教,非常感谢好心人的悉心指导,谢谢
推荐文章: