12-Python配置
本章節下載: 12-Python配置 (215.28 KB)
Python是一種簡單易學,功能強大的編程語言,它有高效率的高層數據結構,簡單而有效地實現了麵向對象編程。Python簡潔的語法和對動態輸入的支持,再加上解釋性語言的本質,使得它在大多數平台上的許多領域都是一個理想的腳本語言,特別適用於快速的應用程序開發。
在Comware V7係統上可以采用如下方式使用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,必須先導入Platformtools包。導入時,可選擇導入整個Platformtools包或單個API。
(1) 請在用戶視圖下執行本命令,進入Python shell。
python
(2) 導入整個Platformtools包。
import platformtools
(3) 執行擴展API。
platformtools.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 platformtools
>>> platformtools.Transfer('tftp', '192.168.1.26', 'test.cfg', 'flash:/test.cfg', user='', password='')
<platformtools.Transfer object at 0xb7eab0e0>
(1) 請在用戶視圖下執行本命令,進入Python shell。
python
(2) 導入單個API函數。
from platformtools import api-name
(3) 執行擴展API函數。
api-function
# 下例采用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 platformtools import Transfer
>>> Transfer('tftp', '192.168.1.26', 'test.cfg', 'flash:/test.cfg', user='', password='')
<platformtools.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 platformtools
platformtools.Transfer('tftp', '192.168.1.26', 'main.cfg', 'flash:/main.cfg')
platformtools.Transfer('tftp', '192.168.1.26', 'backup.cfg', 'flash:/backup.cfg')
platformtools.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語言語法。
用來執行Comware V7係統的命令並創建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 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platformtools
>>> platformtools.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.
<platformtools.CLI object at 0xb7f680a0>
用來獲取下載文件過程中的錯誤信息。
【命令】
Transfer.get_error()
【返回值】
下載文件過程中的錯誤信息,若沒有錯誤信息則返回空值。
【舉例】
# 使用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 platformtools
>>> c = platformtools.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 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platformtools
>>> c = platformtools.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()
【返回值】
返回一個列表對象,格式為:[chassis-number,slot-number],其中:chassis-number表示全局主控板所在設備的成員編號,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 platformtools
>>> platformtools.get_self_slot()
【結果】
[-1,0]
get_slot_info接口用來獲取指定單板的信息。
【命令】
get_slot_info()
【返回值】
返回一個字典對象,返回值始終為{'Slot': slot-number, 'Status': 'status', 'Chassis': chassis-number, 'Role': 'role', 'Cpu': CPU-number }。slot-number表示單板所在的槽位號,status表示單板的狀態,chassis-number表示設備在IRF中的成員編號,role表示單板的角色,CPU-number表示單板上主CPU的編號。
【舉例】
# 使用API獲取成員編號/槽位號信息。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platformtools
>>> platformtools.get_slot_info(1)
【結果】
{'Slot': 1, 'Status': 'Normal', 'Chassis': 0, 'Role': 'Master', 'Cpu': 0}
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 platformtools
>>> platformtools.get_slot_range()
【結果】
{'MaxSlot': 5, 'MinSlot': 0}
get_standby_slot接口用來獲取所有全局備用主控板所在的槽位號。
【命令】
get_standby_slot()
【返回值】
返回一個列表對象,格式為:[[chassis-number,slot-number]],其中:chassis-number表示全局備用主控板所在設備的成員編號,slot-number表示全局備用主控板在成員設備上的槽位號。如果IRF中沒有全局備用主控板,則返回[ ];當IRF中有多個全局備用主控板時,則返回:[[chassis-number1,slot-number1],[chassis-number2,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 platformtools
>>> platformtools.get_standby_slot()
【結果】
[]
用來將指定文件通過指定協議下載到本地。
【命令】
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 platformtools
>>> platformtools.Transfer('tftp', '192.168.1.26', 'test.cfg', 'flash:/test.cfg', user='', password='')
【結果】
<platformtools.Transfer object at 0xb7f700e0>
用來生成並發送日誌信息。
【命令】
SYSLOG.send(digest=‘’, info=‘’, priority=‘’)
【參數】
digest:表示日誌助記符,為1~32個字符的字符串,區分大小寫。
info:日誌內容,為1~1024個字符的字符串,區分大小寫。
priority:表示日誌優先級,取值範圍為0~4294967295。
【返回值】
無。
【使用指導】
當需要使用Python生成並發送日誌時,請先使用SYSLOG API創建SYSLOG對象,並指定日誌所屬模塊,再使用API send指定日誌參數、生成並發送日誌。有關日誌信息的格式的詳細介紹,請參見“網絡管理和監控配置指導”中的“信息中心”。
要使當前終端能夠顯示日誌信息,請先在用戶視圖下執行terminal monitor命令。
【舉例】
# 配置允許當前終端顯示日誌信息,然後使用API生成並發送一條SNMP模塊的日誌:日誌助記符為Test、日誌信息為“Try to send one message.”、日誌優先級為1000。
<Sysname> terminal monitor
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platformtools
>>> a = platformtools.SYSLOG('snmp')
>>> a.send('Test','Try to send one message.',1000)
【結果】
>>> %Jan 1 06:24:17:908 2019 Sysname SNMP/0/Test: Try to send one message.
用來創建SYSLOG對象。
【命令】
SYSLOG(module=‘’)
【參數】
module:表示產生日誌的模塊名,為1~8個字符的字符串,不區分大小寫。
【返回值】
無。
【使用指導】
創建的SYSLOG對象用於發送指定模塊的日誌。
【舉例】
# 為SNMP模塊創建SYSLOG對象,並賦值給變量a。
<Sysname> python
Python 2.7.3 (default)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platformtools
>>> a = platformtools.SYSLOG('snmp')
>>> a
【結果】
<platformtools.SYSLOG object at 0xb7e180e0>
不同款型規格的資料略有差異, 詳細信息請向具體銷售和400谘詢。H3C保留在沒有任何通知或提示的情況下對資料內容進行修改的權利!