聲明:本文參考了以下連接,若有侵權 請告知刪除,謝謝!
http://www.javashuo.com/article/p-gmoamqpl-ve.html
https://www.cnblogs.com/michael9/p/14481135.html
http://www.h3c.com/cn/d_201905/1185327_30005_0.htm
https://github.com/ncclient
https://netheroone.cn/archives/5fe04e01.html
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>