NetEngine AR V300R019 CLI-based Configuration Guide - Device Management
Example for Using a Python Script to Provide a Warning Message
Networking Requirements
As shown in Figure 8-4, the administrator manages the router through a remote network and can upload a Python script to the router. Users want the system to display a warning message before a high-risk command is executed. This command can be executed only after you enter Y or y to confirm the execution of the command. Otherwise, this command cannot be executed, reducing the risk in executing the command.
Configuration Roadmap
Configure the routing address for the port.
Make a Python script riskwarning.py to display a warning message when a high-risk command needs to be executed.
Upload the Python script to the router, and install the script.
Configure a Python script assistant and register the subscription event in the Python script to enable the Python script to run automatically.
Procedure
- Configure the routing address for the port.
# Configure the routing address for the port.
<Huawei> system-view [Huawei] sysname Router [Router] interface GigabitEthernet 1/0/0 [Router-GigabitEthernet1/0/0] ip address 10.2.1.1 255.255.255.0 [Router-GigabitEthernet1/0/0] quit [Router] quit
- Make Python scripts.
# Make a Python script riskwarning.py to implement the following functions:
Register the command line event to trigger a warning message when a high-risk command needs to be executed.
The script can determine whether to continue to execute the high-risk command based on the user input.
- Upload and install the Python script.
# Upload the Python script from the PC to the router.
# Install the Python script to the router.
<Router> ops install file riskwarning.py
- Configure an assistant.
# Configure an assistant and register the command line event in the script riskwarning.py to wait for the event to be triggered.
<Router> system-view [Router] ops [Router-ops] script-assistant python riskwarning.py [Router-ops] quit
- Verify the configuration.
# After the preceding configurations are complete, run a specified risky command to check whether the system will display a warning message. The specified risky command in the current script is arp broadcast disable.
[Router] interface Ethernet 2/0/0.1 [Router-Ethernet2/0/0.1] dot1q termination vid 10 [Router-Ethernet2/0/0.1] arp broadcast disable This command may result in route flapping on the entire network, affecting services. Are you sure to continue?[Y/N]:
Currently, you can view prompt information displayed after scripts are executed only when you log in to the device through the console port.
Configuration Files and Example of the script
Router configuration file
# sysname Router # interface Ethernet2/0/0.1 dot1q termination vid 10 arp broadcast disable # interface GigabitEthernet1/0/0 ip address 10.2.1.1 255.255.255.0 # ops script-assistant python riskwarning.py # return
Example of the script riskwarning.py
# coding=utf-8 import ops # Import the OPS module. import sys # Import the sys module. # Subscription processing function def ops_condition (ops): print("\r\n user.py: enter ops_condition()") # Print information. value, err_str = ops.cli.subscribe("cli1", "arp broadcast disable", enter=True, sync=True, sync_wait=60) # Match the high-risk command arp broadcast disable. print("\r\n reg_cli.subscribe.value: %-15d"%(value)) print("\r\n reg_cli.subscribe.err_str: %s"%(err_str)) key, value = ops.environment.get("_phase") # Obtain the system environment variable _phase, indicating the phase running phase. print("\r\n key:%s"%(key)) print("\r\n value:%s"%(value)) return 0 # Work processing function def ops_execute (ops): key, value = ops.environment.get("_cli_vty") # Obtain the system environment variable _cli_vty, indicating the user channel. a, b = ops.terminal.write("This command may result in route flapping on the entire network, affecting services. Are you sure to continue?[Y/N]:",vty = key) # Display a warning message for the high-risk command on the terminal. print a print b a, b = ops.terminal.read(maxLen = 200,timeout = 30,vty=key) # Obtain the contents input by users from the terminal during interaction. # If Y or y is input, the system continues to executing the high-risk command arp broadcast disable. if a == "Y" or a == "y": return 1 # If other values are input, the system will not execute the high-risk command. else: return 0