使用python进行netconf访问

2024-10-11 lqy python

from ncclient import manager
from ncclient.xml_ import to_ele

# 设备信息
hostname = '10.98.249.12'
username = 'huawei'
password = 'Admin@123'

# NETCONF连接
get_xml="""
  <edit-config>
    <target>
      <running/>
    </target>
    <default-operation>merge</default-operation>
    <error-option>rollback-on-error</error-option>
    <config>
      <ifm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <interfaces>
          <interface>
            <ifName>Ethernet1/0/5</ifName>
            <ifmAm4>
              <am4CfgAddrs>
                <am4CfgAddr operation="merge">
                  <ifIpAddr>192.168.15.1</ifIpAddr>
                  <subnetMask>255.255.255.0</subnetMask>
                  <addrType>main</addrType>
                </am4CfgAddr>
              </am4CfgAddrs>
            </ifmAm4>
          </interface>
          <interface>
            <ifName>LoopBack0</ifName>
            <ifmAm4>
              <am4CfgAddrs>
                <am4CfgAddr operation="merge">
                  <ifIpAddr>1.1.1.1</ifIpAddr>
                  <subnetMask>255.255.255.255</subnetMask>
                  <addrType>main</addrType>
                </am4CfgAddr>
              </am4CfgAddrs>
            </ifmAm4>
          </interface>
        </interfaces>
      </ifm>
      <staticrt xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <staticrtbase>
          <srRoutes>
            <srRoute operation="merge">
              <vrfName>_public_</vrfName>
              <afType>ipv4unicast</afType>
              <topologyName>base</topologyName>
              <prefix>10.5.0.0</prefix>
              <maskLength>24</maskLength>
              <ifName>Ethernet1/0/5</ifName>
              <destVrfName>_public_</destVrfName>
              <nexthop>192.168.15.2</nexthop>
            </srRoute>
          </srRoutes>
        </staticrtbase>
      </staticrt>
      <ospfv2 xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
        <ospfv2comm>
          <ospfSites>
            <ospfSite operation="merge">
              <processId>1</processId>
              <routerId>1.1.1.1</routerId>
              <vrfName>_public_</vrfName>
            </ospfSite>
          </ospfSites>
        </ospfv2comm>
      </ospfv2>
    </config>
  </edit-config>
"""
m=manager.connect(host=hostname,
                  username=username,
                  password=password,
                  port=830,
                  hostkey_verify=False,
                  device_params={'name': 'huawei'}
                  )
get_xml = to_ele(get_xml)
reply=m.rpc(get_xml)
print(reply)

标签: python

发表评论:

Powered by emlog