03-Python配置
本章節下載: 03-Python配置 (229.24 KB)
目 錄
Python是一種簡單易學,功能強大的編程語言,它有高效率的高層數據結構,簡單而有效地實現了麵向對象編程。Python簡潔的語法和對動態輸入的支持,再加上解釋性語言的本質,使得它在大多數平台上的許多領域都是一個理想的腳本語言,特別適用於快速的應用程序開發。
在Comware V9係統上可以采用如下方式使用Python:
· 通過執行Python腳本進行自動化配置係統。
· 進入Python shell,使用Python2.7版本的命令、標準API或擴展API對設備進行配置。其中,擴展API是Comware對Python進行的擴展,用來方便用戶進行係統配置。關於Comware的Python擴展,可以參考“2 Comware擴展Python API”。
請在用戶視圖下執行本命令,執行Python腳本文件。
python filename
請在用戶視圖下執行本命令,進入Python shell。
python
用戶如需使用擴展Python API,必須先導入Comware包。導入時,可選擇導入整個Comware包或單個API。
(1) 請在用戶視圖下執行本命令,進入Python shell。
python
(2) 導入整個Comware包。
import comware
(3) 執行擴展API。
comware.api
# 下例采用API Transfer將TFTP服務器(192.168.1.26)上的文件test.cfg下載到設備上。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "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>
(1) 請在用戶視圖下執行本命令,進入Python shell。
python
(2) 導入單個API函數。
from comware import api-name
(3) 執行擴展API函數。
api-function
# 下例采用API Transfer將TFTP服務器(192.168.1.26)上的文件test.cfg下載到設備上。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "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>
請在Python shell下執行本命令,退出Python shell。
exit()
使用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 V9中提供的擴展Python API,擴展Python API必須遵循標準Python語言語法。
用來執行Comware係統的命令並創建channel對象。
【命令】
channel(command=‘’)
【參數】
command:表示要下發的命令,缺省為空。channel下發命令是從用戶視圖開始,如果command中不指定視圖,直接輸入命令,表示該命令在用戶視圖下執行;當需要執行其它視圖的命令時,需要先輸入進視圖的命令,再輸入具體的配置命令。多條命令之間以空格加分號分隔,如’system-view ;display interface brief | include UP’。
【返回值】
命令執行的輸出信息,為列表對象。
【使用指導】
channel僅支持Comware命令,不支持Linux、Python、Tcl命令。
channel一般用於自動化測試。
channel返回的列表對象中可能含有十六進製隱藏字符,例如0x08。如果要避免顯示隱藏字符,可以使用API CLI;或者通過Python的for語句循環輸出channel的執行結果,例如:
<Sysname> python
>>> import comware
>>> c = comware.channel('display device')
>>> for value in c:
... print value
...
display device
Slot No. Brd Type Brd Status Subslot Sft Ver Patch Ver
1 Device-ABCDE Master 0 Device-ABCDE-1234 None
<Sysname>
【舉例】
# 使用API channel顯示設備信息。
<Sysname> python
>>> import comware
>>> comware.channel('display device')
【結果】
['display device', 'Slot No. Brd Type Brd Status Subslot Sft Ver Patch Ver', ' 1 Device-ABCDE Master 0 Device-ABCDE-1234 None ', '<Sysname>']
用來執行Comware V9係統的命令並創建CLI對象。
【命令】
CLI(command=‘’, do_print=True)
【參數】
command:表示要下發的命令,缺省為空。CLI下發命令是從用戶視圖開始,如果command中不指定視圖,直接輸入命令,表示該命令在用戶視圖下執行;當需要執行其它視圖的命令時,需要先輸入進視圖的命令,再輸入具體的配置命令。多條命令之間以空格加分號分隔,如’system-view ;local-user test class manage’。
do_print:表示是否輸出執行結果,True表示輸出執行結果,False表示不輸出執行結果。缺省值為True。
【返回值】
CLI對象
【使用指導】
需要注意的是,CLI僅支持Comware命令,不支持Linux、Python、Tcl命令。
【舉例】
# 使用API CLI添加本地用戶test。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "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>
用來獲取下載文件過程中的錯誤信息。
【命令】
Transfer.get_error()
【返回值】
下載文件過程中的錯誤信息,若沒有錯誤信息則返回空值。
【舉例】
# 使用API Transfer將TFTP服務器(1.1.1.1)上的文件test.cfg下載到設備上。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "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'
用來獲取命令執行的輸出信息。
【命令】
CLI.get_output()
【返回值】
命令執行的輸出信息
【舉例】
# 使用API CLI添加本地用戶,並輸出命令行執行結果。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "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.']
get_self_slot接口用來獲取主用主控板所在的槽位號。
【命令】
get_self_slot()
【返回值】
返回一個列表對象,格式為:[-1,slot-number],其中slot-number表示主用主控板所在的槽位號。
【舉例】
# 使用API獲取主用主控板所在的槽位號/主設備所在的成員編號。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_self_slot()
【結果】
[-1,0]
get_slot_info接口用來獲取指定單板的信息。
【命令】
get_slot_info(global-slot-number)
【參數】
global-slot-number:全局slot編號。
【返回值】
返回一個字典對象,返回值始終為{'Slot': slot-number, 'Status': 'status', 'Chassis': chassis-number, 'Role': 'role', 'Cpu': CPU-number }。slot-number表示單板所在的槽位號,status表示單板的狀態,chassis-number表示設備的成員編號,role表示單板的角色,CPU-number表示單板上主CPU的編號。
【舉例】
# 使用API獲取指定slot的信息。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_slot_info(1)
【結果】
{'Slot': 1, 'Status': 'Normal', 'Chassis': 0, 'Role': 'Master', 'Cpu': 0}
get_slot_range接口用來獲取當前係統所支持的slot編號範圍。
【命令】
get_slot_range()
【返回值】
返回一個字典對象,返回值始終為{'MaxSlot': max-slot-number, 'MinSlot': min-slot-number }。max-slot-number表示設備支持的最大slot編號,min-slot-number表示設備支持的最小slot編號。
【舉例】
# 使用API獲取係統slot編號範圍。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_slot_range()
【結果】
{'MaxSlot': 129, 'MinSlot': 1}
get_standby_slot接口用來獲取所有備用主控板所在的槽位號。
【命令】
get_standby_slot()
【返回值】
返回一個列表對象,格式為:[[-1,slot-number]],其中slot-number表示備用主控板所在的槽位號。如果設備上沒有備用主控板,則返回[ ]。
【舉例】
# 使用API獲取備用主控板所在的槽位號。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_standby_slot()
【結果】
[[-1, 1], [-1, 2]]
用來將指定文件通過指定協議下載到本地。
【命令】
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對象
【使用指導】
當遠程服務器使用IPv6地址時,該地址必須置於方括號[ ]內。例如:
Transfer("ftp","[2200::22]","0729.cfg","flash:/0729.cfg",user="abc",password="123456")
【舉例】
# 使用API Transfer將TFTP服務器(192.168.1.26)上的文件test.cfg下載到設備上。
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "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>
不同款型規格的資料略有差異, 詳細信息請向具體銷售和400谘詢。H3C保留在沒有任何通知或提示的情況下對資料內容進行修改的權利!