Display and Read Messages on User Terminals

Function Description

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.

Command Prototype

# 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)

Parameter Description

Table 1 describes the parameters supported by terminal display and read APIs.
Table 1 Parameters supported by terminal display and read APIs

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.

Description of Return Values

Return values of opsObj.terminal.write():
  • 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.terminal.read():

  • None: indicates a user input timeout, a user has entered Ctrl+C, or the parameter is incorrect.
  • Null character string: indicates that a user has pressed Enter.

Example

# 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)
Copyright © Huawei Technologies Co., Ltd.
Copyright © Huawei Technologies Co., Ltd.
< Previous topic Next topic >