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

01-基础配置指导

目录

11-Python配置

本章节下载 11-Python配置  (160.18 KB)

docurl=/cn/Service/Document_Software/Document_Center/Switches/Catalog/S5560/S5560X-EI/Configure/Operation_Manual/H3C_S5560X-EI_CG-R1118[1118P07]-6W101/01/201809/1108837_30005_0.htm

11-Python配置


1 Python

1.1  Python简介

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

在Comware V7系统上可以采用如下方式使用Python:

·     通过执行Python脚本进行自动化配置系统。

·     进入Python shell,使用Python2.7版本的命令、标准API或扩展API对设备进行配置。其中,扩展API是Comware对Python进行的扩展,用来方便用户进行系统配置。关于Comware的Python扩展,可以参考“2 Comware扩展Python API”。

1.2  执行Python脚本文件

请在用户视图下执行本命令,执行Python脚本文件。

python filename

1.3  进入Python shell

请在用户视图下执行本命令,进入Python shell。

python

1.4  导入Comware包以使用扩展API

用户如需使用扩展Python API,必须先导入Comware包。导入时,可选择导入整个Comware包或单个API。

1.4.1  导入整个Comware包并执行扩展API

1. 配置步骤

(1)     请在用户视图下执行本命令,进入Python shell。

python

(2)     导入整个Comware包。

import comware

(3)     执行扩展API。

comware.api

2. 配置举例

# 下例采用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>

1.4.2  导入单个API函数并执行该函数

1. 配置步骤

(1)     请在用户视图下执行本命令,进入Python shell。

python

(2)     导入单个API函数。

from comware import api-name

(3)     执行扩展API函数。

api-function

2. 配置举例

# 下例采用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>

1.5  退出Python shell

请在Python shell下执行本命令,退出Python shell。

exit()

1.6  Python典型配置举例

1.6.1  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语言语法。

2.1  CLI

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

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

【命令】

get_self_slot()

【返回值】

返回一个列表对象,格式为:[-1,slot-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.5  get_slot_info

get_slot_info接口用来获取指定成员设备的信息。

【命令】

get_slot_info()

【返回值】

返回一个字典对象,返回值始终为{'Slot': slot-number, 'Status': 'status', 'Chassis': chassis-number, 'Role': 'role', 'Cpu': CPU-number }。slot-number表示备在IRF中的成员编号,status表示成员设备的状态,chassis-number取值固定为0,role表示成员设备的角色,CPU-number取值固定为0。

【举例】

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

2.6  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': 10, 'MinSlot': 1}

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

[GCC 4.4.1] on linux2

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

>>> import comware

>>> comware.get_standby_slot()

【结果】

[[-1, 1], [-1, 2]]

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

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

新华三官网
联系我们