- Table of Contents
-
- 02-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-Python configuration
- 09-Tcl configuration
- Related Documents
-
Title | Size | Download |
---|---|---|
08-Python configuration | 89.50 KB |
Importing and using the extended Python API
Importing the entire extended API and using the API
Importing an extended API function and using the function
Example: Using a Python script for device configuration
Using Python
The information, including Python version, displayed after a device enters the Python shell varies by device model.
About Python
Comware 9 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 9 extended Python API."
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 comware
3. Execute an extended API function.
comware.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 6.2.0] on linux2
Type "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>
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 comware 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 6.2.0] on linux2
Type "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>
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 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')
# 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 9 extended Python API
The Comware 9 extended Python API is compatible with the Python syntax.
channel
Use channel to execute Comware 9 CLI commands and create channel objects.
Syntax
channel(command=‘’)
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 ;display interface brief | include UP’ to execute the display interface brief | include UP command.
Usage guidelines
This API function supports only Comware 9 commands. It does not support Linux, Python, or Tcl commands.
The API function is used for automated testing.
The list objects returned by the channel function might contain hexadecimal hidden characters such as 0x08. To hide the hexadecimal hidden characters, you can use the CLI function instead, or use the for loop to output the execution result of the channel function, for example:
<Sysname> python
>>> import comware
>>> c = comware.channel('display device')
>>> for value in c:
... print value
...
display device
Slot No. Brd Type Brd Status Subslot Sft Ver Patch Ver
1 Device-ABCDE Master 0 Device-ABCDE-1234 None
<Sysname>
Returns
Output from the executed commands, in the format of a list object.
Examples
# Display the device information.
<Sysname> python
>>> import comware
>>> comware.channel('display device')
Sample output
['display device', 'Slot No. Brd Type Brd Status Subslot Sft Ver Patch Ver', ' 1 Device-ABCDE Master 0 Device-ABCDE-1234 None ', '<Sysname>']
CLI
Use CLI to execute Comware 9 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 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.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.
<comware.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 6.2.0] on linux2
Type "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()
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 6.2.0] on linux2
Type "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
['<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
Use get_self_slot to get the slot number of the active MPU.
Syntax
get_self_slot()
Returns
The return is always [-1,-1]. A centralized device does not have an active MPU.
Examples
# Get the member ID of the master device or the slot number of the active MPU.
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_self_slot()
Sample output
[-1,-1]
get_slot_info
Use get_slot_info to get information about the device.
Syntax
get_slot_info(global-slot-number)
Parameters
global-slot-number: Specifies the global slot number of a card.
Returns
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.
Examples
# Get information about a slot.
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.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 slot number 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 slot number. The min-slot-number argument indicates the minimum slot number.
Examples
# Get the supported slot number range.
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_slot_range()
Sample output
{'MaxSlot': 1, 'MinSlot': 1}
get_standby_slot
Use get_standby_slot to get the slot number of the standby MPU.
Syntax
get_standby_slot()
Returns
The return is always [ ]. A centralized device does not have a standby MPU.
Examples
# Get the slot number of the standby MPU.
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.get_standby_slot()
Sample output
[]
Transfer
Use Transfer to download a file from a server.
Syntax
Transfer(protocol=‘’, host=‘’, source=‘’, dest=‘’,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.
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
Usage guidelines
The IP address of the remote server is an IPv6 address, the IP address must be enclosed with brackets ([]). For example:
Transfer("ftp","[2200::22]","0729.cfg","flash:/0729.cfg",user="abc",password="123456")
Examples
# Download file test.cfg from TFTP server 192.168.1.26.
<Sysname> python
Python 2.7.3 (default)
[GCC 6.2.0] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import comware
>>> comware.Transfer('tftp', '192.168.1.26', 'test.cfg', 'flash:/test.cfg', user='', password='')
Sample output
<comware.Transfer object at 0xb7f700e0>