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.
from jnpr.junos import Device from pprint import pprint with Device(host='router1.example.net') as dev: pprint (dev.facts['hostname']) pprint (dev.facts)
user1@server:~$ python3 get-facts.py 'router1' {'2RE': True, 'HOME': '/var/home/user1', 'RE0': {'last_reboot_reason': '0x200:normal shutdown', 'mastership_state': 'master', 'model': 'RE-MX-104', 'status': 'OK', 'up_time': '25 days, 8 hours, 22 minutes, 40 seconds'}, 'RE1': {'last_reboot_reason': '0x200:normal shutdown', 'mastership_state': 'backup', 'model': 'RE-MX-104', 'status': 'OK', 'up_time': '25 days, 8 hours, 23 minutes, 55 seconds'}, ...
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.
from jnpr.junos import Device from pprint import pprint with Device(host='router1.example.net') as dev: pprint (dev.facts) dev.facts_refresh() pprint (dev.facts)
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:
dev.facts_refresh(keys='hostname') dev.facts_refresh(keys=('hostname','domain','master'))
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.
from jnpr.junos import Device import json with Device(host='router1.example.net') as dev: print (json.dumps(dev.facts))
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:
The entire program is presented here:
import sys from jnpr.junos import Device from jnpr.junos.exception import ConnectError from pprint import pprint dev = Device(host='router1.example.net') try: dev.open() except ConnectError as err: print ("Cannot connect to device: {0}".format(err)) sys.exit(1) pprint (dev.facts['hostname']) pprint (dev.facts) dev.close()
You can also quickly perform the same operations in Python interactive mode.
user1@server:~$ python Python 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> from jnpr.junos import Device >>> from pprint import pprint >>> >>> dev = Device('router1.example.net') >>> dev.open() Device(router1.example.net) >>> >>> pprint (dev.facts) {'2RE': True, 'HOME': '/var/home/user1', 'RE0': {'last_reboot_reason': '0x200:normal shutdown', 'mastership_state': 'master', 'model': 'RE-MX-104', 'status': 'OK', 'up_time': '25 days, 8 hours, 22 minutes, 40 seconds'}, 'RE1': {'last_reboot_reason': '0x200:normal shutdown', 'mastership_state': 'backup', 'model': 'RE-MX-104', 'status': 'OK', 'up_time': '25 days, 8 hours, 23 minutes, 55 seconds'}, ...>>> >>> dev.close() >>> quit()
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.