使用netconf配置交换机_JUST DO IT的技术博客_51CTO博客_netconf协议
声明:本文参考了以下连接,若有侵权 请告知删除,谢谢!
一文读懂网管协议 - SNMP,NETCONF,RESTCONF - 来份锅包肉 - 博客园
数据模型与网络自动化 - 来份锅包肉 - 博客园
08-NETCONF配置-新华三集团-H3C
https://github.com/ncclient
IP新技术-NETCONF协议 | 网英的日常
https://medium.com/@k.okasha/network-automation-and-the-rise-of-netconf-e96cc33fe28html
1.什么是netconf?python
NETCONF(Network Configuration Protocol,网络配置协议)是一种基于XML的网络管理协议,它提供了一种可编程的、对网络设备进行配置和管理的方法。用户能够经过该协议设置参数、获取参数值、获取统计信息等。
NETCONF报文使用XML格式,具备强大的过滤能力,并且每个数据项都有一个固定的元素名称和位置,这使得同一厂商的不一样设备具备相同的访问方式和结果呈现方式,不一样厂商之间的设备也能够通过映射XML获得相同的效果,这使得它在第三方软件的开发上很是便利,很容易开发出在混合不一样厂商、不一样设备的环境下的特殊定制的网管软件。在这样的网管软件的协助下,使用NETCONF功能会使网络设备的配置管理工做,变得更简单更高效git
特色:
a.基于 RPC,增长了事务支持
b.优化查询功能,增长过滤查询方式
c.拓展性强,在其协议内部分为 4 层,各层之间相互独立
d.更好的将配置和状态数据解耦,并区分状态数据(candidate, running, startup)
e.易使用,结合提供的 API,实现可编程性的网络操做
f.安全性更好,在传输层可选用 SSH,TLS 协议等。
NETCONF 交互:

对于 Manager 和 Agent 来讲,Session 创建会经历以下的过程:github
a.Manager 请求 NETCONF 中 SSH 子系统创建链接。
b.Agent 回复 Hello 消息,包含自己支持的特性和能力。
c.Manager 告知 Agent 本身所支持的特性和能力。
d.Manager 开始发送 RPC 操做请求。
e.Agent 回复 RPC 请求操做结果。
2.netconf有什么用?编程
a.配置自动化下发时的校验,xml 是基于yang模型约束的,设备会基于[yang模型](https://www.cnblogs.com/michael9/p/14481135.html)校验xml配置是否合法。
b.提供网络配置的接口,更利于开发自动化工具或平台。
3.怎么使用netconf
json
a.实验环境搭建:安全
H3C_Comware7
Python3.7
ncclient-0.6.10
Win10
b.H3C netconf xml API网络
H3C《NETCONF XML API》 下载
连接:https://pan.baidu.com/s/16qrstxFk0YBGLIlkKI5eDg
提取码:ol9a
若是连接失效,请底部留言,笔者会不定时进行查看。
c.交换机开启ssh 和 netconfsession
#
local-user admin class manage
password simple admin
service-type ssh
authorization-attribute user-role network-admin
authorization-attribute user-role network-operator
#
line vty 0 14
authentication-mode scheme
user-role network-operator
#
netconf ssh server enable
netconf ssh server port 830
#
d.代码调用ssh
from ncclient import manager
hostname = '172.16.1.100'
netconf_port = 830
username = 'admin'
password = 'admin'
vendor = 'h3c'
# 实例化一个netconf链接
manager_connect = manager.connect(
host=hostname,
port=netconf_port,
username=username,
password=password,
hostkey_verify=False,
device_params={'name': vendor},
allow_agent=False,
look_for_keys=False
)
#获取交换机的接口和MAC表项
request_xml = '''
<top xmlns="http://www.h3c.com/netconf/data:1.0">
<Ifmgr>
<Interfaces>
<Interface>
<PortIndex></PortIndex>
<Name></Name>
</Interface>
</Interfaces>
</Ifmgr>
<MAC>
<MacUnicastTable>
<Unicast>
<VLANID></VLANID>
<MacAddress></MacAddress>
<PortIndex></PortIndex>
<NickName></NickName>
<Status></Status>
<Aging></Aging>
</Unicast>
</MacUnicastTable>
</MAC>
</top>
'''
get_mac = manager_connect.get(filter=('subtree', request_xml))
manager_connect.close_session()
print(get_mac)
############################################
# 返回结果 是一行字符,下面是我格式化过了的 #
# 能够经过python的xml库能够将xml转化为json #
############################################
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply
xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:ec18629a-5e7d-4640-89da-eed5b896f325">
<data>
<top
xmlns="http://www.h3c.com/netconf/data:1.0">
<Ifmgr>
<Interfaces>
<Interface>
<IfIndex>1</IfIndex>
<Name>GigabitEthernet1/0/1</Name>
<PortIndex>1</PortIndex>
</Interface>
<Interface>
<IfIndex>2</IfIndex>
<Name>GigabitEthernet1/0/2</Name>
<PortIndex>2</PortIndex>
</Interface>
<Interface>
<IfIndex>3</IfIndex>
<Name>GigabitEthernet1/0/3</Name>
<PortIndex>3</PortIndex>
</Interface>
<Interface>
<IfIndex>4</IfIndex>
<Name>GigabitEthernet1/0/4</Name>
<PortIndex>4</PortIndex>
</Interface>
<Interface>
<IfIndex>5</IfIndex>
<Name>GigabitEthernet1/0/5</Name>
<PortIndex>5</PortIndex>
</Interface>
<Interface>
<IfIndex>6</IfIndex>
<Name>GigabitEthernet1/0/6</Name>
<PortIndex>6</PortIndex>
</Interface>
<Interface>
<IfIndex>7</IfIndex>
<Name>GigabitEthernet1/0/7</Name>
<PortIndex>7</PortIndex>
</Interface>
<Interface>
<IfIndex>8</IfIndex>
<Name>GigabitEthernet1/0/8</Name>
<PortIndex>8</PortIndex>
</Interface>
<Interface>
<IfIndex>9</IfIndex>
<Name>GigabitEthernet1/0/9</Name>
<PortIndex>9</PortIndex>
</Interface>
<Interface>
<IfIndex>10</IfIndex>
<Name>GigabitEthernet1/0/10</Name>
<PortIndex>10</PortIndex>
</Interface>
<Interface>
<IfIndex>11</IfIndex>
<Name>GigabitEthernet1/0/11</Name>
<PortIndex>11</PortIndex>
</Interface>
<Interface>
<IfIndex>12</IfIndex>
<Name>GigabitEthernet1/0/12</Name>
<PortIndex>12</PortIndex>
</Interface>
<Interface>
<IfIndex>13</IfIndex>
<Name>GigabitEthernet1/0/13</Name>
<PortIndex>13</PortIndex>
</Interface>
<Interface>
<IfIndex>14</IfIndex>
<Name>GigabitEthernet1/0/14</Name>
<PortIndex>14</PortIndex>
</Interface>
<Interface>
<IfIndex>15</IfIndex>
<Name>GigabitEthernet1/0/15</Name>
<PortIndex>15</PortIndex>
</Interface>
<Interface>
<IfIndex>16</IfIndex>
<Name>GigabitEthernet1/0/16</Name>
<PortIndex>16</PortIndex>
</Interface>
<Interface>
<IfIndex>17</IfIndex>
<Name>GigabitEthernet1/0/17</Name>
<PortIndex>17</PortIndex>
</Interface>
<Interface>
<IfIndex>18</IfIndex>
<Name>GigabitEthernet1/0/18</Name>
<PortIndex>18</PortIndex>
</Interface>
<Interface>
<IfIndex>19</IfIndex>
<Name>GigabitEthernet1/0/19</Name>
<PortIndex>19</PortIndex>
</Interface>
<Interface>
<IfIndex>20</IfIndex>
<Name>GigabitEthernet1/0/20</Name>
<PortIndex>20</PortIndex>
</Interface>
<Interface>
<IfIndex>21</IfIndex>
<Name>GigabitEthernet1/0/21</Name>
<PortIndex>21</PortIndex>
</Interface>
<Interface>
<IfIndex>22</IfIndex>
<Name>GigabitEthernet1/0/22</Name>
<PortIndex>22</PortIndex>
</Interface>
<Interface>
<IfIndex>23</IfIndex>
<Name>GigabitEthernet1/0/23</Name>
<PortIndex>23</PortIndex>
</Interface>
<Interface>
<IfIndex>24</IfIndex>
<Name>GigabitEthernet1/0/24</Name>
<PortIndex>24</PortIndex>
</Interface>
<Interface>
<IfIndex>25</IfIndex>
<Name>GigabitEthernet1/0/25</Name>
<PortIndex>25</PortIndex>
</Interface>
<Interface>
<IfIndex>26</IfIndex>
<Name>GigabitEthernet1/0/26</Name>
<PortIndex>26</PortIndex>
</Interface>
<Interface>
<IfIndex>27</IfIndex>
<Name>GigabitEthernet1/0/27</Name>
<PortIndex>27</PortIndex>
</Interface>
<Interface>
<IfIndex>28</IfIndex>
<Name>GigabitEthernet1/0/28</Name>
<PortIndex>28</PortIndex>
</Interface>
<Interface>
<IfIndex>29</IfIndex>
<Name>GigabitEthernet1/0/29</Name>
<PortIndex>29</PortIndex>
</Interface>
<Interface>
<IfIndex>30</IfIndex>
<Name>GigabitEthernet1/0/30</Name>
<PortIndex>30</PortIndex>
</Interface>
<Interface>
<IfIndex>31</IfIndex>
<Name>GigabitEthernet1/0/31</Name>
<PortIndex>31</PortIndex>
</Interface>
<Interface>
<IfIndex>32</IfIndex>
<Name>GigabitEthernet1/0/32</Name>
<PortIndex>32</PortIndex>
</Interface>
<Interface>
<IfIndex>33</IfIndex>
<Name>GigabitEthernet1/0/33</Name>
<PortIndex>33</PortIndex>
</Interface>
<Interface>
<IfIndex>34</IfIndex>
<Name>GigabitEthernet1/0/34</Name>
<PortIndex>34</PortIndex>
</Interface>
<Interface>
<IfIndex>35</IfIndex>
<Name>GigabitEthernet1/0/35</Name>
<PortIndex>35</PortIndex>
</Interface>
<Interface>
<IfIndex>36</IfIndex>
<Name>GigabitEthernet1/0/36</Name>
<PortIndex>36</PortIndex>
</Interface>
<Interface>
<IfIndex>37</IfIndex>
<Name>GigabitEthernet1/0/37</Name>
<PortIndex>37</PortIndex>
</Interface>
<Interface>
<IfIndex>38</IfIndex>
<Name>GigabitEthernet1/0/38</Name>
<PortIndex>38</PortIndex>
</Interface>
<Interface>
<IfIndex>39</IfIndex>
<Name>GigabitEthernet1/0/39</Name>
<PortIndex>39</PortIndex>
</Interface>
<Interface>
<IfIndex>40</IfIndex>
<Name>GigabitEthernet1/0/40</Name>
<PortIndex>40</PortIndex>
</Interface>
<Interface>
<IfIndex>41</IfIndex>
<Name>GigabitEthernet1/0/41</Name>
<PortIndex>41</PortIndex>
</Interface>
<Interface>
<IfIndex>42</IfIndex>
<Name>GigabitEthernet1/0/42</Name>
<PortIndex>42</PortIndex>
</Interface>
<Interface>
<IfIndex>43</IfIndex>
<Name>GigabitEthernet1/0/43</Name>
<PortIndex>43</PortIndex>
</Interface>
<Interface>
<IfIndex>44</IfIndex>
<Name>GigabitEthernet1/0/44</Name>
<PortIndex>44</PortIndex>
</Interface>
<Interface>
<IfIndex>45</IfIndex>
<Name>GigabitEthernet1/0/45</Name>
<PortIndex>45</PortIndex>
</Interface>
<Interface>
<IfIndex>46</IfIndex>
<Name>GigabitEthernet1/0/46</Name>
<PortIndex>46</PortIndex>
</Interface>
<Interface>
<IfIndex>47</IfIndex>
<Name>GigabitEthernet1/0/47</Name>
<PortIndex>47</PortIndex>
</Interface>
<Interface>
<IfIndex>48</IfIndex>
<Name>GigabitEthernet1/0/48</Name>
<PortIndex>48</PortIndex>
</Interface>
<Interface>
<IfIndex>49</IfIndex>
<Name>Ten-GigabitEthernet1/0/49</Name>
<PortIndex>49</PortIndex>
</Interface>
<Interface>
<IfIndex>50</IfIndex>
<Name>Ten-GigabitEthernet1/0/50</Name>
<PortIndex>50</PortIndex>
</Interface>
<Interface>
<IfIndex>51</IfIndex>
<Name>Ten-GigabitEthernet1/0/51</Name>
<PortIndex>51</PortIndex>
</Interface>
<Interface>
<IfIndex>52</IfIndex>
<Name>Ten-GigabitEthernet1/0/52</Name>
<PortIndex>52</PortIndex>
</Interface>
<Interface>
<IfIndex>631</IfIndex>
<Name>M-GigabitEthernet0/0/0</Name>
<PortIndex>631</PortIndex>
</Interface>
<Interface>
<IfIndex>632</IfIndex>
<Name>NULL0</Name>
</Interface>
<Interface>
<IfIndex>633</IfIndex>
<Name>InLoopBack0</Name>
</Interface>
<Interface>
<IfIndex>634</IfIndex>
<Name>LoopBack111</Name>
</Interface>
<Interface>
<IfIndex>636</IfIndex>
<Name>Vlan-interface1</Name>
</Interface>
<Interface>
<IfIndex>637</IfIndex>
<Name>Vlan-interface99</Name>
</Interface>
</Interfaces>
</Ifmgr>
<MAC>
<MacUnicastTable>
<Unicast>
<VLANID>2</VLANID>
<MacAddress>FE-BB-FE-BB-FE-BB</MacAddress>
<PortIndex>2</PortIndex>
<Status>3</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>00-3C-10-66-0A-98</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>00-45-1D-79-B3-39</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>00-8E-73-E6-AC-19</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>00-8E-73-E6-AC-40</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>08-5B-0E-2F-62-6A</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>0C-11-67-9A-0E-98</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>0C-11-67-9A-0E-C0</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>2C-33-11-40-32-80</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>2C-33-11-6E-C3-00</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>40-A6-E8-8A-0A-B9</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>44-A8-42-05-7E-FD</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>4C-E9-E4-2E-B8-1A</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>70-4C-A5-EB-40-BB</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>70-EA-1A-AD-5C-6C</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>74-A2-E6-66-5A-39</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>78-2C-29-2B-2B-90</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>78-2C-29-2B-67-F0</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>78-2C-29-2B-85-37</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>78-2C-29-44-C5-CD</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>78-2C-29-44-D9-4D</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>78-2C-29-44-DE-95</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>78-2C-29-44-DF-CD</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>78-2C-29-AD-3F-DE</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>7C-1E-06-24-41-A5</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>7C-1E-06-24-45-B5</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>CC-98-91-03-DE-B9</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>CC-98-91-A4-71-39</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>CC-98-91-A4-C9-39</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>CC-98-91-A4-F6-39</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>CC-98-91-C3-7F-B9</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>CC-98-91-C3-9E-B9</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>CC-98-91-C3-B0-B9</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>CC-98-91-DA-2A-B9</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>CC-98-91-DA-39-39</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>DC-DA-80-61-94-89</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>DC-DA-80-61-A0-89</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>DC-DA-80-61-A0-98</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>F8-A0-3D-40-B8-1D</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>F8-A0-3D-40-C1-6F</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
<Unicast>
<VLANID>99</VLANID>
<MacAddress>F8-A0-3D-40-C1-7D</MacAddress>
<PortIndex>48</PortIndex>
<Status>2</Status>
<Aging>true</Aging>
</Unicast>
</MacUnicastTable>
</MAC>
</top>
</data>
</rpc-reply>
本文详细介绍了如何使用Python通过NETCONF协议对H3C交换机进行配置,包括环境搭建、SSH连接、XML API调用示例,以及XML到JSON的转换。重点展示了NETCONF在配置自动化中的应用和优势。

192

被折叠的 条评论
为什么被折叠?



