In Figure 1, interface descriptions need to be automatically configured using scripts based on LLDP neighbor status when LLDP neighbors are added or deleted on SwitchA.
Make a Python script port_description_lldp.py to implement automatic configuration of interface descriptions based on LLDP neighbor status.
Log in to SwitchA remotely, upload the Python script to SwitchA, and install the script.
Configure a Python script assistant.
Subscribe to LLDP neighbor addition and deletion events.
Use environment variables to obtain the port of which an LLDP neighbor change, LLDP neighbor device name and neighbor port name of this port.
Configure the combination of the LLDP neighbor device name and neighbor port name as the description of a local port.
# Log in to SwitchA remotely, and upload the Python script from a PC to SwitchA. For details on how to upload a file, see "File Management" in the S2720, S5700, and S6700 V200R019C10 Configuration Guide - Basic Configuration.
<SwitchA> ops install file port_description_lldp.py
# Configure a Python script assistant and register the command line event in the script port_description_lldp.py to wait for the event to be triggered.
<SwitchA> system-view
[SwitchA] ops
[SwitchA-ops] script-assistant python port_description_lldp.py
[SwitchA-ops] quit
# After the preceding configuration is complete, for example, after neighbor information is added to GE0/0/1, the description of GE0/0/1 is configured as follows:
[SwitchA-GigabitEthernet0/0/1] display this # interface GigabitEthernet0/0/1 description To-SwitchB-GigabitEthernet0/0/1 # return
[SwitchA-GigabitEthernet0/0/1] undo lldp enable [SwitchA-GigabitEthernet0/0/1] display this # interface GigabitEthernet0/0/1 undo lldp enable # return
SwitchA configuration file
#
sysname SwitchA
#
ops
script-assistant python port_description_lldp.py
#
return
Example of the script port_description_lldp.py
# -*- coding: utf-8 -*- import ops # Import the OPS module. import sys # Import the SYS module. import os # Import the OS module. import re # Import the RE module, a regular expression. # Subscription processing function def ops_condition (ops): # Detect a neighbor addition event. In this type of event, neighbors can only be switches or routers. To subscribe to events of another type, use the following format. value1, err_str1 = ops.lldp.subscribe("add1", ops.lldp.LLDP_NEIGHBOR_EVENT_ADD, "INTERFACE_ALL", ops.lldp.LLDP_NEIGHBOR_TYPE_SWITCH) value11, err_str11 = ops.lldp.subscribe("add2", ops.lldp.LLDP_NEIGHBOR_EVENT_ADD, "INTERFACE_ALL", ops.lldp.LLDP_NEIGHBOR_TYPE_ROUTER) # Detect a neighbor deletion event. value2, err_str2 = ops.lldp.subscribe("delete1", ops.lldp.LLDP_NEIGHBOR_EVENT_DEL, "INTERFACE_ALL", ops.lldp.LLDP_NEIGHBOR_TYPE_SWITCH) value22, err_str21 = ops.lldp.subscribe("delete2", ops.lldp.LLDP_NEIGHBOR_EVENT_DEL, "INTERFACE_ALL", ops.lldp.LLDP_NEIGHBOR_TYPE_ROUTER) # Combination of neighbor addition and deletion events. The combination of a maximum of eight events is supported. value10, err_str10 = ops.correlate("add1 or add2 or delete1 or delete2") return 0 # Working processing function def ops_execute (ops): # Obtain the system environment variable _lldp_event, which indicates an event trigger type. key, value = ops.environment.get("_lldp_event") inter, value = ops.environment.get("_lldp_interface") if key == "OPR_TYPE_ADD": # Open the CLI channel. handle, err_desp = ops.cli.open() neighbor, n11, n21 = ops.cli.execute(handle,"display lldp neighbor interface " + inter) resultsys = re.search(r'System[\s]+name[\s:]*\S*', neighbor).group() sysname = re.split(':', resultsys) resultport = re.search(r'Port\s+ID\s{2,}:*\S*', neighbor).group() port = re.split(':', resultport) # Enter the system view. result, n11, n21 = ops.cli.execute(handle,"system-view") # Enter the interface view. result, n11, n21 = ops.cli.execute(handle,"interface " + inter) # Configure an interface description. result, n11, n21 = ops.cli.execute(handle,"description " + "To-" + sysname[1] + "-" + port[1]) # Close the CLI channel. result = ops.cli.close(handle) elif key == "OPR_TYPE_DEL": handle, err_desp = ops.cli.open() # Enter the system view. result, n11, n21 = ops.cli.execute(handle,"system-view") # Enter the interface view. result, n11, n21 = ops.cli.execute(handle,"interface " + inter) # Configure an interface description. result, n11, n21 = ops.cli.execute(handle,"undo description") # Close the CLI channel. result = ops.cli.close(handle) else: return 1 return 0