When making a Python script, you can use OPS APIs supported by a device to develop functions.
# 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 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 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 S2720, S5700, and S6700 V200R019C10 Log Reference.