Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use Junos PyEZ Operational Tables and Views that Parse Structured Output

Junos PyEZ operational (op) Tables for structured output extract specific data from the XML output of an RPC executed on a Junos device. After loading or importing the Table definition into your Python module, you can retrieve the Table items and extract and manipulate the data.

To retrieve information from a specific device, you must create a Table instance and associate it with the Device object representing the target device. For example:

The following sections discuss how to then retrieve and manipulate the data:

Retrieve Table Items

The Table item property determines which items are extracted from the operational command output. For example, the Junos PyEZ EthPortTable definition, which is included here for reference, executes the show interfaces "[afgxe][et]-*" media command by default and extracts the physical-interface items from the output.

You retrieve the Table items in your Python script by calling the get() method and supplying any desired arguments. If the Table definition includes default arguments in the args property, the executed RPC automatically includes these arguments when you call get() unless you override them in your argument list.

To retrieve all Table items, call the get() method with an empty argument list.

You can also retrieve specific Table items by passing command options as arguments to the get() method. If the command option is a flag that does not take a value, set the option equal to True in the argument list. Otherwise, include the argument and desired value as a key-value pair in the argument list. You can review the possible arguments for operational commands in the Junos CLI.

By default, EthPortTable returns information for Ethernet interfaces that have names matching the expression "[afgxe][et]-*". To retrieve the Table item for the ge-0/3/0 interface only, include interface_name='ge-0/3/0' as an argument to get().

Note:

If the option name in the Junos OS command-line interface (CLI) is hyphenated, you must change any dashes in the name to underscores. The argument value, however, is a string and as such can contain hyphens.

If the CLI command takes an optional first argument that does not require you to explicitly specify an option name or keyword, you can omit the option name in the get() method argument list provided that the Table args_key property references this argument. In the following example, the show interfaces command takes an interface name as an optional argument:

The EthPortTable definition args_key property defines the optional argument as interface_name, which enables you to use this argument without having to explicitly specify the option name in the get() method argument list.

By default, Junos PyEZ normalizes all op Table keys and values, which strips out all leading and trailing whitespace and replaces sequences of internal whitespace characters with a single space. To disable normalization, include normalize=False as an argument to the get() method.

Access Table Items

After you retrieve the Table items, you can treat them like a Python dictionary, which enables you to use methods in the standard Python library to access and manipulate the items.

To view the list of dictionary keys corresponding to the Table item names, call the keys() method.

In this case, there is only a single key.

You can verify that a specific key is present in the Table items by using the Python in operator.

To view a list of the fields, or values, associated with each key, call the values() method. The values() method returns a list of tuples with the name-value pairs for each field that was defined in the View.

To view the complete list of items, including both keys and values, call the items() method.

How to Iterate Through a Table

Tables support iteration, which enables you to loop through each Table item in the same way that you would loop through a list or dictionary. This makes it easy to quickly format and print desired fields.

The EthPortTable definition, which is included in the jnpr.junos.op module, executes the show interfaces "[afgxe][et]-*" media command and extracts the physical-interface items from the output. The following code loops through the physical-interface items and prints the name and operational status of each Ethernet port:

The oper field, which is defined in EthPortView, corresponds to the value of the oper-status element in the output. The EthPortView definition does not define a name field. By default, each View item has a name property that references the key that uniquely identifies that item.

The output includes the interface name and operational status.