Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Use Junos PyEZ to Retrieve Facts from Junos Devices

Understanding Junos PyEZ Device Facts

Junos PyEZ is a microframework for Python that enables you to manage and automate Junos devices. Junos PyEZ models each device as an instance of the jnpr.junos.device.Device class. After connecting to a Junos device, 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. The device uses SSH keys to authenticate the user.

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 value of that fact is 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: Retrieve Facts from a Junos Device

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 Junos device 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 Junos device 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.

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 Junos device.

Change History Table

Feature support is determined by the platform and release you are using. Use Feature Explorer to determine if a feature is supported on your platform.

Release
Description
2.1.0
Starting in Junos PyEZ Release 2.1.0, device facts are gathered on demand for all connection types.
2.0.0
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.