• 产品与解决方案
  • 行业解决方案
  • 服务
  • 支持
  • 合作伙伴
  • 新华三人才研学中心
  • 关于我们

01-基础配置配置指导

目录

14-Python配置

本章节下载 14-Python配置  (161.99 KB)

docurl=/cn/Service/Document_Software/Document_Center/Switches/Catalog/S12500/S12500-S/Configure/Operation_Manual/H3C_S12500-S_CG-R755X-6W100/01/201709/1029257_30005_0.htm

14-Python配置


1 Python

Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现了面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想的脚本语言,特别适用于快速的应用程序开发。

Comware V7系统内嵌了Python解释器,可以直接在设备上使用Python2.7版本的命令和标准API。为了方便用户进行系统配置,Comware对Python进行了扩展,用户可以使用这些扩展功能。关于Comware的Python扩展,可以参考“2 附录:Comware Python API”。

1.1  通过Python配置设备

Comware V7系统内嵌了Python解释器。用户可以进入Python shell,使用Python的命令、标准API和扩展API;还可以直接在设备上执行Python脚本,以方便自动化配置系统。

1.1.1  进入Python shell

表1-1 进入Python shell

操作

命令

说明

进入Python shell

python

该命令在用户视图下执行

 

1.1.2  执行Python脚本文件

表1-2 执行Python脚本文件

操作

命令

说明

执行Python脚本文件

python filename

该命令在用户视图下执行

 

1.1.3  退出Python shell

表1-3 退出Python shell

操作

命令

说明

退出Python shell

exit()

该命令在Python shell下执行

 

1.2  Python典型配置举例

1. 组网需求

使用Python脚本,下载main.cfg和backup.cfg两个配置文件到设备上,并设置为下次主用配置文件和备用配置文件。

2. 组网图

图1-1 Python典型配置举例组网图

 

3. 配置步骤

# 在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.

4. 验证结果

# 使用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


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下载到设备上。

<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>

2.1  CLI类

2.1.1  CLI

用来执行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>

2.1.2  get_output

用来获取命令执行的输出信息。

【命令】

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.']

2.2  Transfer

2.2.1  Transfer

用来将指定文件通过指定协议下载到本地。

【命令】

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>

2.2.2  get_error

用来获取下载文件过程中的错误信息。

【命令】

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”

2.3  API get_self_slot

2.3.1  get_self_slot

get_self_slot接口用来获取主用主控板所在的槽位号。(独立运行模式)

get_self_slot接口用来获取全局主用主控板所在的槽位号。(IRF模式)

【命令】

get_self_slot()

【返回值】

返回一个列表对象,格式为:[-1,slot-number],其中slot-number表示主用主控板所在的槽位号。(独立运行模式)

返回一个列表对象,格式为:[chassis-number,slot-number],其中:chassis-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,0]

2.4  API get_standby_slot

2.4.1  get_standby_slot

get_standby_slot接口用来获取所有备用主控板所在的槽位号。(独立运行模式)

get_standby_slot接口用来获取所有全局备用主控板所在的槽位号。(IRF模式)

【命令】

get_standby_slot()

【返回值】

返回一个列表对象,格式为:[[-1,slot-number]],其中slot-number表示备用主控板所在的槽位号。如果设备上没有备用主控板,则返回[ ]。(独立运行模式)

返回一个列表对象,格式为:[[chassis-number,slot-number]],其中:chassis-number表示全局备用主控板所在设备的成员编号,slot-number表示全局备用主控板在成员设备上的槽位号。如果IRF中没有全局备用主控板,则返回[ ];当IRF中有多个全局备用主控板时,则返回:[[chassis-number1,slot-number1],[chassis-number2,slot-number2],……]。(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_standby_slot()

【结果】

[]

2.5  API get_slot_range

2.5.1  get_slot_range

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': 327, 'MinSlot': 0}

2.6  API get_slot_info

2.6.1  get_slot_info

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 comware

>>> comware.get_slot_info(1)

【结果】

{'Slot': 1, 'Status': 'Normal', 'Chassis': 0, 'Role': 'Master', 'Cpu': 0}

 

不同款型规格的资料略有差异, 详细信息请向具体销售和400咨询。H3C保留在没有任何通知或提示的情况下对资料内容进行修改的权利!

新华三官网
联系我们