< Home

Example for Using a User-defined Command to Monitor Changes of Important Routes

Networking Requirements

In Figure 1, the user remotely manages a switch through a network and wants to use a user-defined command to monitor changes of important routes and generate logs to detect changes of important routes as soon as they occur.

Figure 1 Networking for important route change monitoring using a user-defined command

Configuration Roadmap

  1. Make Python scripts climuti.py and route_track.py to customize the command for monitoring changes of important routes and generate logs when important routes change.

  2. Log in to the switch remotely, upload the Python script to the switch, and install the script climuti.py.

  3. Configure a Python script assistant.

Procedure

  1. Make Python scripts.

    # Make Python scripts climuti.py and route_track.py to implement the following functions:
    • The script climuti.py defines the routetrack command to enable the function of monitoring changes of important routes and install the script route_track.py.

      To prevent failures to copy or delete Python script files, ensure that the command lines subscribed using ops.cli.subscribe are different from the names of Python script files.

    • The script route_track.py monitors the changes of routes and generates logs when routes change.

  2. Upload and install the Python script.

    # Log in to the switch remotely and upload the Python scripts 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 climuti.py

  3. Configure a Python script assistant.

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

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

  4. Verify the configuration.

    # After the preceding configurations are complete, run the routetrack command to enable the function of monitoring the changes of important routes and then check whether the script route_track.py has been installed successfully in the flash:/$_user/ directory of the switch.

    <Switch> routetrack
    <Switch> cd $_user
    <Switch> dir
    Directory of flash:/$_user/                                                       
                                                                                    
      Idx  Attr     Size(Byte)  Date        Time       FileName                     
        0  -rw-          1,672  Mar 22 2018 14:29:33   climuti.py              
        1  -rw-          2,000  Mar 22 2018 14:29:57   climuti.pyc             
        2  -rw-            441  Mar 22 2018 14:31:00   route_track.py              
        3  -rw-            891  Mar 22 2018 14:31:03   route_track.pyc             
                                                                                    
    1,927,468 KB total available (387,044 KB free)

    A .pyc file is generated after a Python script file is compiled. Whether the .pyc file is generated is irrelevant to the installation result of the corresponding Python script. Therefore, you do not need to focus on the .pyc file.

    A .pyc file cannot be deleted separately. It is automatically deleted when the ops uninstall file command is executed to uninstall the corresponding Python script.

    # Enable the log function. When changes of important routes are monitored, the switch automatically generates logs.

    <Switch> system-view
    [Switch] info-center enable
    [Switch] quit
    <Switch> terminal monitor
    <Switch> terminal logging
    <Switch>
    Mar 28 2018 14:29:17+08:00 Switch %%01OPSA/2/SCRIPT_LOG: OPS: Syslog: The important route changed. (user="route_track.py", session=964036020).

Configuration Files and Scripts

  • Configuration file of the switch

    #
    ops
     script-assistant python climuti.py
     script-assistant python route_track.py
    #
    return

    The script-assistant python route_track.py command is displayed in the configuration file only when the event subscribed in the climuti.py script is triggered by the routetrack command.

  • Example of the script climuti.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):
    	# User-defined command routetrack
    	value1, err_str1 = ops.cli.subscribe("cli1", "routetrack", enter=True)
    	# User-defined command no routetrack
    	value2, err_str2 = ops.cli.subscribe("cli2", "no routetrack", enter=True)
    	# Combined event, input the routetrack or no routetrack command.
    	value10, err_str10 = ops.correlate("cli1 or cli2")
    	return 0
    # Work processing function
    def ops_execute (ops):
    	# Obtain the system environment variable _cli_input, indicating the user input command.
    	key, value = ops.environment.get("_cli_input")
    	# When finding that the routetrack command is input, perform the following operations:
    	if key == "routetrack":
    		# Open the CLI channel.
    		handle, err_desp = ops.cli.open()
    		# Run the ops install file route_track.py command to install the script route_track.py.
    		result, n11, n21 = ops.cli.execute(handle,"ops install file route_track.py")
    		# Run the system-view command to enter the system view.
    		result, n11, n21 = ops.cli.execute(handle,"system-view")
    		# Run the ops command to enter the OPS view.
    		result, n11, n21 = ops.cli.execute(handle,"ops")
    		# Run the script-assistant python route_track.py command to configure an assistant for the script route_track.py.
    		result, n11, n21 = ops.cli.execute(handle,"script-assistant python route_track.py")
    		# Close the CLI channel.
    		result = ops.cli.close(handle)
    	# When finding that the no routetrack command is input, perform the following operations:
    	elif key == "no routetrack":
    		# Open the CLI channel.
    		handle, err_desp = ops.cli.open()
    		# Run the system-view command to enter the system view.
    		result, n11, n21 = ops.cli.execute(handle,"system-view")
    		# Run the ops command to enter the OPS view.
    		result, n11, n21 = ops.cli.execute(handle,"ops")
    		# Run the undo script-assistant python route_track.py command to delete the assistant for the script route_track.py.
    		result, n11, n21 = ops.cli.execute(handle,"undo script-assistant python route_track.py")
    		# Run the quit command to return to the system view.
    		result, n11, n21 = ops.cli.execute(handle,"quit")
    		# Run the quit command to return to the user view.
    		result, n11, n21 = ops.cli.execute(handle,"quit")
    		# Run the ops uninstall file route_track.py command to uninstall the script route_track.py.
    		result, n11, n21 = ops.cli.execute(handle,"ops uninstall file route_track.py")   
    		# Close the CLI channel.
    		result = ops.cli.close(handle)
    	else:
    		return 1
    	return 0
    
  • Example of the script route_track.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):
    	# Monitor changes of OSPF routes to network segment 10.20.1.0/24.
    	value, err_str = ops.route.subscribe("route1", "10.20.1.0", maskLen=24, optype="modify", protocol="ospf")
    	return 0
    # Work processing function
    def ops_execute (ops):
    	# Record user-defined critical user logs to notify route changes.
    	status, err_log = ops.syslog("Syslog: The important route changed.", "critical", "syslog")
    	return 0
    
Copyright © Huawei Technologies Co., Ltd.
Copyright © Huawei Technologies Co., Ltd.
Next topic >