Using Junos PyEZ to Connect to and Retrieve Facts from Devices Running Junos OS

 

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. For detailed information about the jnpr.junos.device module, see http://junos-pyez.readthedocs.io/en/latest/jnpr.junos.html#module-jnpr.junos.device.

After connecting to a device running Junos OS, you can retrieve and print facts about the device. These facts are accessed as the facts attribute of a Device object instance. For detailed information about the keys that are included in the facts dictionary, see jnpr.junos.facts.

Note

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 you access its value or the value of a dependent fact. In earlier releases, when you call 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.

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.

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. After performing any necessary tasks, close the connection to the device.

  7. Save and execute the program.

    [root@server]# python filename.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.

 

By default, Junos PyEZ returns the device facts as a Python dictionary. Starting in Junos PyEZ Release 1.2, you can view the device facts in YAML or JavaScript Object Notation (JSON). To view a YAML or JSON representation of the facts, import the yaml or json module, respectively, and call either the yaml.dump() or json.dumps() function, as appropriate.

Release History Table
Release
Description
Starting in Junos PyEZ Release 2.1.0, device facts are gathered on demand for all connection types.