The OPS provides an API for outputting prompt information to the CLI terminal and reading user input from the CLI terminal when the CLI terminal is waiting for CLI event synchronization.
# Output prompt information to the CLI terminal.
opsObj.terminal.write(msg, vty=None, fgrd=False)
# Read user input from the CLI terminal.
opsObj.terminal.read(maxLen=512, timeout=30, vty=None)
Method |
Description |
---|---|
opsObj |
Specifies an OPS object. It is obtained through ops.ops() instantiation. |
msg |
Specifies a character string to be displayed on user terminals. |
vty |
Specifies a user terminal. Currently, messages can only be displayed on user terminals that wait for script execution or execute the script on the front end. You can enter environment('_cli_vty') to obtain the VTY name or enter None. |
fgrd |
Boolean value, which indicates whether to display prompt information on the login pages of all users. Only when fgrd is set to True in the system script, the prompt information is displayed on all terminals. If a common script is used, the prompt information is displayed only on the current terminal. |
maxLen |
Specifies the maximum number of characters allowed to be input. The default value is 512 characters. |
timeout |
Specifies a timeout period for waiting for user inputs. The default value is 30s. |
Return values of opsObj.terminal.read():
# When the front end executes the script, the Python script outputs "Hello World!" to the CLI terminal.
test.py import ops def ops_condition(_ops): ret, reason = _ops.cli.subscribe("corn1","device",True,True,False,20) return ret def ops_execute(_ops): _ops.terminal.write("Hello world!",None,False) return 1
# When the front end executes the script, the character string entered by a user on the CLI terminal is output.
test.py import ops def ops_condition(_ops): ret, reason = _ops.cli.subscribe("corn1","device",True,True,False,20) return ret def ops_execute(_ops): _ops.terminal.write("Enter your passwd:",None) passwrd,ret = _ops.terminal.read(10,15,None) print(passwrd)