1、NETCONF簡介
NETCONF(Network Configuration Protocol,網絡配置協議)是一種基於XML的網絡管理協議,它提供了一種可編程的、對網絡設備進行配置和管理的方法。用戶可以通過該協議設置參數、獲取參數值、獲取統計信息等。NETCONF協議采用了分層結構,分成四層:內容層、操作層、RPC(Remote Procedure Call,遠程調用)層和通信協議層。
2、操作層介紹(ncclient庫中manager.py模塊)
manager支持的操作:(manager中的操作,都是映射到ncclient.operations.xxx對應的class)
2.1 <get>
用於查詢狀態數據,另外如果支持server能力:urn:ietf:params:netconf:capability:xpath:1.0則還可以使用filter進行條件查詢,例如:
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <get> <filter type="subtree"> <top xmlns="http://example.com/schema/1.2/stats"> <interfaces> <interface> <ifName>eth0</ifName> </interface> </interfaces> </top> </filter> </get> </rpc> <rpc-reply message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <data> <top xmlns="http://example.com/schema/1.2/stats"> <interfaces> <interface> <ifName>eth0</ifName> <ifInOctets>45621</ifInOctets> <ifOutOctets>774344</ifOutOctets> </interface> </interfaces> </top> </data> </rpc-reply>
2.2 <get-config>
用於查詢配置數據,可以通過<source/>來指定不同的配置庫,例如:
<rpc message-id="101"xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <get-config> <source> <running/> </source> <filter type="subtree"> <top xmlns="http://example.com/schema/1.2/config"> <users/> </top> </filter> </get-config> </rpc> <rpc-reply message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <data> <top xmlns="http://example.com/schema/1.2/config"> <users> <user> <name>root</name> <type>superuser</type> <full-name>Charlie Root</full-name> <company-info> <dept>1</dept> <id>1</id> </company-info> </user> <!-- additional <user> elements appear here... --> </users> </top> </data> </rpc-reply>
2.3 <edit-config>
用於對指定配置數據庫內容進行修改,支持以下幾種操作:
merge: 合並操作,此操作為默認操作。
replace: 替換操作,如果對象已經存在則替換,不存在則創建。
create: 創建操作,如果對象已經存在,則報錯誤“data-exists”。
delete: 刪除操作,如果對象存在則刪除,不存在則報錯 “data-missing”。
remove: 刪除操作,如果對象存在則刪除,不存在則忽略。
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <edit-config> <target> <running/> </target> <config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> <top xmlns="http://example.com/schema/1.2/config"> <interface xc:operation="replace"> <name>Ethernet0/0</name> <mtu>1500</mtu> <address> <name>192.0.2.4</name> <prefix-length>24</prefix-length> </address> </interface> </top> </config> </edit-config> </rpc> <rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <edit-config> <target> <running/> </target> <default-operation>none</default-operation> <config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> <top xmlns="http://example.com/schema/1.2/config"> <protocols> <ospf> <area> <name>0.0.0.0</name> <interfaces> <interface xc:operation="delete"> <name>192.0.2.4</name> </interface> </interfaces> </area> </ospf> </protocols> </top> </config> </edit-config> </rpc>
2.4 <copy-config>
將一個庫的數據復制到另一個庫。
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <copy-config> <target> <running/> </target> <source> <url>https://user:password@example.com/cfg/new.txt</url> </source> </copy-config> </rpc>
2.5 <delete-config>
刪除一個數據庫。但是<running/>庫不能被刪除。
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <delete-config> <target> <startup/> </target> </delete-config> </rpc>
2.6 <lock>
獲取指定數據庫的鎖,當某個client獲得了指定數據庫的鎖之后,在其沒有釋放該鎖之前,其余client均不能獲得該數據庫的鎖,也不能對其進行修改。
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <lock> <target> <running/> </target> </lock> </rpc>
2.7 <unlock>
釋放指定數據庫的鎖。client只能釋放自己持有的鎖,不能釋放其它client的鎖。
<rpc message-id="101" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> <unlock> <target> <running/> </target> </unlock> </rpc>
2.8 <close-session>
優雅關閉netconf會話,netconf-server將釋放該client持有的鎖和為其分配的資源,並優化的關閉與該client的鏈接。所有在<close-session>之后收到的操作均會被忽略。
2.9 <kill-session>
強制關閉netconf會話。
相關鏈接:
https://www.cnblogs.com/LiuYanYGZ/p/14286189.html