C/C++教程

华为网络自动化编程 之 NETCONF配置实验

本文主要是介绍华为网络自动化编程 之 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配置实验

这篇关于华为网络自动化编程 之 NETCONF配置实验的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!