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:
from jnpr.junos import Device from jnpr.junos.op.ethport import EthPortTable with Device(host='router.example.com') as dev: eths = EthPortTable(dev)
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.
--- EthPortTable: rpc: get-interface-information args: media: True interface_name: '[afgxe][et]-*' args_key: interface_name item: physical-interface view: EthPortView
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.
from jnpr.junos import Device from jnpr.junos.op.ethport import EthPortTable with Device(host='router.example.com') as dev: eths = EthPortTable(dev) eths.get()
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()
.
eths = EthPortTable(dev) eths.get(interface_name='ge-0/3/0')
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:
user@router> show interfaces ? Possible completions: <[Enter]> Execute this command <interface-name> Name of physical or logical interface ge-0/0/0 ge-0/0/0.0
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.
eths = EthPortTable(dev) eths.get('ge-0/3/0')
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.
eths = EthPortTable(dev) eths.get(interface_name='ge-0/3/0', normalize=False)
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.
eths = EthPortTable(dev).get(interface_name='ge-0/3/0') print (eths.keys())
In this case, there is only a single key.
['ge-0/3/0']
You can verify that a specific key is present in the
Table items by using the Python in
operator.
if 'ge-0/3/0' in eths:
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.
print (eths.values())
[[('oper', 'down'), ('rx_packets', '0'), ('macaddr', '00:00:5E:00:53:01'), ('description', None), ('rx_bytes', '0'), ('admin', 'up'), ('mtu', 1514), ('running', True), ('link_mode', None), ('tx_bytes', '0'), ('tx_packets', '0'), ('present', True)]]
To view the complete list of items, including both keys
and values, call the items()
method.
print (eths.items())
[('ge-0/3/0', [('oper', 'down'), ('rx_packets', '0'), ('macaddr', '00:00:5E:00:53:01'), ('description', None), ('rx_bytes', '0'), ('admin', 'up'), ('mtu', 1514), ('running', True), ('link_mode', None), ('tx_bytes', '0'), ('tx_packets', '0'), ('present', True)])]
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:
from jnpr.junos import Device from jnpr.junos.op.ethport import EthPortTable with Device(host='router.example.com') as dev: eths = EthPortTable(dev) eths.get() for port in eths: print ("{}: {}".format(port.name, port.oper))
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.
ge-0/3/0: up ge-0/3/1: up ge-0/3/2: up ge-0/3/3: up