其他分享
首页 > 其他分享> > NETCONF下发网络设备配置:netconf_switch

NETCONF下发网络设备配置:netconf_switch

作者:互联网


#!/usr/bin # _*_ coding: UTF-8 _*_ # Copyright (c) 2021 GengYu.All rights reserved # @Create by gengyu # @Create Time :2021/12/12 # @File Name : netconf_switch # 打包命令 pyinstaller -F package\netconf_switch """ """ __author__ = 'Administrator' from ncclient import manager import doctest import paramiko import time ip = '192.168.56.177' ssh_user = 'python' ssh_pwd = 'Huawei@123' netconf_port = '830' netconf_user = 'netconf' netconf_pwd = 'Huawei@123' filename = 'netconf.txt' #定义类ssh ,用于配置设备netconf class ssh: def ssh_connect(self,ip,username,pwd): ssh = paramiko.client.SSHClient() ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy()) ssh.connect(hostname=ip,port=22,username=username,password=pwd) print(ip + 'login successfully') return ssh def ssh_config(self,file,ip,username,pwd): server = ssh.ssh_connect(ip,username,pwd) cli = server.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 line in config_list: cli.send(line) time.sleep(0.5) dis_this = cli.recv(999999).decode() print(dis_this) server.close() #定义函数netconf_connect,用于建立netconf连接 def netconf_connect(host, port, user, pwd): return manager.connect(host=host, port=port, username=user, password=pwd, hostkey_verify = False, device_params={'name': "huawei"}, allow_agent = False, look_for_keys = False) # netconf 发送XML数据,配置设备接口IP地址 CREATE_INTERFACE = r''' <config> <ethernet xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> <ethernetIfs> <ethernetIf operation="merge"> <ifName>GE1/0/2</ifName> <l2Enable>disable</l2Enable> </ethernetIf> </ethernetIfs> </ethernet> <ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> <interfaces> <interface operation="merge"> <ifName>GE1/0/2</ifName> <ifDescr>Config by NETCONF</ifDescr> <ifmAm4> <am4CfgAddrs> <am4CfgAddr operation="create"> <subnetMask>255.255.255.0</subnetMask> <addrType>main</addrType> <ifIpAddr>192.168.2.1</ifIpAddr> </am4CfgAddr> </am4CfgAddrs> </ifmAm4> </interface> </interfaces> </ifm> </config>''' if __name__ == "__main__": doctest.testmod() ssh = ssh() ssh.ssh_config(filename,ip,ssh_user,ssh_pwd) time.sleep(5) m = netconf_connect(ip,netconf_port,netconf_user,netconf_pwd) m.edit_config(target='running',config=CREATE_INTERFACE)

netconf.txt

sys
aaa
local-user netconf password irreversible-cipher Huawei@123
local-user netconf service-type ssh
local-user netconf level 3
q
ssh user netconf authentication-type password
ssh user netconf service-type snetconf
snetconf server enable
netconf
protocol inbound ssh port 830
commit
quit

  

交换机配置:

sysname HUAWEI

aaa

local-user python password irreversible-cipher Huawei@123
local-user python service-type ssh
local-user python user-group manage-ug

interface Vlanif1
ip address 192.168.56.177 255.255.255.0

stelnet server enable

ssh user python
ssh user python authentication-type password
ssh user python service-type stelnet
ssh authorization-type default aaa

user-interface vty 4
authentication-mode aaa
user privilege level 3
protocol inbound ssh

 

标签:NETCONF,netconf,ip,pwd,connect,switch,ssh,user
来源: https://www.cnblogs.com/gengyazhao/p/15679965.html