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.
# 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)
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"}. |
Return values of ops.cli.execute():
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