Load Inline or External Tables and Views in Junos PyEZ Applications
SUMMARY Import predefined Tables or inline or external custom Tables into your Junos PyEZ application.
Junos PyEZ Tables and Views provide a simple and efficient way to configure Junos devices or extract specific information from operational command output or configuration data. Junos PyEZ provides a set of predefined operational Tables and Views that you can use in applications, or you can create your own custom operational or configuration Tables and Views.
You can create quick inline Tables and Views as a multiline string directly in the Python application, or you can create one or more Table and View definitions in external files and import the Tables into your Python application. Inline Tables and Views are simpler to use, but using external files enables you to create a central, reusable library.
To use Junos PyEZ’s predefined Tables and Views in your Python application, you must import the Table into your application. To use custom Tables and Views, you must create the Table and View definitions, and then either load or import the definitions into the application, depending on whether they are internal or external to the module. The following sections outline this process for Tables and Views that are both internal and external to the module.
Import Junos PyEZ’s Predefined Tables and Views
The Junos PyEZ jnpr.junos.op
module
and jnpr.junos.command
module provide predefined
Table and View definitions for some common operational RPCs and commands.
To use Junos PyEZ’s predefined Tables and Views in your Python
application, you must include the appropriate import statements in
your application. Along with importing the Junos PyEZ Device
class, you must also import any required Tables.
The following example imports a predefined operational Table, EthPortTable
, from the jnpr.junos.op.ethport
module:
from jnpr.junos import Device from jnpr.junos.op.ethport import EthPortTable
After you import the Table and View definitions, you can use them as described in Use Tables and Views. The following example retrieves the data for the RPC defined in the Table and then prints the interface name and operational status.
from jnpr.junos import Device from jnpr.junos.op.ethport import EthPortTable with Device(host='router1.example.net') as dev: eth = EthPortTable(dev) eth.get() for item in eth: print ("{}: {}".format(item.name, item.oper))
For more information about Junos PyEZ’s predefined Tables and Views, see Predefined Junos PyEZ Operational Tables (Structured Output).
Load Inline Tables and Views
To create, load, and use custom inline Tables and Views in your Junos PyEZ application:
After the Table and View definitions are loaded, there is no difference in how you use inline or external Tables in your module. For additional information, see Use Tables and Views.
Import External Tables and Views
External Table and View definitions are placed in files that are external to your Junos PyEZ application. To create external custom Tables and Views and import them into your Junos PyEZ application:
After the Table and View definitions are loaded, there is no difference in how you use inline or external Tables in your module. For additional information, see Use Tables and Views.
Use Tables and Views
After you load or import the Table and View definitions, you can use the predefined, custom inline, or custom external Tables in the same manner.
To use a Table:
The following example imports a custom external Table, UserTable
. The application connects to the device and
calls the Tables’s get()
method to
retrieve user
objects from the [edit system login]
hierarchy level. The application then prints each username and its
corresponding login class.
from jnpr.junos import Device from myTables.ConfigTables import UserTable with Device(host='router.example.com') as dev: users = UserTable(dev) users.get() for account in users: print("Username is {}\nUser class is {}".format(account.username, account.userclass))
For more information about using Junos PyEZ Tables, see the following topics:
Use Junos PyEZ Operational Tables and Views that Parse Structured Output
Use Junos PyEZ Operational Tables and Views that Parse Unstructured Output
Use Junos PyEZ Configuration Tables to Retrieve Configuration Data
Use Junos PyEZ Configuration Tables to Configure Structured Resources on Junos Devices