Using Junos PyEZ to Retrieve Facts from Devices Running Junos OS

 

Understanding Junos PyEZ Device Facts

Junos PyEZ is a microframework for Python that enables you to manage and automate devices running Junos OS. Junos PyEZ models each device as an instance of the jnpr.junos.device.Device class. After connecting to a device running Junos OS, Junos PyEZ applications can retrieve facts about the device. The device facts are accessed as the facts attribute of the Device object. For detailed information about the keys that are included in the returned device facts, see jnpr.junos.facts.

The following example establishes a NETCONF session over SSH with the device and prints the device facts:

user1@server:~$ python get-facts.py

In Junos PyEZ Release 2.0.0 and earlier releases, when the application calls the Device open() method to connect to a device, Junos PyEZ automatically gathers the device facts for NETCONF-over-SSH connections and gathers the device facts for Telnet and serial console connections when you explicitly include gather_facts=True in the Device argument list.

Starting in Junos PyEZ Release 2.1.0, device facts are gathered on demand for all connection types. Each fact is gathered and cached the first time the application accesses its value or the value of a dependent fact. When you print or use device facts, previously accessed facts are served from the cache, and facts that have not yet been accessed are retrieved from the device. If a fact is not supported on a given platform, or if the application encounters an issue gathering the value of a specific fact, then the fact will have the value None.

Junos PyEZ caches a device fact when it first accesses the fact or a dependent fact, but it does not update the cached value upon subsequent access. To refresh the device facts, call the facts_refresh() method. The facts_refresh() method empties the cache of all facts, such that when the application next accesses a fact, it retrieves it from the device and stores the current value in the cache.

To refresh only a single fact or a set of facts, include the keys argument in the facts_refresh() method, and specify the keys to clear from the cache. For example:

Note

Starting in Junos PyEZ Release 2.0.0, exceptions that occur when gathering facts raise a warning instead of an error, which enables the script to continue running.

By default, Junos PyEZ returns the device facts as a dictionary-like object. Starting in Junos PyEZ Release 2.2.1, you can view the device facts in JavaScript Object Notation (JSON). To view a JSON representation of the facts, import the json module, and call the json.dumps() function.

Example: Retrieving Facts from Devices Running Junos OS

With Junos PyEZ, you can quickly execute commands in Python interactive mode, or you can create programs to perform tasks. The following example establishes a NETCONF session over SSH with a device running Junos OS and retrieves and prints facts for the device using both a simple Python program and Python interactive mode. The examples use existing SSH keys for authentication.

To create a Junos PyEZ application that establishes a NETCONF session over SSH with a device running Junos OS and prints the device facts:

  1. In your favorite editor, create a new file with a descriptive name that uses the .py file extension.
  2. Import the Device class and any other modules or objects required for your tasks.

  3. Create the device instance and provide the hostname, any parameters required for authentication, and any optional parameters.

  4. Connect to the device by calling the open() method.

  5. Print the device facts.

    Tip

    To refresh the facts for a device, call the facts_refresh() method, for example, dev.facts_refresh().

  6. Close the connection to the device.

  7. Save and execute the program.

    user1@server:~$ python junos-pyez-device-facts.py

The entire program is presented here:

You can also quickly perform the same operations in Python interactive mode.

The following video presents a short Python session that demonstrates how to use Junos PyEZ to connect to and retrieve facts from a device running Junos OS.

 
Release History Table
Release
Description
Starting in Junos PyEZ Release 2.1.0, device facts are gathered on demand for all connection types.
Starting in Junos PyEZ Release 2.0.0, exceptions that occur when gathering facts raise a warning instead of an error, which enables the script to continue running.