- Table of Contents
-
- 01-Fundamentals Configuration Guide
- 00-Preface
- 01-CLI configuration
- 02-RBAC configuration
- 03-Login management configuration
- 04-FTP and TFTP configuration
- 05-File system management configuration
- 06-Configuration file management configuration
- 07-Software upgrade configuration
- 08-ISSU configuration
- 09-Emergency shell configuration
- 10-Automatic configuration
- 11-Device management configuration
- 12-Security zone configuration
- 13-Tcl configuration
- 14-Python configuration
- 15-Management with BootWare
- 16-License management
- Related Documents
-
Title | Size | Download |
---|---|---|
14-Python configuration | 95.28 KB |
Using Python
Comware 7 provides a built-in Python interpreter that supports the following items:
· Python 2.7 commands.
· Python 2.7 standard API.
· Comware 7 extended API. For more information about the Comware 7 extended API, see "Comware 7 extended Python API."
· Python scripts. You can use a Python script to configure the system.
Entering the Python shell
To use Python commands and APIs, you must enter the Python shell.
Command |
|
Enter the Python shell from user view. |
Executing a Python script
Execute a Python script in user view.
Command |
|
Execute a Python script. |
Python usage example
Network requirements
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
Usage procedure
# Use a text editor on the PC to edit Python script test.py as follows:
comware.Transfer('tftp', '192.168.1.26', 'main.cfg', 'cfa0:/main.cfg')
comware.Transfer('tftp', '192.168.1.26', 'backup.cfg', 'cfa0:/backup.cfg')
comware.CLI('startup saved-configuration cfa0:/main.cfg main ;startup saved-configuration cfa0:/backup.cfg backup')
# Use TFTP to download the script to the device.
<Sysname> tftp 192.168.1.26 get test.py
<Sysname> python cfa0:/test.py
<Sysname>startup saved-configuration cfa0:/main.cfg main
Please wait...... Done.
<Sysname>startup saved-configuration cfa0:/backup.cfg backup
Please wait...... Done.
Verifying the configuration
# Display startup configuration files.
Current startup saved-configuration file: cfa0:/startup.cfg
Next main startup saved-configuration file: cfa0:/main.cfg
Next backup startup saved-configuration file: cfa0:/backup.cfg
Comware 7 extended Python API
The Comware 7 extended Python API is compatible with the Python syntax.
Importing and using the Comware 7 extended Python API
To use the Comware 7 extended Python API, you must import the API to Python.
Use either of the following methods to import and use the Comware 7 extended Python API:
· Use import comware to import the entire API and use comware.API to execute an API.
For example, to use the extended API Transfer to download the test.cfg file from TFTP server 192.168.1.26:
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', 'cfa0:/test.cfg', user='', password='')
<comware.Transfer object at 0x77e2a0e0>
· Use from comware import API to import an API and use API to execute the API.
For example, to use the extended API Transfer to download the test.cfg file from TFTP server 192.168.1.26:
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', 'cfa0:/test.cfg', user='', password='')
<comware.Transfer object at 0x77e2a090>
Comware 7 extended Python API functions
CLI class
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 supports only Comware commands. It does not support Linux, Python, or Tcl commands.
Returns
CLI objects
Examples
# Add a local user with the username test.
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')
System View: return to User View with Ctrl+Z.
[Sysname] local-user test class manage
New local user added.
<comware.CLI object at 0x77e2a0e0>
get_output
Use get_output to get the output from executed commands.
Syntax
Returns
Output from executed commands
Examples
# Add a local user and get the output from the command.
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()
Sample output
Transfer class
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:
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.
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 the test.cfg file from TFTP server 192.168.1.26.
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', 'cfa0:/test.cfg', user='', password='')
Sample output
<comware.CLI object at 0x77e2a0e0>
get_error
Use get_error to get the error information from the download operation.
Syntax
Returns
Error information (if there is no error information, None is returned)
Examples
# Download the test.cfg file from TFTP server 1.1.1.1 and get the error information from the operation.
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', 'cfa0:/test.cfg', user='', password='')
>>> c.get_error()
Sample output
API get_self_slot
get_self_slot
Use get_self_slot to get the slot number of the active MPU. (In standalone mode.)
Use get_self_slot to get the slot number of the global active MPU. (In IRF mode.)
Syntax
Returns
A list object in the format of [-1,slot-number]. The slot-number indicates the slot number of the active MPU. (In standalone mode.)
Examples
# Get the slot number of the active MPU.
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()
API get_standby_slot
get_standby_slot
Use get_standby_slot to get the slot number of the standby MPU. (In standalone mode.)
Use get_standby_slot to get the slot numbers of the global standby MPUs. (In IRF mode.)
Syntax
Returns
A list object in the format of [[-1,slot-number]]. The slot-number indicates the slot number of a standby MPU. If the device does not have a standby MPU, [ ] is returned. (In standalone mode.)
A list object in one of the following formats:
· [ ]—The IRF fabric does not have a global standby MPU.
· [[chassis-number,slot-number]]—The IRF fabric has only one global standby MPU.
· [[chassis-number1,slot-number1],[chassis-number2,slot-number2],…]—The IRF fabric has multiple standby MPUs.
The chassis-number and slot-number arguments indicate the device member IDs and slot numbers of the global standby MPUs. (In IRF mode.)
Examples
# Get the slot number of the standby MPU.
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()
Sample output
API get_slot_range
get_slot_range
Use get_slot_range to get the supported slot number range.
get_slot_range()
A dictionary object in the format of {'MaxSlot': max-slot-number, 'MinSlot': min-slot-number }. The max-slot-number argument indicates the maximum slot number. The min-slot-number argument indicates the minimum slot number.
# Get the supported slot number range.
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': 19, 'MinSlot': 0}
API get_slot_info
get_slot_info
Use get_slot_info to get information about a card.
get_slot_info()
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 slot number. The status argument indicates the status of the card. The chassis-number argument indicates the member ID of the device. The role argument indicates the role of the card. The CPU-number argument indicates the ID of the main CPU.
# Get information about a slot.
<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}