2附录:Comware Python API

本文描述在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下载到设备上。

python
Python 2.7.3 (default, May 24 2013,
14:37:26)
[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='')
·     方法二:用from comware import API引入单个API。例如,下面的举例表示:使用API Transfer将TFTP服务器(192.168.1.26)上的文件test.cfg下载到设备上。
 python
Python 2.7.3 (default, May 24 2013,
14:37:26)
[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='')
2.1  CLI类
用来执行Comware V7系统的命令并创建CLI对象。
【原型】
CLI(command=‘’,
do_print=True)
【参数】
command:表示要下发的命令,缺省为空。多条命令之间以空格加分号分隔,如’system-view
;local-user test class manage’。
do_print:表示是否输出执行结果,True表示输出执行结果,Flase表示不输出执行结果。缺省值为True。

【返回值】

CLI对象

【使用指导】

需要注意下发命令是从用户视图开始,如果需要在其它视图下下发命令需要首先进入该视图。

如果command中不指定视图,直接输入命令,表示该命令在用户视图下执行;当需要执行其它视图的命令时,需要先输入进视图的命令,再输入具体的配置命令。

【举例】

# 使用API CLI添加本地用户test。
 python
Python 2.7.3 (default, May 24 2013,
14:37:26)
[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')
【结果】
 system-view
System View: return to User View with Ctrl+Z.
[Sysname] local-user test class manage
New local user added.
用来获取命令执行的输出信息。
【原型】
CLI.get_output()
【返回值】
命令执行的输出信息
【举例】
# 使用API CLI添加本地用户,并输出命令行执行结果。
 python
Python 2.7.3 (default, May 24 2013,
14:37:26)
[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()
【结果】
['system-view',
'System View: return to User View with Ctrl+Z.', '[Sysname]local-user test class manage']
用来将指定文件通过指定协议下载到本地。
【原型】
Transfer(protocol=‘’,
host=‘’, source=‘’, dest=‘’, login_timeout=10, user=‘’,
password=‘’)
【参数】
protocol:表示下载文件时使用的协议。取值为:
·     ftp:表示使用FTP协议传输文件。
·     tftp:表示使用TFTP协议传输文件。
·     http:表示使用HTTP协议传输文件。
host:表示远程服务器的IP地址。
source:表示服务器上源文件的名称。
dest:表示保存到本地的目的文件的名称。
login_timeout:表示下载文件时登录的超时时间,缺省值为10,单位为秒。
user:表示登录时使用的用户名称。
password:表示登录时使用的用户密码。
【返回值】
Transfer对象
【举例】
# 使用API Transfer将TFTP服务器(192.168.1.26)上的文件test.cfg下载到设备上。
 python
Python 2.7.3 (default, May 24 2013,
14:37:26)
[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='')
【结果】
用来获取下载文件过程中的错误信息。
【原型】
Transfer.get_error()
【返回值】
下载文件过程中的错误信息,若没有错误信息则返回None。
【举例】
# 使用API Transfer将TFTP服务器(1.1.1.1)上的文件test.cfg下载到设备上。
 python
Python 2.7.3 (default, May 24 2013,
14:37:26)
[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()
【结果】
“Couldn’t connect to server”
get_self_slot接口用来获取主控单元所在的槽位号。(独立运行模式)
get_self_slot接口用来获取全局主用主控单元所在的槽位号。(IRF模式)
【命令】
get_self_slot()
【返回值】
·     独立运行模式
返回一个列表对象,格式为:[-1,slot-number],其中slot-number表示主控单元所在的槽位号。
·     IRF模式
返回一个列表对象,格式为:[chassis-number,slot-number],其中:chassis-number表示全局主用主控单元所在设备的成员编号,slot-number表示全局主控单元在成员设备上的槽位号。
【举例】
# 使用API获取主控单元所在的槽位号(独立运行模式)。
 python
Python 2.7.3 (default, May 24 2013,
14:37:26)
[GCC 4.4.1] on linux2
Type "help",
"copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_self_slot()
【结果】
[-1,0]
get_standby_slot接口用来获取所有全局备用主控单元所在的槽位号。(IRF模式)
【命令】
get_standby_slot()
【返回值】
返回一个列表对象,格式为:[[chassis-number,slot-number]],其中:chassis-number表示全局备用主控单元所在设备的成员编号,slot-number表示全局备用主控单元在成员设备上的槽位号。如果IRF中没有全局备用主控单元,则返回[ ];当IRF中有多个全局备用主控单元时,则返回:[[chassis-number1,slot-number1],[chassis-number2,slot-number2],……]。

H3C S9800系列以太网交换机独立运行模式下没有备用主控单元,执行get_standby_slot()返回值为[ ]。
【举例】
# 使用API获取备用主控单元所在的槽位号(IRF模式)。
 python
Python 2.7.3 (default, May 24 2013,
14:37:26)
[GCC 4.4.1] on linux2
Type "help",
"copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_standby_slot()

【结果】

[2,0]