Obtaining environment variable
Subscription and execution phases
result1_value, result2_description = ops.environment.get(envName)
Parameter |
Description |
Value |
---|---|---|
envName |
Indicates the name of an environment variable. |
The value is a string of 1 to 31 case-sensitive characters without spaces. It can contain only letters, digits, and underscores (_).
|
The result1_value and result2_description in the API prototype indicate return values.
The first return value is an environment variable value, and is a character string or digit. If the first return value is a digit, it must be converted through int(). If the first return value is a character string, None indicates a failure.
The second return value is a string of characters indicating the results. When the first return value is the environment variable value, Success is displayed. If the first return value is None, the failure cause is displayed.
An environment variable consists of a name and a value. Currently, OPS supports system and user-defined environment variables.
System environment variables
Environment variables supported by the device by default cannot be created, deleted, or modified. A system environment variable name starts with a _, and a system environment variable value is determined by the system.
System environment variables include:
Public environment variables: can be used for all types of events.
Non-public environment variables: can only be used for specified events. Its value is obtained when an event is triggered and indicates some information about the event.
Table 1 shows system environment variables currently supported by the OPS.
Event |
Environment Variable Name |
Environment Variable Description |
Application Phase |
---|---|---|---|
All events |
_event_name |
Event name |
Execution phase |
_event_datetime |
Time when an event is received |
Execution phase |
|
_assistant_name |
Assistant name, or script name |
Subscription and execution phases |
|
_sysname |
Device name |
Subscription and execution phases |
|
_phase |
Script execution phase |
Subscription and execution phases |
|
_event_hits |
Number of times the current event occurs within the last period |
Execution phase |
|
CLI |
_cli_input |
Command entered by a user (an incomplete keyword, such as dis dev) |
Execution phase |
_cli_command |
User entered command (a complete keyword, such as display device) |
Execution phase |
|
_cli_view |
CLI view name |
Execution phase |
|
_cli_trigger |
CLI triggering point |
Execution phase |
|
_cli_vty |
User channel |
Execution phase |
|
_cli_ip |
User channel IP address |
Execution phase |
|
Timer |
_timer_type |
Timer type |
Execution phase |
Route change |
_routing_network |
Network prefix |
Execution phase |
_routing_mask |
Network mask |
Execution phase |
|
_routing_protocol |
Network protocol |
Execution phase |
|
_routing_type |
Change type |
Execution phase |
|
_routing_nexthop |
Next hop |
Execution phase |
|
_routing_interface |
Interface name |
Execution phase |
|
Log |
_iclog_module |
Log module name |
Execution phase |
_iclog_level |
Log severity |
Execution phase |
|
_iclog_alias |
Log alias |
Execution phase |
|
_iclog_content |
Log contents |
Execution phase |
|
_iclog_time |
Log generation time |
Execution phase |
|
Trap |
_trap_module |
Trap module name |
Execution phase |
_trap_level |
Trap severity |
Execution phase |
|
_trap_alias |
Trap alias |
Execution phase |
|
_trap_content |
Trap contents |
Execution phase |
|
_trap_time |
Trap generation time |
Execution phase |
|
_trap_oid |
Trap OID |
Execution phase |
|
LLDP neighbor change |
_lldp_interface |
Interface on which the neighbor status changes |
Execution phase |
_lldp_devtype |
Type of a neighbor device |
Execution phase |
|
_lldp_event |
Event triggering type |
Execution phase |
|
Stack status change |
_dev_event |
Event name |
Execution phase |
_dev_slot |
Stack ID |
Execution phase |
User-defined environment variables
In a Python script, you can input an environment variable name in the location where a parameter needs to be input to indicate that an environment variable value needs to be referenced. When the system is running a Python script, it replaces an environment variable name with an environment variable value. To change the value, you can directly change it on the device without having to change and install the Python script. You can define and use user-defined environment variables to simplify the configuration and improve flexibility and feasibility of the Python script.
User-define environment variables start with letters, can contain letters, digits, and _, apply to all types of events, and can be used in subscription and execution phases. The value of a user-defined environment variable can be configured using the environment command. You can create, modify, and delete user-defined variables.
If the subscription event does not occur, some environment variables related to the event cannot be obtained.
Using the Command Line Event Subscription API, you can subscribe to commands run on a device. When a subscription event is matched, the user wants to view information about the command.
value, loginfo = ops.environment.get("_cli_view")