01-Fundamentals Configuration Guide

HomeSupportConfigure & DeployConfiguration GuidesH3C MSR1000[2600][3600] Routers Configuration Guides(V9)-R9119-6W10001-Fundamentals Configuration Guide
12-Python configuration
Title Size Download
12-Python configuration 101.19 KB

Using Python

About Python

Comware 7 provides a built-in Python interpreter. You can use Python to perform the following tasks:

·     Execute Python scripts to implement automatic device configuration.

·     Enter Python shell to configure the device by using the following items:

¡     Python 2.7 commands.

¡     Python 2.7 standard API.

¡     Extended API. For more information about the extended API, see "Comware 7 extended Python API."

Restrictions: Hardware compatibility with Python

Hardware

Python compatibility

MSR810, MSR810-W, MSR810-W-DB, MSR810-LM, MSR810-W-LM, MSR810-10-PoE, MSR810-LM-HK, MSR810-W-LM-HK, MSR810-LM-CNDE-SJK, MSR810-CNDE-SJK

Yes

MSR810-LMS, MSR810-LUS

No

MSR810-LMS-EA, MSR810-LME

Yes

MSR1004S-5G

Yes

MSR2600-6-X1, MSR2600-10-X1, MSR2600-15-X1

Yes

MSR 2630

Yes

MSR3600-28, MSR3600-51

Yes

MSR3600-28-SI, MSR3600-51-SI

No

MSR3600-28-X1, MSR3600-28-X1-DP, MSR3600-51-X1, MSR3600-51-X1-DP

Yes

MSR3610-I-DP, MSR3610-IE-DP, MSR3610-IE-ES, MSR3610-IE-EAD, MSR-EAD-AK770, MSR3610-I-IG, MSR3610-IE-IG

Yes

MSR3610-X1, MSR3610-X1-DP, MSR3610-X1-DC, MSR3610-X1-DP-DC, MSR3620-X1, MSR3640-X1

Yes

MSR 3610, MSR 3620, MSR 3620-DP, MSR 3640, MSR 3660

Yes

MSR3610-G, MSR3620-G

Yes

MSR3640-X1-HI

Yes

Hardware

Python compatibility

MSR810-W-WiNet, MSR810-LM-WiNet

Yes

MSR830-4LM-WiNet

Yes

MSR830-5BEI-WiNet, MSR830-6EI-WiNet, MSR830-10BEI-WiNet

Yes

MSR830-6BHI-WiNet, MSR830-10BHI-WiNet

Yes

MSR2600-6-WiNet , MSR2600-10-X1-WiNet

Yes

MSR2630-WiNet

Yes

MSR3600-28-WiNet

Yes

MSR3610-X1-WiNet

Yes

MSR3610-WiNet, MSR3620-10-WiNet, MSR3620-DP-WiNet, MSR3620-WiNet, MSR3660-WiNet

Yes

Hardware

Python compatibility

MSR2630-XS

Yes

MSR3600-28-XS

Yes

MSR3610-XS

Yes

MSR3620-XS

Yes

MSR3610-I-XS

Yes

MSR3610-IE-XS

Yes

MSR3620-X1-XS

Yes

MSR3640-XS

Yes

MSR3660-XS

Yes

 

Hardware

Python compatibility

MSR810-LM-GL

Yes

MSR810-W-LM-GL

Yes

MSR830-6EI-GL

Yes

MSR830-10EI-GL

Yes

MSR830-6HI-GL

Yes

MSR830-10HI-GL

Yes

MSR1004S-5G-GL

Yes

MSR2600-6-X1-GL

Yes

MSR3600-28-SI-GL

No

Executing a Python script

To execute a Python script, use the following command in user view:

python filename

Entering the Python shell

To enter the Python shell from user view, execute the following command:

python

Importing and using the extended Python API

To use the extended Python API, you must first import the API to Python.

Importing the entire extended API and using the API

Procedure

1.     Enter the Python shell from user view.

python

2.     Import the entire extended API.

import platformtools

3.     Execute an extended API function.

platformtools.api

Example

# Use extended API function Transfer to download the test.cfg file from TFTP server 192.168.1.26.

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

Importing an extended API function and using the function

Procedure

1.     Enter the Python shell from user view.

python

2.     Import an extended API function.

from platformtools import api-name

3.     Execute an extended API function.

api-function

Example

# Use extended API function Transfer to download the test.cfg file from TFTP server 192.168.1.26.

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

Exiting the Python shell

To exit the Python shell, execute the following command in the Python shell.

exit()

Python usage examples

Example: Using a Python script for device configuration

Network configuration

Use a Python script to perform the following tasks:

·     Download configuration files main.cfg and backup.cfg to the device.

·     Configure the files as the main and backup configuration files for the next startup.

Figure 1 Network diagram

Procedure

# Use a text editor on the PC to configure Python script test.py as follows:

#!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')

# Use TFTP to download the script to the device.

<Sysname> tftp 192.168.1.26 get test.py

# Execute the script.

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

Verifying the configuration

# Display startup configuration files.

<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 7 extended Python API

 

The Comware 7 extended Python API is compatible with the Python syntax.

CLI

Use CLI to execute Comware 7 CLI commands and create CLI objects.

Syntax

CLI(command=‘’, do_print=True)

Parameters

command: Specifies the commands to be executed. To enter multiple commands, use a space and a semicolon (;) as the delimiter. To enter a command in a view other than user view, you must first enter the commands used to enter the view. For example, you must enter ’system-view ;local-user test class manage’ to execute the local-user test class manage command.

do_print: Specifies whether to output the execution result:

·     True—Outputs the execution result. This value is the default.

·     False—Does not output the execution result.

Usage guidelines

This API function supports only Comware commands. It does not support Linux, Python, or Tcl commands.

Returns

CLI objects

Examples

# Add a local user named 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')

Sample output

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

get_error

Use get_error to get the error information from the download operation.

Syntax

Transfer.get_error()

Returns

Error information (if there is no error information, the return is null)

Examples

# Download file test.cfg from TFTP server 1.1.1.1 and get the error information from the operation.

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

Sample output

"Timeout was reached”

get_output

Use get_output to get the output from executed commands.

Syntax

CLI.get_output()

Returns

Output from executed commands

Examples

# Add a local user and get the output from the command.

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

Sample 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

(In standalone mode.) Use get_self_slot to get the slot number of the active MPU.

(In IRF mode.) Use get_self_slot to get the member ID of the master device.

Syntax

get_self_slot()

Returns

(In standalone mode.) The return is always [-1,-1]. A centralized device does not have an active MPU.

(In IRF mode.) A list object in the format of [-1,slot-number]. The slot-number indicates the member ID of the master device.

Examples

# (In IRF mode.) Get the member ID of the master device. 

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

Sample output

[-1,0]

get_slot_info

(In standalone mode.) Use get_slot_info to get information about the device.

(In IRF mode.) Use get_slot_info to get information about a member device.

Syntax

get_slot_info()

Returns

(In standalone mode.) A dictionary object in the format of {'Slot': slot-number, 'Status': 'status', 'Chassis': chassis-number, 'Role': 'role', 'Cpu': CPU-number }. The slot-number, chassis-number, and CPU-number arguments are fixed at 0. The status argument indicates the status of the device. The role argument indicates the role of the device.

(In IRF mode.) A dictionary object in the format of {'Slot': slot-number, 'Status': 'status', 'Chassis': chassis-number, 'Role': 'role', 'Cpu': CPU-number }. The slot-number argument indicates the member ID of the device. The status argument indicates the status of the member device. The chassis-number and CPU-number arguments are fixed at 0. The role argument indicates the role of the member device.

Examples

# Get information about the device, a card, or a member device.

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

Sample output

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

get_slot_range

Use get_slot_range to get the supported IRF member ID range.

Syntax

get_slot_range()

Returns

A dictionary object in the format of {'MaxSlot': max-slot-number, 'MinSlot': min-slot-number }. The max-slot-number argument indicates the maximum member ID. The min-slot-number argument indicates the minimum member ID.

Examples

# Get the supported IRF member ID range.

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

Sample output

{'MaxSlot': 0, 'MinSlot': 0}

get_standby_slot

(In standalone mode.) Use get_standby_slot to get the slot number of the standby MPU.

(In IRF mode.) Use get_standby_slot to get the member IDs of the subordinate devices.

Syntax

get_standby_slot()

Returns

(In standalone mode.) The return is always [ ]. A centralized device does not have a standby MPU.

(In IRF mode.) A list object in one of the following formats:

·     [ ]—The IRF fabric does not have a subordinate device.

·     [[-1,slot-number]]—The IRF fabric has only one subordinate device.

·     [[-1,slot-number1],[-1,slot-number2],...]—The IRF fabric has multiple subordinate devices.

The slot-number arguments indicate the member IDs of the subordinate devices.

Examples

# (In IRF mode.) Get the member IDs of the subordinate devices.

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

Sample output

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

Transfer

Use Transfer to download a file from a server.

Syntax

Transfer(protocol=‘’, host=‘’, source=‘’, dest=‘’, vrf=‘’,login_timeout=10, user=‘’, password=‘’)

Parameters

protocol: Specifies the protocol used to download a file:

·     ftp—Uses FTP.

·     tftp—Uses TFTP.

·     http—Uses HTTP.

host: Specifies the IP address of the remote server.

source: Specifies the name of the file to be downloaded from the remote server.

dest: Specifies a name for the downloaded file.

vrf: Specifies the MPLS L3VPN instance to which the remote server belongs. This argument represents the VPN instance name, a case-sensitive string of 1 to 31 characters. If the server belongs to the public network, do not specify this argument.

login_timeout: Specifies the timeout for the operation, in seconds. The default is 10.

user: Specifies the username for logging in to the server.

password: Specifies the login password.

Returns

Transfer object

Examples

# Download file test.cfg from TFTP server 192.168.1.26.

<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='')

Sample output

<platformtools.Transfer object at 0xb7f700e0>

send

Use send to generate and send a log message.

Syntax

SYSLOG.send(digest=‘’, info=‘’, priority=‘’)

Parameters

digest: Specifies a mnemonic for the log message. The mnemonic is a case-sensitive string of 1 to 32 characters.

info: Specifies the content of the log message, a case-sensitive string of 1 to 1024 characters.

priority: Specifies a priority for the log message, in the range of 0 to 4294967295.

Returns

None.

Usage guidelines

You must use the SYSLOG function to create a syslog object before you can generate and send a log message.

To display the generated log messages on the current terminal, first execute the terminal monitor command in user view.

For more information about the log message format, see information center configuration in Network Management and Monitoring Configuration Guide.

Examples

# Enable the current terminal to display log messages, and then generate and send a log message for the SNMP module. Set the mnemonic to Test, the content to Try to send one message., and the priority to 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)

Sample output

>>> %Jan  1 06:24:17:908 2019 Sysname SNMP/0/Test: Try to send one message.

SYSLOG

Use SYSLOG to create a syslog object.

Syntax

SYSLOG(module=‘’)

Parameters

module: Specifies the name of the feature module for which the Syslog object is created. The name is a case-insensitive string of 1 to 8 characters.

Returns

None.

Usage guidelines

You must create a syslog object for a feature module before you can generate and send a log message for that object.

Examples

# Create a syslog object named SNMP and assign the object to variable 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

Sample output

<platformtools.SYSLOG object at 0xb7e180e0>

  • Cloud & AI
  • InterConnect
  • Intelligent Computing
  • Security
  • SMB Products
  • Intelligent Terminal Products
  • Product Support Services
  • Technical Service Solutions
All Services
  • Resource Center
  • Policy
  • Online Help
All Support
  • Become a Partner
  • Partner Resources
  • Partner Business Management
All Partners
  • Profile
  • News & Events
  • Online Exhibition Center
  • Contact Us
All About Us
新华三官网