11-Python配置
本章節下載: 11-Python配置 (202.45 KB)
Python是一種簡單易學,功能強大的編程語言,它有高效率的高層數據結構,簡單而有效地實現了麵向對象編程。Python簡潔的語法和對動態輸入的支持,再加上解釋性語言的本質,使得它在大多數平台上的許多領域都是一個理想的腳本語言,特別適用於快速的應用程序開發。
Comware V7係統內嵌了Python解釋器,可以直接在設備上使用Python2.7版本的命令和標準API。為了方便用戶進行係統配置,Comware對Python進行了擴展,用戶可以使用這些擴展功能。關於Comware的Python擴展,可以參考“2 附錄:Comware Python API”。
Comware V7係統內嵌了Python解釋器。用戶可以進入Python shell,使用Python的命令、標準API和擴展API;還可以直接在設備上執行Python腳本,以方便自動化配置係統。
表1-1 進入Python shell
操作 |
命令 |
說明 |
進入Python shell |
python |
該命令在用戶視圖下執行 |
表1-2 執行Python腳本文件
操作 |
命令 |
說明 |
執行Python腳本文件 |
python filename |
該命令在用戶視圖下執行 |
表1-3 退出Python shell
操作 |
命令 |
說明 |
退出Python shell |
exit() |
該命令在Python shell下執行 |
使用Python腳本,下載main.cfg和backup.cfg兩個配置文件到設備上,並設置為下次主用配置文件和備用配置文件。
圖1-1 Python典型配置舉例組網圖
# 在PC上使用寫字板編輯Python腳本文件test.py,內容如下:
#!usr/bin/python
import comware
comware.Transfer('tftp', '192.168.1.26', 'main.cfg', 'flash:/main.cfg')
comware.Transfer('tftp', '192.168.1.26', 'backup.cfg', 'flash:/backup.cfg')
comware.CLI('startup saved-configuration flash:/main.cfg main ;startup saved-configuration flash:/backup.cfg backup')
# 通過TFTP將test.py文件下載到設備上
<Sysname> tftp 192.168.1.26 get test.py
# 執行Python腳本文件
<Sysname> python flash:/test.py
<Sysname>startup saved-configuration flash:/main.cfg main
Please wait...... Done.
<Sysname>startup saved-configuration flash:/backup.cfg backup
Please wait...... Done.
# 使用display startup命令查看下次啟動文件已經變為main.cfg和backup.cfg。
<Sysname> display startup
Current startup saved-configuration file: flash:/startup.cfg
Next main startup saved-configuration file: flash:/main.cfg
Next backup startup saved-configuration file: flash:/backup.cfg
本文描述在Comware V7中提供的擴展Python API,擴展Python API必須遵循標準Python語言語法。在使用擴展Python API時,必須先導入Comware包,導入方法有兩種:
· 方法一:用import comware引入整個Comware包,在執行具體API的時候用comware.API。例如,下麵的舉例表示:使用API Transfer將TFTP服務器(192.168.1.26)上的文件test.cfg下載到設備上。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.Transfer('tftp', '192.168.1.26', 'test.cfg', 'flash:/test.cfg', user='', password='')
<comware.Transfer object at 0xb7eab0e0>
· 方法二:用from comware import API引入單個API。例如,下麵的舉例表示:使用API Transfer將TFTP服務器(192.168.1.26)上的文件test.cfg下載到設備上。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from comware import Transfer
>>> Transfer('tftp', '192.168.1.26', 'test.cfg', 'flash:/test.cfg', user='', password='')
<comware.Transfer object at 0xb7e5e0e0>
用來執行Comware V7係統的命令並創建CLI對象。
【命令】
CLI(command=‘’, do_print=True)
【參數】
command:表示要下發的命令,缺省為空。多條命令之間以空格加分號分隔,如’system-view ;local-user test class manage’。
do_print:表示是否輸出執行結果,True表示輸出執行結果,False表示不輸出執行結果。缺省值為True。
【返回值】
CLI對象
【使用指導】
需要注意下發命令是從用戶視圖開始,如果需要在其它視圖下下發命令,需要首先進入該視圖。
如果command中不指定視圖,直接輸入命令,表示該命令在用戶視圖下執行;當需要執行其它視圖的命令時,需要先輸入進視圖的命令,再輸入具體的配置命令。
需要注意的是,CLI僅支持Comware命令,不支持Linux、Python、Tcl命令。
【舉例】
# 使用API CLI添加本地用戶test。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.CLI('system-view ;local-user test class manage')
【結果】
<Sysname> system-view
System View: return to User View with Ctrl+Z.
[Sysname] local-user test class manage
New local user added.
<comware.CLI object at 0xb7f680a0>
用來獲取命令執行的輸出信息。
【命令】
CLI.get_output()
【返回值】
命令執行的輸出信息
【舉例】
# 使用API CLI添加本地用戶,並輸出命令行執行結果。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import comware
>>> c = comware.CLI('system-view ;local-user test class manage', False)
>>> c.get_output()
【結果】
['<Sysname>system-view', 'System View: return to User View with Ctrl+Z.', '[Sysname]local-user test class manage', 'New local user added.']
用來將指定文件通過指定協議下載到本地。
【命令】
Transfer(protocol=‘’, host=‘’, source=‘’, dest=‘’, vrf=‘’, login_timeout=10, user=‘’, password=‘’)
【參數】
protocol:表示下載文件時使用的協議。取值為:
· ftp:表示使用FTP協議傳輸文件。
· tftp:表示使用TFTP協議傳輸文件。
· http:表示使用HTTP協議傳輸文件。
host:表示遠程服務器的IP地址。
source:表示服務器上源文件的名稱。
dest:表示保存到本地的目的文件的名稱。
vrf:指定目的端所屬的MPLS L3VPN的VPN實例名稱,為1~31個字符的字符串,區分大小寫。如果未指定本參數,則表示目的端位於公網中。
login_timeout:表示下載文件時登錄的超時時間,單位為秒,缺省值為10。
user:表示登錄時使用的用戶名稱。
password:表示登錄時使用的用戶密碼。
【返回值】
Transfer對象
【舉例】
# 使用API Transfer將TFTP服務器(192.168.1.26)上的文件test.cfg下載到設備上。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.Transfer('tftp', '192.168.1.26', 'test.cfg', 'flash:/test.cfg', user='', password='')
【結果】
<comware.Transfer object at 0xb7f700e0>
用來獲取下載文件過程中的錯誤信息。
【命令】
Transfer.get_error()
【返回值】
下載文件過程中的錯誤信息,若沒有錯誤信息則返回None。
【舉例】
# 使用API Transfer將TFTP服務器(1.1.1.1)上的文件test.cfg下載到設備上。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import comware
>>> c = comware.Transfer('tftp', '1.1.1.1', 'test.cfg', 'flash:/test.cfg', user='', password='')
>>> c.get_error()
【結果】
“Timeout was reached”
get_self_slot接口用來獲取主設備的成員編號。
【命令】
get_self_slot()
【返回值】
返回一個列表對象,格式為:[-1,slot-number],其中slot-number表示主設備在IRF中的成員編號。
【舉例】
# 使用API獲取主設備所在的成員編號。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_self_slot()
【結果】
[-1,1]
get_standby_slot接口用來獲取所有從設備的成員編號。
【命令】
get_standby_slot()
【返回值】
返回一個列表對象,格式為:[[-1,slot-number]],其中slot-number表示從設備在IRF中的成員編號。如果IRF中沒有從設備,則返回[ ];當IRF中有多個從設備時,則返回:[[-1,slot-number1],[-1,slot-number2],……]。
【舉例】
# 使用API獲取從設備所在的成員編號。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_standby_slot()
【結果】
[[-1, 1], [-1, 2]]
get_slot_range接口用來獲取當前係統所支持的成員編號範圍。
【命令】
get_slot_range()
【返回值】
返回一個字典對象,返回值始終為{'MaxSlot': max-slot-number, 'MinSlot': min-slot-number }。max-slot-number表示設備支持的最大成員編號,min-slot-number表示設備支持的最小成員編號。
【舉例】
# 使用API獲取係統槽位號範圍。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware. get_slot_range()
【結果】
{'MaxSlot': 129, 'MinSlot': 1}
get_slot_info接口用來獲取指定成員設備的信息。
【命令】
get_slot_info()
【返回值】
返回一個字典對象,返回值始終為{'Slot': slot-number, 'Status': 'status', 'Chassis': chassis-number, 'Role': 'role', 'Cpu': CPU-number }。slot-number表示備在IRF中的成員編號,status表示成員設備的狀態,chassis-number取值固定為0,role表示成員設備的角色,CPU-number取值固定為0。
【舉例】
# 使用API獲取成員設備信息。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_slot_info(1)
【結果】
{'Slot': 1, 'Status': 'Normal', 'Chassis': 0, 'Role': 'Master', 'Cpu': 0}
不同款型規格的資料略有差異, 詳細信息請向具體銷售和400谘詢。H3C保留在沒有任何通知或提示的情況下對資料內容進行修改的權利!