Subscribe to CLI Events

Function Description

After you subscribe to CLI events, if the entered character strings of a CLI match the regular expression, the system executes the ops_execute() function in the Python script.

The OPS allows the system to use the Python script to open/close a CLI channel and execute commands.

Command Prototype

# Subscribe to a CLI event.

opsObj.cli.subscribe(tag, pattern, enter=False, sync=True, async_skip=False, sync_wait=30)

This API can only be used in the ops_condition() function of the maintenance assistant script.

# Open a CLI channel.

opsObj.cli.open()

# Execute a command.

ops.cli.execute(fd, command, choice=None)

# Close a CLI channel.

ops.cli.close(fd)

Parameter Description

Table 1 describes parameters supported by CLI event subscription APIs.
Table 1 Parameters supported by CLI event subscription APIs

Method

Description

opsObj

Specifies an OPS object. It is obtained through ops.ops() instantiation.

tag

Specifies a condition ID. The value is a string of 1 to 8 case-sensitive characters that starts with a letter and contains letters, digits, and underscores (_). Enter double quotation marks ("") or None for the only one condition. tag cannot be set to and, or, or andnot.

pattern

Specifies a regular expression for matching commands.

enter

The value can be True or False. True indicates that the regular expression is matched immediately after you press Enter. False indicates that a regular expression is matched after the keyword is supplemented.

sync

Indicates whether the CLI terminal waits for script execution after a command event is triggered. True indicates waiting, and False indicates not waiting.

async_skip

The value can be True or False, indicating whether the original command is skipped. (This setting takes effect only when sync is set to False.) True indicates that the original command is not executed, and False indicates that the original command is executed.

sync_wait

The value is an integer ranging from 1 to 100, indicating the time during which the CLI terminal waits for script execution. (This setting takes effect only when sync is set to True.)

fd

Specifies a CLI channel handle. It is generated using ops.cli.open().

command

Specifies a command to be executed. For example, system-view im. You do not need to press Enter; the CLI automatically adds a carriage return. The value can only be one command.

choice

Specifies a lexical type, used for auto reply for interactive commands. choice = {"Continue?": "n", "save": "n"}. A maximum of eight options are supported. Multiple lines are entered for multi-line commands, such as header login information. For example, choice={"": "a\r\nb\r\n\a"}.

Description of Return Values

Return values of opsObj.cli.subscribe() and ops.cli.close():
  • First return value: The value 0 indicates a success, and the value 1 indicates a failure.
  • Second return value: This value describes success or failure reasons, expressed in a character string.
Return values of opsObj.cli.open():
  • First return value: None indicates an error, and other values indicate command handles.
  • Second return value: result description expressed in a character string.

Return values of ops.cli.execute():

  • First return value: If None is returned, the command fails to be sent to the CLI or command execution times out. Otherwise, the command output is returned. Each data package is 32 KB in size, separated at a carriage return.
  • Second return value: If Next is 0, there is no more output. If Next is 1, more output will be displayed. This function is still called for obtaining the next batch of data, except that you must set command=None and choice=None.
  • Third return value: result description expressed in a character string.

Example

test.py

import ops
import igpcomm

def ops_condition(_ops):
    _ops.cli.subscribe("con11","logbuffer1",True,True,False,10)
    _ops.correlate("con11")
    return ret

def ops_execute(_ops):
    handle, err_desp= _ops.cli.open()
    choice = {"Continue": "y", "save": "n"}
    _ops.cli.execute(handle,"sys")
    _ops.cli.execute(handle,"pm",None)
    _ops.cli.execute(handle,"undo statistics-task a",choice)
    _ops.cli.execute(handle,"commit",None)
    ret = _ops.cli.close(handle)
    print 'test2 =',ret
    return 0
  1. When the front end executes the script, the CLI channel is opened, and the CLI terminal displays the user view.
  2. Run the system-view command to enter the system view.
  3. Run the pm command to enter the PM view.
  4. Run the undo statistics-task a command, which is an interactive command. The system then automatically interacts based on the choice variable value.
  5. Run the commit command to commit the configuration.
  6. Close the CLI channel.
  • After the CLI channel is opened using the script, commands can be delivered to the device only when the CLI terminal displays the user view.
  • The CLI channel privileges are inherited from the user authorities of the maintenance assistant created.
  • A script can be used to create only one CLI channel. If an attempt is made to create a second CLI channel using this script, the system returns a failure.
  • A VTY resource is consumed for every channel opened. The display users command shows that a VTY resource is consumed by an assistant (Assistant: Name). When only three or less VTY resources are available, opening a channel fails.
Copyright © Huawei Technologies Co., Ltd.
Copyright © Huawei Technologies Co., Ltd.
Next topic >