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

01 基础配置指导

目录

10-Python配置

本章节下载 10-Python配置  (145.88 KB)

docurl=/cn/Service/Document_Software/Document_Center/Switches/Catalog/S5130/S5130-HI/Configure/Operation_Manual/H3C_S5130-HI_CG-Release_1111-6W100/01/201506/876458_30005_0.htm

10-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.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, May 24 2014, 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='')

<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, May 24 2014, 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='')

<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表示输出执行结果,Flase表示不输出执行结果。缺省值为True。

【返回值】

CLI对象

【使用指导】

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

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

【举例】

# 使用API CLI添加本地用户test。

<Sysname> python

Python 2.7.3 (default, May 24 2014, 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')

【结果】

<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, May 24 2014, 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()

【结果】

['<Sysname>system-view', 'System View: return to User View with Ctrl+Z.', '[Sysname]local-user test class manage']

2.2  Transfer

2.2.1  Transfer

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

【原型】

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

<Sysname> python

Python 2.7.3 (default, May 24 2014, 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='')

【结果】

<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, May 24 2014, 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()

【结果】

'Timeout was reached'

2.3  API get_self_slot

2.3.1  get_self_slot

get_self_slot接口用来获取主设备的成员编号。

【命令】

get_self_slot()

【返回值】

返回一个列表对象,格式为:[-1,slot-number],其中slot-number表示主设备在IRF中的成员编号。

【举例】

# 使用API获取主设备所在的槽位号。

<Sysname> python

Python 2.7.3 (default, May 24 2014, 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,1]

2.4  API get_standby_slot

2.4.1  get_standby_slot

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, May 24 2014, 14:37:26)

[GCC 4.4.1] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import comware

>>> comware.get_standby_slot()

【结果】

[]

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

新华三官网
联系我们