1、實驗說明:有一台CE12800設備,管理IP地址192.168.56.177,
現在需要測試設備OPS(Open Programmability System)功能,查詢並清除設備的啟動配置文件。
本實驗需要額外准備FTP服務器/軟件,FTP服務器地址192.168.56.1
交換機為FTP客戶端,交換機管理IP:192.168.56.177
2、配置思路:
2.1在PC上編寫OPS腳本ops_demo.py
2.2上傳腳本文件ops_demo.py到交換機
2.3在交換機上運行ops_demo.py
FTP服務器工作目錄:F:\Users\Administrator\PycharmProjects\python_switch\ops\
使用FileZilla作為FTP軟件,Virtual path /ops/
Native path F:\Users\Administrator\PycharmProjects\python_switch\ops\
交換機配置:
int g1/0/0 un sh int vlani 1 ip add 192.168.56.177 q stel s e user-i v 4 auth aaa pro in ssh u p l 3 q ssh user python ssh user python auth password ssh user python ser stel aaa local-user python password irreversible-cipher Huawei@123 local-user python service-type ssh local-user python user-group manage-ug commit
ops_demo.py代碼:
#!/usr/bin # _*_ coding: UTF-8 _*_ # Copyright (c) 2021 GengYu.All rights reserved # @Create by gengyu # @Create Time :2021/12/18 # @File Name : ops_demo # 打包命令 pyinstaller -F package\ops_demo """ """ __author__ = 'Administrator' import traceback import httplib #Python 2.x中的"httplib"模塊在Python 3.x中變成了"http.client",目前交換機內置python 2.x # import http.client import string class OPSConnection(object): """Make an OPS connection instance.""" #初始化類,創建一個HTTP連接 def __init__(self, host, port = 80): self.host = host self.port = port self.headers = { "Content-type": "text/xml", "Accept": "text/xml" } self.conn = None #關閉HTTP連接 def close(self): """ Close the connection""" self.conn.close() #創建設備資源操作 def create(self, uri, req_data): """ Delete operation""" ret = self.rest_call("GET", uri, req_data) return ret #刪除設備資源操作 def delete(self, uri, req_data): """Delete operation""" ret = self.rest_call("DELETE", uri, req_data) return ret #查詢設備資源操作 def get(self, uri, req_data=None): """Get operation""" ret = self.rest_call("GET", uri, req_data) return ret #修改設備資源操作 def set(self, uri, req_data): """ Set operation""" ret = self.rest_call("PUT", uri, req_data) return ret #類內部調用的方法 def rest_call(self, method, uri, req_data): """ REST call""" print('| - - - - - - - - - - - - - - - request: - - - - - - - - - - - - - - -|') print('%s %s HTTP/1.1\n' % (method, uri)) if req_data == None: body = "" else: body = req_data print(body) if self.conn: self.conn.close() self.conn = httplib.HTTPConnection(self.host, self.port) # self.conn = http.client.HTTPConnection(self.host, self.port) self.conn.request(method, uri, body, self.headers) response = self.conn.getresponse() # response.status = http.client.OK #stub code response.status = httplib.OK # stub code ret = (response.status, response.reason, response.read()) print('| - - - - - - - - - - - - - - - response: - - - - - - - - - - - - - - -|') print('HTTP/1.1 %s %s\n\n%s' % ret) print('| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|') return ret def get_startup_info(ops_conn): #指定系統啟動信息的URI。URI為Restful API中定義的管理對象,不同的管理對象有不同的URI #用戶需要根據實際需求對URI進行修改,關於設備支持的URI可參考RESTful API uri = "/cfg/startupInfos/startupInfo" #指定發送的請求內容。該部分內容與URI相對應,不同的URI對應不同的請求內容 #用戶需要根據實際需求對URI進行修改,關於設備支持的URI可參考RESTful API req_data = \ """ <?xml version="1.0" encoding="UTF-8"?> <startupInfo> </startupInfo> """ ret,_,rsp_data = ops_conn.get(uri, req_data) if ret != httplib.OK: return None return rsp_data def clear_startup_info(ops_conn): uri = "/cfg/clearStartup" req_data = \ """ <?xml version="1.0" encoding="UTF-8"?> <clearStartup> </clearStartup> """ ret,_,rsp_data = ops_conn.create(uri, req_data) if ret != httplib.OK: return None def main(): """The main function.""" #host表示環路地址,當前RESTful API為設備內部調用,即取值為"localhost"。 host = "localhost" try: #建立HTTP連接 ops_conn = OPSConnection(host) #調用獲取系統啟動信息的函數 rsp_data = get_startup_info(ops_conn) rsp_data = clear_startup_info(ops_conn) rsp_data = get_startup_info(ops_conn) #關閉HTTP連接 ops_conn.close() return except: errinfo = traceback.format_exc() print(errinfo) return if __name__ == "__main__": main()
下載ops_demo.py文件到交換機:
ftp 192.168.56.1
admin
admin123
get /ops/ops_demo.py
q
在交換機上運行ops_demo.py文件
ops install file ops_demo.py
dis ops script
ops run python ops_demo.py
運行結果:
<HUAWEI>ops run python ops_demo.py
| - - - - - - - - - - - - - - - request: - - - - - - - - - - - - - - -|
GET /cfg/startupInfos/startupInfo HTTP/1.1
<?xml version="1.0" encoding="UTF-8"?>
<startupInfo>
</startupInfo>
| - - - - - - - - - - - - - - - response: - - - - - - - - - - - - - - -|
HTTP/1.1 200 OK
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply>
<data>
<cfg xmlns="http://www.huawei.com/netconf/vrp" format-version="1.0" content-
version="1.0">
<startupInfos>
<startupInfo>
<position>17</position>
<nextStartupFile>cfcard:/vrpcfg.cfg</nextStartupFile>
<configedSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</con
figedSysSoft>
<curSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</curSysSo
ft>
<nextSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</nextSys
Soft>
<curStartupFile>cfcard:/vrpcfg.cfg</curStartupFile>
<curPatchFile>NULL</curPatchFile>
<nextPatchFile>NULL</nextPatchFile>
<boardInfo>101</boardInfo>
<curPafFile>default</curPafFile>
<nextPafFile>default</nextPafFile>
</startupInfo>
</startupInfos>
</cfg>
</data>
</rpc-reply>
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| - - - - - - - - - - - - - - - request: - - - - - - - - - - - - - - -|
POST /cfg/clearStartup HTTP/1.1
<?xml version="1.0" encoding="UTF-8"?>
<clearStartup>
</clearStartup>
| - - - - - - - - - - - - - - - response: - - - - - - - - - - - - - - -|
HTTP/1.1 200 OK
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply>
<ok/>
</rpc-reply>
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| - - - - - - - - - - - - - - - request: - - - - - - - - - - - - - - -|
GET /cfg/startupInfos/startupInfo HTTP/1.1
<?xml version="1.0" encoding="UTF-8"?>
<startupInfo>
</startupInfo>
| - - - - - - - - - - - - - - - response: - - - - - - - - - - - - - - -|
HTTP/1.1 200 OK
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply>
<data>
<cfg xmlns="http://www.huawei.com/netconf/vrp" format-version="1.0" content-
version="1.0">
<startupInfos>
<startupInfo>
<position>17</position>
<nextStartupFile>NULL</nextStartupFile>
<configedSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</con
figedSysSoft>
<curSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</curSysSo
ft>
<nextSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</nextSys
Soft>
<curStartupFile>NULL</curStartupFile>
<curPatchFile>NULL</curPatchFile>
<nextPatchFile>NULL</nextPatchFile>
<boardInfo>101</boardInfo>
<curPafFile>default</curPafFile>
<nextPafFile>default</nextPafFile>
</startupInfo>
</startupInfos>
</cfg>
</data>
</rpc-reply>
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
