华为网络自动化编程 之 NETCONF配置实验
2021/6/6 20:26:18
本文主要是介绍华为网络自动化编程 之 NETCONF配置实验,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
环境
在华为网络自动化编程 之 SSH 连接交换机基础上继续操作
//netconf.txt 通过代码进行SSH配置NETCONF system-view immediately aaa local-user netconf password irreversible-cipher Huawei12#$ local-user netconf service-type ssh local-user netconf level 3 quit ssh user netconf authentication-type password ssh user netconf service-type snetconf snetconf server enable netconf protocol inbound ssh port 830 quit
逻辑
1.SSH登录设备,配置NETCONF over SSH
2.通过NETCONF配置交换机端口
代码
from ncclient import manager from ncclient import operations import paramiko import time # 设备参数 ip = '172.16.1.3' 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.AutoAddPolicy()) ssh.connect(hostname=ip, port=22, username=username, password=password) print(ip+'login successfully') return ssh def ssh_config(filename, 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(filename, 'r') config_list = f.readlines() f.close() 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 = ''' <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__': ssh.ssh_config(filename, ip, ssh_user, ssh_password) m = huawei_connect(ip, netconf_port, netconf_user, netconf_password) m.edit_config(target='running', config=CREATE_INTERFACE)
查看交换机接口配置
这篇关于华为网络自动化编程 之 NETCONF配置实验的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain
- 2024-06-19EntBot.ai: AI Website Chatbot for Product Guides and Development Doc
- 2024-06-17zero-shot-learning-definition-examples-comparison
- 2024-06-06Package Easy(基于 NSIS 的打包exe安装包工具)使用方法-icode9专业技术文章分享
- 2024-06-06基于 casdoor 的 ELK 开源登录认证解决方案: elk-auth-casdoor-icode9专业技术文章分享
- 2024-05-29Elasticsearch慢查询日志配置