< Home

Example for Providing a Warning Message According to User Input

Networking Requirements

As shown in Figure 1, the user remotely manages a switch through a network and wants to add a warning message before high-risk commands are executed. The command is only executed after the user confirms the operation, reducing the risk of accidental execution of the command.

Figure 1 Networking for providing a warning message according to user input

Configuration Roadmap

  1. Configure the IP address of the interface as the management IP address of the switch.

  2. Make a Python script riskwarning.py to display a warning message when a high-risk command needs to be executed.

  3. Log in to the switch remotely, upload the Python script to the switch, and install the script.

  4. Configure a Python script assistant.

Procedure

  1. Make a Python script.

    # 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.

    • Determine whether to execute the high-risk command based on the user input.

  2. Upload and install the Python script.

    # Log in to the switch remotely and upload the Python script from the PC to the switch. For details about how to upload a file, see File Management in S2720, S5700, and S6700 V200R019C10 Configuration Guide - Basic Configuration.

    # Install the Python script on the switch.

    <Switch> ops install file riskwarning.py

  3. Configure a Python script assistant.

    # Configure a Python script assistant and register the CLI event in the script riskwarning.py. Wait for the event to be triggered.

    <Switch> system-view
    [Switch] ops
    [Switch-ops] script-assistant python riskwarning.py
    [Switch-ops] quit

  4. Verify the configuration.

    # After the preceding configurations are complete, run the specified command to check whether the system will display a warning message. The command specified in the current script is arp learning multicast enable and is executed in the system view.

    [Switch] arp learning multicast enable
    The device may be attacked by ARP attack packets with multicast MAC addresses. Are you sure to continue?[Y/N]:

    If you enter N, the arp learning multicast enable command is not executed. If you enter the display current-configuration | include arp command in any view, the command output does not display the arp learning multicast enable command.

    If you enter Y, the arp learning multicast enable command is executed. If you enter the display current-configuration | include arp command in any view, the command output displays the arp learning multicast enable command.

    To view the information displayed by print statements in OPS scripts, log in to the switch through the serial port.

Configuration Files and Scripts

  • Configuration file of the switch

    #
    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.
    import os          # Import the OS module.
    # Subscription processing function
    def ops_condition (ops):
    	# Match the high-risk command arp learning multicast enable.
    	value, err_str = ops.cli.subscribe("cli1", "arp learning multicast enable", enter=True, sync=True, sync_wait=60)
    	return 0
    # Work processing function
    def ops_execute (ops):
    	# Obtain the system environment variable _cli_vty, indicating the user channel.
    	key, value = ops.environment.get("_cli_vty")
    	# Display a warning message for the high-risk command on the terminal.
    	a, b = ops.terminal.write("The device may be attacked by ARP attack packets with multicast MAC addresses. Are you sure to continue?[Y/N]:", vty=key)
    	print a
    	print b
    	# Read the user input in the interaction between terminals.
    	a, b = ops.terminal.read(maxLen=200, timeout=30, vty=key)
    	# If Y or y is entered, the system continues to executing the high-risk command arp learning multicast enable.
    	if a == "Y" or a == "y":
    		return 1
    	# If other values are entered, the system will not execute the high-risk command.
    	else:
    		return 0
Copyright © Huawei Technologies Co., Ltd.
Copyright © Huawei Technologies Co., Ltd.
< Previous topic Next topic >