- play_arrow Disclaimer
- play_arrow Junos PyEZ Overview
- play_arrow Install Junos PyEZ
- play_arrow Use Junos PyEZ to Manage Device Operations
- Use Junos PyEZ to Execute RPCs on Junos Devices
- Suppress RpcError Exceptions Raised for Warnings in Junos PyEZ Applications
- Use Junos PyEZ to Halt, Reboot, or Shut Down Junos Devices
- Use Junos PyEZ to Install Software on Junos Devices
- Use Junos PyEZ to Perform File System Operations
- Transfer Files Using Junos PyEZ
- Specify the XML Parser for a Junos PyEZ Session
- play_arrow Use Junos PyEZ to Manage the Configuration
- Use Junos PyEZ to Retrieve a Configuration
- Use Junos PyEZ to Compare the Candidate Configuration and a Previously Committed Configuration
- Use Junos PyEZ to Configure Junos Devices
- Use the Junos PyEZ Config Utility to Configure Junos Devices
- Use Junos PyEZ to Commit the Configuration
- Example: Use Junos PyEZ to Load Configuration Data from a File
- Example: Use Junos PyEZ to Roll Back the Configuration
- Use Junos PyEZ to Manage the Rescue Configuration on Junos Devices
- play_arrow Create and Use Junos PyEZ Tables and Views
- Understanding Junos PyEZ Tables and Views
- Predefined Junos PyEZ Operational Tables (Structured Output)
- Load Inline or External Tables and Views in Junos PyEZ Applications
- Define Junos PyEZ Operational Tables for Parsing Structured Output
- Define Views for Junos PyEZ Operational Tables that Parse Structured Output
- Use Junos PyEZ Operational Tables and Views that Parse Structured Output
- Define Junos PyEZ Operational Tables for Parsing Unstructured Output
- Define Views for Junos PyEZ Operational Tables that Parse Unstructured Output
- Use Junos PyEZ Tables with TextFSM Templates
- Use Junos PyEZ Operational Tables and Views that Parse Unstructured Output
- Define Junos PyEZ Configuration Tables
- Define Views for Junos PyEZ Configuration Tables
- Use Junos PyEZ Configuration Tables to Retrieve Configuration Data
- Overview of Using Junos PyEZ Configuration Tables to Define and Configure Structured Resources
- Use Junos PyEZ Configuration Tables to Configure Structured Resources on Junos Devices
- Save and Load Junos PyEZ Table XML to and from Files
- play_arrow Troubleshoot Junos PyEZ
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.
Video 1: Junos PyEZ - Hello, World
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.