No relevant resource is found in the selected language.

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Read our privacy policy>Search

Reminder

To have a better experience, please upgrade your IE browser.

upgrade

S600-E V200R013C00 Configuration Guide - Device Management

This document describes the configurations of Device Management, including device status query, hardware management, Stack, SVF, cloud-based management, PoE, OPS, energy-saving management, information center, fault management, NTP.
Rate and give feedback:
Huawei uses machine translation combined with human proofreading to translate this document to different languages in order to help you better understand the content of this document. Note: Even the most advanced machine translation cannot match the quality of professional translators. Huawei shall not bear any responsibility for translation accuracy and it is recommended that you refer to the English document (a link for which has been provided).
Making a Python Script

Making a Python Script

Context

When making a Python script, you can use OPS APIs supported by a device to develop functions.

Procedure

  1. Create a text and modify its file name extension to .py as a Python script.
  2. Edit the script according to the Python script format.

    # Declare that this Python script file uses utf-8 as source code encoding.
    # -*- coding: utf-8 -*-
    
    # Fixed statement for importing the OPS module. You can use OPS APIs supported by the device in the script only after the OPS module is imported. For details, see OPS API List.
    import ops
    # Fixed statement for importing the sys module. The sys module is responsible for interaction between programs and the built-in Python interpreter of the device and provides a series of functions and variables. After the sys module is imported, you can use these functions and variables. For details about functions and variables, see official Python documents.
    import sys
    # Fixed statement for importing the OS module. The OS module is responsible for interaction between programs and the operating system and provides interfaces for accessing the bottom layer of the operating system. After the OS module is imported, you can use these interfaces. For details about these interfaces, see official Python documents.
    import os
    
    # Fixed statement for defining the subscription processing function ops_condition. This function is invoked when the script assistant is configured and is used to subscribe to events. It is scheduled and executed by the built-in framework script _frame.py of the device.
    # The input parameter of the function ops_condition is the OPS object created in _frame.py. You can perform data access under this object.
    def ops_condition(ops):
    	# The current phase is the subscription phase. To use OPS APIs applicable to the subscription phase, see Table 7-1 in OPS APIs Supported by a Device. The following uses Timer Event Subscription as an example.
    	# Use the ops.timer.cron API to subscribe to a timer identified by t1. It indicates that the specified action in the execution phase is triggered at 6:00 every Monday. You can modify the timer as required.
    	# If the input parameter value is a character string, you need to enclose the string in double quotation marks.
    	# The status and err_str are user-defined script variables, indicating the first and second return values of the ops.timer.cron API, respectively. Typically, return values can be displayed using the print statement in the debugging phase. This helps you view debugging information and locate faults.
    	# For OPS APIs, you can determine whether return values are required based on actual requirements. If return values are required, the number of return values must be specified based on the OPS API prototype. The meaning of return values varies depending on the OPS API. For details, see the corresponding OPS API.
    	# If the first return value of the ops.timer.cron API is a digit, 0 indicates that the API is successfully executed and 1 indicates that the API fails to be executed. The second return value is returned only when the first return value is 1. If the second value is a character string, the cause of the execution failure is described.
    	status, err_str = ops.timer.cron("t1", "0 6 * * 1")
    	# Specify the return value of a function. The return value of a function can be used as the processing result. You can also check the returned processing result using the function result. (For details, see Returning Event Execution Results.)
    	# The return value of a function is used as the output of the function and can be assigned to other variables as the input of other functions.
    	# The return value of the function ops_condition is 0, indicating that this function is successfully executed.
    	return 0
    
    # Fixed statement for defining the execution processing function ops_execute. This function is invoked when the script event is executed and is used to execute the action. It is scheduled and executed by the built-in framework script _frame.py of the device.
    # The input parameter of the function ops_execute is the OPS object created in _frame.py. You can perform data access under this object.
    def ops_execute(ops):
    	# The current phase is the execution phase. To use OPS APIs applicable to the execution phase, see Table 7-1 in OPS APIs Supported by a Device. The following uses Opening CLI Channel, Executing CLI Command, and Closing CLI Channel as an example.
    	# Open the CLI channel. Commands can be executed only after the CLI channel is opened. After commands are executed, you need to close the CLI channel.
    	# The handle and err_desp are user-defined script variables, indicating the first and second return values of the ops.cli.open API, respectively. Typically, return values can be displayed using the print statement in the debugging phase. This helps you view debugging information and locate faults. 
    	# The first return value of the Opening CLI Channel API is used as the input parameter for the Executing CLI Command and Closing CLI Channel APIs. Therefore, the return value must be specified when the Opening CLI Channel API is used.
    	handle, err_desp = ops.cli.open()
    	# Run the display interface gigabitethernet 0/0/1 command.
    	# If the return value of result is None, the command fails to be sent to the CLI or the command execution times out. If the return value is not None, the command executed on the CLI is displayed. If the return value n11 is Next:0, there is no output information later. If the return value is Next:1, there will be output information later. The return value n12 is displayed only when the value of result is None, indicating the cause of the command execution failure.
    	result1, n11, n12 = ops.cli.execute(handle, "display interface gigabitethernet 0/0/1")
    	# Run the display current-configuration interface gigabitethernet 0/0/1 command.
    	result2, n21, n22 = ops.cli.execute(handle, "display current-configuration interface gigabitethernet 0/0/1")
    	# Close the CLI channel after the command execution is complete.
    	result = ops.cli.close(handle)
    	# Record the output of the display interface gigabitethernet 0/0/1 command into logs. You can check corresponding information in the logs.
    	log1, descri_str1 = ops.syslog(result1, "informational", "syslog")
    	# Record the output of the display current-configuration interface gigabitethernet 0/0/1 command into logs. You can check corresponding information in the logs.
    	log2, descri_str2 = ops.syslog(result2, "informational", "syslog")
    	# Specify the return value of a function. The return value of a function can be used as the processing result. You can also check the returned processing result using the function result. (For details, see Returning Event Execution Results.)
    	# The return value of a function is used as the output of the function and can be assigned to other variables as the input of other functions.
    	# The return value of the function ops_execute is 0, indicating that this function is successfully executed.
    	return 0
    • A Python script involves three parts: importing the Python module, ops_condition(ops) subscription function, and ops_execute(ops) execution function.

    • For the subscription function in the preceding script example, the time (06:00 on each Monday) is specified for the subscribed event using the timer. When the specified time reaches, the action defined in the execution function is triggered, namely, the device is triggered to run the display interface gigabitethernet 0/0/1 and display current-configuration interface gigabitethernet 0/0/1 commands. In this case, GE0/0/1 can be periodically checked. The command output is recorded into logs using the ops.syslog API. You can log in to the device through the control interface or Telnet, and run the display logbuffer command to check the content saved in the log buffer. To save logs on a device, export logs to the log server using the syslog protocol.

    • No matter whether OPS APIs used in a Python script are successfully executed, corresponding logs are recorded on the device. For details about corresponding logs, see OPSA in S600-E V200R013C00 Log Reference.

  3. Save the Python script.
Translation
Download
Updated: 2019-04-20

Document ID: EDOC1100066159

Views: 26089

Downloads: 3

Average rating:
This Document Applies to these Products
Related Version
Related Documents
Share
Previous Next