Junos OS, PyEZ User Manual

®
Published
2020-10-27
Junos
OS
Junos PyEZ Developer Guide
Juniper Networks, Inc. 1133 Innovation Way Sunnyvale, California 94089 USA 408-745-2000 www.juniper.net
Juniper Networks, the Juniper Networks logo, Juniper, and Junos are registered trademarks of Juniper Networks, Inc. in the United States and other countries. All other trademarks, service marks, registered marks, or registered service marks are the property of their respective owners.
Juniper Networks assumes no responsibility for any inaccuracies in this document. Juniper Networks reserves the right to change, modify, transfer, or otherwise revise this publication without notice.
Junos®OS Junos PyEZ Developer Guide
Copyright © 2020 Juniper Networks, Inc. All rights reserved.
The information in this document is current as of the date on the title page.
ii
YEAR 2000 NOTICE
Juniper Networks hardware and software products are Year 2000 compliant. Junos OS has no known time-related limitations through the year 2038. However, the NTP application is known to have some difficulty in the year 2036.
END USER LICENSE AGREEMENT
The Juniper Networks product that is the subject of this technical documentation consists of (or is intended for use with) Juniper Networks software. Use of such software is subject to the terms and conditions of the End User License Agreement (“EULA”) posted at https://support.juniper.net/support/eula/. By downloading, installing or using such software, you agree to the terms and conditions of that EULA.

Table of Contents

1
2
3
About the Documentation | x
Documentation and Release Notes | x
Documentation Conventions | x
Documentation Feedback | xiii
Requesting Technical Support | xiii
Self-Help Online Tools and Resources | xiv
Creating a Service Request with JTAC | xiv
Disclaimer
Junos PyEZ Disclaimer | 16
iii
Junos PyEZ Overview
Understanding Junos PyEZ | 18
Junos PyEZ Overview | 18
Benefits of Junos PyEZ | 19
Using Junos PyEZ in Automation Scripts | 19
Junos PyEZ Resources | 20
Junos PyEZ Modules Overview | 21
Installing Junos PyEZ
Installing Junos PyEZ | 25
Installing Prerequisite Software | 25
Installing Junos PyEZ on the Configuration Management Server | 27
Installing Junos PyEZ in a Python Virtual Environment | 28
Using the Junos PyEZ Docker Image | 29
Setting Up Junos PyEZ Managed Nodes | 31
Enabling NETCONF over SSH on Devices Running Junos OS | 32
Satisfying Requirements for SSHv2 Connections | 32
Configuring Telnet Service on Devices Running Junos OS | 33
Connecting to and Retrieving Facts From a Device Using Junos PyEZ
4
5
Connecting to Devices Running Junos OS Using Junos PyEZ | 36
Connection Methods Overview | 36
Understanding Junos PyEZ Connection Properties | 38
Connecting to a Device Using SSH | 39
Connecting to a Device Using Outbound SSH | 43
Connecting to a Device Using Telnet | 47
Connecting to a Device Using a Serial Console Connection | 49
Authenticating Junos PyEZ Users | 52
Junos PyEZ User Authentication Overview | 52
Authenticating Junos PyEZ Users Using a Password | 53
Authenticating Junos PyEZ Users Using SSH Keys | 55
Generating and Configuring SSH Keys | 56
iv
Referencing SSH Keys in Junos PyEZ Applications | 57
Using Junos PyEZ to Retrieve Facts from Devices Running Junos OS | 60
Understanding Junos PyEZ Device Facts | 61
Example: Retrieving Facts from Devices Running Junos OS | 63
Accessing the Shell on Devices Running Junos OS Using Junos PyEZ | 66
Using Junos PyEZ to Manage Device Operations
Using Junos PyEZ to Execute RPCs on Devices Running Junos OS | 71
Mapping Junos OS Commands to Junos PyEZ RPCs | 71
Executing RPCs as a Property of the Device Instance | 73
Specifying the Format of the RPC Output | 74
Specifying the Scope of Data to Return | 76
Specifying the RPC Timeout | 77
Normalizing the XML RPC Reply | 78
Suppressing RpcError Exceptions Raised for Warnings in Junos PyEZ Applications | 80
Using Junos PyEZ to Reboot or Shut Down Devices Running Junos OS | 81
Performing a Reboot or Shut Down | 82
Performing a Reboot or Shut Down with a Delay or at a Specified Time | 84
Specifying the Target for the Reboot or Shutdown | 85
6
Rebooting a VM Host | 86
Using Junos PyEZ to Install Software on Devices Running Junos OS | 87
Supported Deployment Scenarios | 87
Specifying the Software Image Location | 88
Installation Process Overview | 89
Specifying Installation and Checksum Timeouts | 91
Logging the Installation Process | 92
Performing a VM Host Upgrade | 93
Performing a Unified ISSU or NSSU | 94
Example: Using Junos PyEZ to Install Software on Devices Running Junos OS | 96
Transferring Files Using Junos PyEZ | 105
Specifying the XML Parser for a Junos PyEZ Session | 109
v
Using Junos PyEZ to Manage the Configuration
Using Junos PyEZ to Retrieve a Configuration | 112
Retrieving the Complete Candidate Configuration | 112
Specifying the Source Database for the Configuration Data | 113
Specifying the Scope of Configuration Data to Return | 115
Specifying the Format for Configuration Data to Return | 118
Retrieving Configuration Data for Standard or Custom YANG Data Models | 119
Specifying Additional RPC Options | 121
Handling Namespaces in Configuration Data | 121
Using Junos PyEZ to Compare the Candidate Configuration and a Previously Committed
Configuration | 123
Using Junos PyEZ to Configure Devices Running Junos OS | 126
Comparing Structured and Unstructured Configuration Changes | 126
Understanding the General Configuration Process | 128
Specifying the Configuration Mode | 130
Specifying the Load Operation | 132
Creating the Config or Table Object as a Property of the Device Instance | 133
Using the Junos PyEZ Config Utility to Configure Devices Running Junos OS | 134
Configuration Process Overview | 134
Specifying the Configuration Mode | 136
Specifying the Load Operation | 137
Specifying the Format of the Configuration Data to Load | 138
Specifying the Location of the Configuration Data | 139
Loading Configuration Data from a Local or Remote File | 140
Loading Configuration Data from a String | 142
Loading Configuration Data Formatted as an XML Object | 144
Loading Configuration Data Using Jinja2 Templates | 145
Rolling Back the Configuration | 147
Loading the Rescue Configuration | 149
vi
Committing the Configuration | 150
Using Junos PyEZ to Commit the Configuration | 151
Committing the Candidate Configuration | 151
Specifying Commit Options | 153
Example: Using Junos PyEZ to Load Configuration Data from a File | 157
Example: Using Junos PyEZ to Roll Back the Configuration | 167
Using Junos PyEZ to Manage the Rescue Configuration on Devices Running Junos
OS | 174
Managing the Rescue Configuration | 174
Saving a Rescue Configuration | 175
Retrieving the Rescue Configuration | 175
Loading and Committing the Rescue Configuration | 176
Deleting the Rescue Configuration | 176
Example: Using Junos PyEZ to Save a Rescue Configuration | 176
Creating and Using Junos PyEZ Tables and Views
7
Understanding Junos PyEZ Tables and Views | 185
Predefined Junos PyEZ Operational Tables (Structured Output) | 187
Loading Inline or External Tables and Views in Junos PyEZ Applications | 190
Importing Junos PyEZ’s Predefined Tables and Views | 191
Loading Inline Tables and Views | 191
Importing External Tables and Views | 193
Using Tables and Views | 194
Defining Junos PyEZ Operational Tables for Parsing Structured Output | 196
Table Name | 198
RPC Command (rpc) | 198
RPC Default Arguments (args) | 198
vii
RPC Optional Argument Key (args_key) | 199
Table Item (item) | 200
Table Item Key (key) | 201
Table View (view) | 204
Defining Views for Junos PyEZ Operational Tables that Parse Structured Output | 205
View Name | 207
Fields (fields) | 207
Groups (groups) and Field Groups (fields_) | 209
Using Junos PyEZ Operational Tables and Views that Parse Structured Output | 211
Retrieving Table Items | 211
Accessing Table Items | 213
Iterating Through a Table | 214
Defining Junos PyEZ Operational Tables for Parsing Unstructured Output | 215
Summary of Parameters in Op Tables for Parsing Unstructured Output | 216
Table Name | 217
Command | 218
Command Arguments (args) | 218
Target FPC (fpc) | 219
Table Item (item) | 221
Table Item Key (key) | 224
Selected Keys (key_items) | 225
Section Title (title) | 227
Field Delimiter (delimiter) | 228
Eval Expression (eval) | 229
Table View (view) | 231
Defining Views for Junos PyEZ Operational Tables that Parse Unstructured Output | 231
Summary of Parameters in Views for Parsing Unstructured Output | 232
View Name | 233
columns | 233
Eval Expression (eval) | 236
exists | 238
fields | 239
viii
filters | 244
regex | 246
Using Junos PyEZ Operational Tables and Views that Parse Unstructured Output | 252
Defining Junos PyEZ Configuration Tables | 253
Table Name | 255
Configuration Scope (get or set) | 255
Key Field (key-field) | 257
Required Keys (required_keys) | 258
Table View (view) | 259
Defining Views for Junos PyEZ Configuration Tables | 260
View Name | 262
Fields (fields) | 263
Field Options ('get' Tables) | 265
Field Options ('set' Tables) | 266
Groups (groups) and Field Groups (fields_) | 268
Using Junos PyEZ Configuration Tables to Retrieve Configuration Data | 270
Retrieving Configuration Items | 271
Specifying Inheritance and Group Options | 272
Accessing Table Items | 274
Iterating Through a Table | 275
8
Overview of Using Junos PyEZ Configuration Tables to Define and Configure Structured
Resources | 276
Creating the Structured Resource | 277
Using the Resource in a Junos PyEZ Application | 278
Using Junos PyEZ Configuration Tables to Configure Structured Resources on Devices
Running Junos OS | 280
General Configuration Process | 281
Configuring Statements Consisting of a Fixed-Form Keyword | 284
Configuring Multiple Values for the Same Statement | 286
Configuring Multiple Instances of the Same Statement | 287
Configuring Multiple Instances of the Same Resource | 289
Deleting Containers or Leaf Statements | 291
ix
Configuring Properties Corresponding to Junos XML Attributes | 293
Using append() to Generate the Junos XML Configuration Data | 296
Viewing Your Configuration Changes | 297
Controlling the RPC Timeout Interval | 298
Saving and Loading Junos PyEZ Table XML to and from Files | 299
Troubleshooting Junos PyEZ
Troubleshooting jnpr.junos Import Errors | 303
Troubleshooting Junos PyEZ Connection Errors | 304
Troubleshooting Junos PyEZ Authentication Errors When Managing Devices Running
Junos OS | 305
Troubleshooting Junos PyEZ Errors When Configuring Devices Running Junos OS | 306
Troubleshooting Timeout Errors | 306
Troubleshooting Configuration Lock Errors | 307
Troubleshooting Configuration Change Errors | 308

About the Documentation

IN THIS SECTION
Documentation and Release Notes | x
Documentation Conventions | x
Documentation Feedback | xiii
Requesting Technical Support | xiii
Use this guide to develop Python scripts that remotely automate and manage devices running Junos OS using the Juniper Networks Junos PyEZ Python library.
x

Documentation and Release Notes

To obtain the most current version of all Juniper Networks®technical documentation, see the product documentation page on the Juniper Networks website at https://www.juniper.net/documentation/.
If the information in the latest release notes differs from the information in the documentation, follow the product Release Notes.
Juniper Networks Books publishes books by Juniper Networks engineers and subject matter experts. These books go beyond the technical documentation to explore the nuances of network architecture, deployment, and administration. The current list can be viewed at https://www.juniper.net/books.

Documentation Conventions

Table 1 on page xi defines notice icons used in this guide.
Table 1: Notice Icons
xi
DescriptionMeaningIcon
Indicates important features or instructions.Informational note
Caution
Indicates a situation that might result in loss of data or hardware damage.
Alerts you to the risk of personal injury or death.Warning
Alerts you to the risk of personal injury from a laser.Laser warning
Indicates helpful information.Tip
Alerts you to a recommended use or implementation.Best practice
Table 2 on page xi defines the text and syntax conventions used in this guide.
Table 2: Text and Syntax Conventions
ExamplesDescriptionConvention
Fixed-width text like this
Italic text like this
Represents text that you type.Bold text like this
Represents output that appears on the terminal screen.
Introduces or emphasizes important
new terms.
Identifies guide names.
Identifies RFC and Internet draft
titles.
To enter configuration mode, type the configure command:
user@host> configure
user@host> show chassis alarms
No alarms currently active
A policy term is a named structure
that defines match conditions and actions.
Junos OS CLI User Guide
RFC 1997, BGP Communities
Attribute
Table 2: Text and Syntax Conventions (continued)
xii
ExamplesDescriptionConvention
Italic text like this
Text like this
< > (angle brackets)
| (pipe symbol)
Represents variables (options for which you substitute a value) in commands or configuration statements.
Represents names of configuration statements, commands, files, and directories; configuration hierarchy levels; or labels on routing platform components.
variables.
Indicates a choice between the mutually exclusive keywords or variables on either side of the symbol. The set of choices is often enclosed in parentheses for clarity.
Configure the machine’s domain name:
[edit] root@# set system domain-name
domain-name
To configure a stub area, include
the stub statement at the [edit protocols ospf area area-id]
hierarchy level.
The console port is labeled
CONSOLE.
stub <default-metric metric>;Encloses optional keywords or
broadcast | multicast
(string1 | string2 | string3)
# (pound sign)
[ ] (square brackets)
Indention and braces ( { } )
; (semicolon)
GUI Conventions
Indicates a comment specified on the same line as the configuration statement to which it applies.
Encloses a variable for which you can substitute one or more values.
Identifies a level in the configuration hierarchy.
Identifies a leaf statement at a configuration hierarchy level.
rsvp { # Required for dynamic MPLS only
community name members [ community-ids ]
[edit] routing-options {
static {
route default {
nexthop address; retain;
}
}
}
Table 2: Text and Syntax Conventions (continued)
xiii
ExamplesDescriptionConvention
Bold text like this
> (bold right angle bracket)
Represents graphical user interface (GUI) items you click or select.
Separates levels in a hierarchy of menu selections.
In the Logical Interfaces box, select
All Interfaces.
To cancel the configuration, click
Cancel.
In the configuration editor hierarchy, select Protocols>Ospf.

Documentation Feedback

We encourage you to provide feedback so that we can improve our documentation. You can use either of the following methods:
Online feedback system—Click TechLibrary Feedback, on the lower right of any page on the Juniper
Networks TechLibrary site, and do one of the following:
Click the thumbs-up icon if the information on the page was helpful to you.
Click the thumbs-down icon if the information on the page was not helpful to you or if you have
suggestions for improvement, and use the pop-up form to provide feedback.
E-mail—Send your comments to techpubs-comments@juniper.net. Include the document or topic name,
URL or page number, and software version (if applicable).

Requesting Technical Support

Technical product support is available through the Juniper Networks Technical Assistance Center (JTAC). If you are a customer with an active Juniper Care or Partner Support Services support contract, or are
covered under warranty, and need post-sales technical support, you can access our tools and resources online or open a case with JTAC.
JTAC policies—For a complete understanding of our JTAC procedures and policies, review the JTAC User
Guide located at https://www.juniper.net/us/en/local/pdf/resource-guides/7100059-en.pdf.
Product warranties—For product warranty information, visit https://www.juniper.net/support/warranty/.
JTAC hours of operation—The JTAC centers have resources available 24 hours a day, 7 days a week,
365 days a year.

Self-Help Online Tools and Resources

For quick and easy problem resolution, Juniper Networks has designed an online self-service portal called the Customer Support Center (CSC) that provides you with the following features:
Find CSC offerings: https://www.juniper.net/customers/support/
Search for known bugs: https://prsearch.juniper.net/
xiv
Find product documentation: https://www.juniper.net/documentation/
Find solutions and answer questions using our Knowledge Base: https://kb.juniper.net/
Download the latest versions of software and review release notes:
https://www.juniper.net/customers/csc/software/
Search technical bulletins for relevant hardware and software notifications:
https://kb.juniper.net/InfoCenter/
Join and participate in the Juniper Networks Community Forum:
https://www.juniper.net/company/communities/
Create a service request online: https://myjuniper.juniper.net
To verify service entitlement by product serial number, use our Serial Number Entitlement (SNE) Tool:
https://entitlementsearch.juniper.net/entitlementsearch/

Creating a Service Request with JTAC

You can create a service request with JTAC on the Web or by telephone.
Visit https://myjuniper.juniper.net.
Call 1-888-314-JTAC (1-888-314-5822 toll-free in the USA, Canada, and Mexico).
For international or direct-dial options in countries without toll-free numbers, see
https://support.juniper.net/support/requesting-support/.
1
CHAPTER

Disclaimer

Junos PyEZ Disclaimer | 16

Junos PyEZ Disclaimer

Use of the Junos PyEZ software implies acceptance of the terms of this disclaimer, in addition to any other licenses and terms required by Juniper Networks.
Juniper Networks is willing to make the Junos PyEZ software available to you only on the condition that you accept all of the terms contained in this disclaimer. Please read the terms and conditions of this disclaimer carefully.
The Junos PyEZ software is provided as is. Juniper Networks makes no warranties of any kind whatsoever with respect to this software. All express or implied conditions, representations and warranties, including any warranty of non-infringement or warranty of merchantability or fitness for a particular purpose, are hereby disclaimed and excluded to the extent allowed by applicable law.
In no event will Juniper Networks be liable for any direct or indirect damages, including but not limited to lost revenue, profit or data, or for direct, special, indirect, consequential, incidental or punitive damages however caused and regardless of the theory of liability arising out of the use of or inability to use the software, even if Juniper Networks has been advised of the possibility of such damages.
16
2
CHAPTER

Junos PyEZ Overview

Understanding Junos PyEZ | 18
Junos PyEZ Modules Overview | 21

Understanding Junos PyEZ

IN THIS SECTION
Junos PyEZ Overview | 18
Benefits of Junos PyEZ | 19
Using Junos PyEZ in Automation Scripts | 19
Junos PyEZ Resources | 20

Junos PyEZ Overview

18
Junos PyEZ is a microframework for Python that enables you to manage and automate devices running the Junos operating system (Junos OS). Junos PyEZ is designed to provide the capabilities that a user would have on the Junos OS command-line interface (CLI) in an environment built for automation tasks. Junos PyEZ does not require extensive knowledge of Junos OS or the Junos XML APIs.
Junos PyEZ enables you to manage devices running Junos OS using the familiarity of Python. However, you do not have to be an experienced programmer to use Junos PyEZ. Non-programmers can quickly execute simple commands in Python interactive mode, and more experienced programmers can opt to create more complex, robust, and reusable programs to perform tasks.
Junos PyEZ enables you to directly connect to a device using a serial console connection, telnet, or a NETCONF session over SSH. In addition, Junos PyEZ also supports connecting to the device through a telnet or SSH connection to a console server that is connected to the device’s CONSOLE port. You can use Junos PyEZ to initially configure a new or zeroized device that is not yet configured for remote access by using either a serial console connection when you are directly connected to the device or by using telnet or SSH through a console server that is directly connected to the device.
Junos PyEZ provides device, software, and file system utilities that enable you to perform common operational tasks on devices running Junos OS. You can use Junos PyEZ to:
Retrieve facts or operational information from a device
Execute remote procedure calls (RPC) available through the Junos XML API
Install or upgrade the Junos OS software
Reboot or shut down the device
Perform common administrative tasks such as copying files and calculating checksums
Junos PyEZ also enables you to manage the configurations of devices running Junos OS. Junos PyEZ configuration management utilities enable you to:
retrieve configuration data
compare configurations
upload and commit configuration changes
roll back the configuration
manage the rescue configuration
Junos PyEZ supports standard formats for configuration data including ASCII text, Junos XML elements, Junos OS set commands, and JavaScript Object Notation (JSON), and also supports using Jinja2 templates and template files for added flexibility and customization. In addition, you can use Tables and Views to define structured resources that you can use to programmatically configure a device.
Junos PyEZ Tables and Views enable you to both configure devices running Junos OS and extract specific operational information or configuration data from the devices. You define Tables and Views using simple YAML files that contain key:value pair mappings, so no complex coding is required to use them. You can use Tables and Views to retrieve the device configuration or the output for any Junos OS command or RPC and then extract a customized subset of information. This is useful when you need to retrieve information from a few specific fields that are embedded in extensive command output such as for the show route or show interfaces command. In addition, starting in Junos PyEZ Release 2.0, you can use Tables and Views to define structured configuration resources. Junos PyEZ dynamically creates a configuration class for the resource, which enables you to programmatically configure the resource on a device.
19

Benefits of Junos PyEZ

Provides an abstraction layer that enables Python programmers as well as non-programmers to easily
manage and automate devices running Junos OS
Increases operational efficiency by enabling operators to automate common tasks thereby reducing the
manual configuration and management of devices
Minimizes errors and risks by enabling structured configuration changes of targeted resources

Using Junos PyEZ in Automation Scripts

Junos OS includes the Python extensions package and the Junos PyEZ library in the software image for certain supported devices. These extensions enable you to create on-box Python scripts that use Junos
PyEZ to execute RPCs and perform operational and configuration tasks on devices running Junos OS. Junos PyEZ is supported in commit, event, op, and SNMP scripts; Juniper Extension Toolkit (JET) scripts; and YANG action and translation scripts.
Table 3 on page 20 summarizes the Junos PyEZ version that is available on supported devices running the
given Junos OS release. For example, starting in Junos OS Release 17.4R1, an on-box Python script can leverage features in Junos PyEZ Release 2.1.4 and earlier releases.
Table 3: Junos PyEZ Version on Supported Devices Running Junos OS
Junos PyEZ VersionJunos OS Release
1.3.116.1R3 through 17.3
2.1.417.4R1 through 19.3
2.2.019.4R1 and later
For more information about creating onbox Python automation scripts, see Understanding Python
Automation Scripts for Devices Running Junos OS in the Junos OS Automation Scripting User Guide.
20

Junos PyEZ Resources

Juniper Networks provides a number of Junos PyEZ resources, which are described in Table 4 on page 20.
Table 4: Junos PyEZ Resources
URLDescriptionResource
API Reference
Day One: Junos PyEZ Cookbook and script repository
Documentation
modules.
Junos PyEZ network automation cookbook with a setup guide, a start-up sandbox, and a complete showcase of automation scripts that are available on GitHub.
Junos PyEZ documentation containing detailed information about installing Junos PyEZ and using Junos PyEZ to perform operational and configuration tasks on devices running Junos OS.
http://junos-pyez.readthedocs.org/Detailed documentation for the Junos PyEZ
https://www.juniper.net/ documentation/en_US/day-one-books/ DO_PyEZ_Cookbook.pdf
https://github.com/Juniper/ junosautomation/tree/master/pyez/ PyEZ_Cookbook_2017
https://www.juniper.net/ documentation/product/en_US/ junos-pyez
Table 4: Junos PyEZ Resources (continued)
21
URLDescriptionResource
GitHub repository
Google Groups forum
Stack Overflow forum
Techwiki page
Public repository for the Junos PyEZ project. This repository includes the most current source code, installation instructions, and release note summaries for all releases.
Forum that addresses questions and provides general support for Junos PyEZ.
Junos PyEZ sample scripts to get you started.Sample scripts
Forum that addresses questions and provides general support for Junos PyEZ.
Juniper Networks J-Net community forum containing additional how-to articles and usage examples.
RELATED DOCUMENTATION
https://github.com/Juniper/ py-junos-eznc/
http://groups.google.com/group/ junos-python-ez
https://github.com/Juniper/ junosautomation/tree/master/pyez
https://stackoverflow.com/questions/ tagged/pyez
https://forums.juniper.net/t5/ Automation-Scripting/Junos-PyEZ/ta-p/ 280496
Junos PyEZ Modules Overview | 21
Installing Junos PyEZ | 25
Connecting to Devices Running Junos OS Using Junos PyEZ | 36
Using Junos PyEZ to Configure Devices Running Junos OS | 126

Junos PyEZ Modules Overview

Junos PyEZ is a microframework for Python that enables you to manage and automate devices running Junos OS. Junos PyEZ consists of the jnpr.junos package, which contains modules that handle device connectivity and provide operational and configuration utilities.
Table 5 on page 22 outlines the primary Junos PyEZ modules that are used to manage devices running
Junos OS. For detailed information about each module, see the Junos PyEZ API Reference at
http://junos-pyez.readthedocs.org/.
Table 5: Junos PyEZ Modules
Descriptionjnpr.junos Modules
22
device
command
exception
factory
facts
op
resources
Defines the Device class, which represents the device running Junos OS and enables you to connect to and retrieve facts from the device.
Includes predefined operational Tables and Views that can be used to filter unstructured output returned from CLI and vty commands and convert it to JSON.
Defines exceptions encountered when accessing, configuring, and managing devices running Junos OS.
Contains code pertaining to Tables and Views, including the loadyaml() function, which is used to load custom Tables and Views.
A dictionary-like object of read-only facts about the device. These facts are accessed using the facts attribute of a Device object instance.
Includes predefined operational Tables and Views that can be used to filter structured output returned from RPCs.
Includes predefined configuration Tables and Views representing specific configuration resources, which can be used to programmatically configure devices running Junos OS.
Contains code used by the Device class to support the different connection types.transport
utils
Includes configuration utilities, file system utilities, shell utilities, software installation utilities, and secure copy utilities.
In Junos PyEZ, each device is modeled as an instance of the jnpr.junos.device.Device class. The device module provides access to devices running Junos OS through a serial console connection, telnet, or SSH and also supports connecting to the device through a telnet or SSH connection to a console server that is connected to the device’s CONSOLE port. All connection methods support retrieving device facts, performing operations, and executing RPCs on demand. Support for serial console connections and for telnet and SSH connections through a console server enables you to connect to and initially configure new or zeroized devices that are not yet configured for remote access. Facts about the device are accessed using the facts attribute of the Device object instance.
The utils module defines submodules and classes that handle software installation, file system and copy operations, and configuration management. The exception module defines exceptions encountered when managing devices running Junos OS.
The command, op, resources, and factory modules pertain to Tables and Views. The command and op modules contain predefined operational Tables and Views that can be used to extract specific information
from the output of common operational commands and RPCs on devices running Junos OS. The resources module contains predefined configuration Tables and Views that can be used to configure specific resources on devices running Junos OS. The factory module contains methods that enable you to load your own custom Tables and Views in Junos PyEZ applications.
RELATED DOCUMENTATION
Understanding Junos PyEZ | 18
Understanding Junos PyEZ Tables and Views | 185
Using Junos PyEZ to Retrieve Facts from Devices Running Junos OS | 60
23
3
CHAPTER

Installing Junos PyEZ

Installing Junos PyEZ | 25
Setting Up Junos PyEZ Managed Nodes | 31

Installing Junos PyEZ

Junos PyEZ is a Python library that enables you to manage and automate devices running Junos OS. You can install Junos PyEZ on a UNIX-like operating system or on Windows. You have the option to install and run Junos PyEZ directly on the configuration management server, within a Python virtual environment, or as a Docker container.
As an alternative to installing Junos PyEZ directly on the server, you can install it in a virtual environment. A Python virtual environment isolates a project’s Python installation and packages from those installed on the system or in other virtual environments, which prevents breaking the dependencies of other projects. You can create virtual environments when you have projects that require different versions of Python or Python packages or as an alternative to installing packages globally on the system.
Juniper Networks also provides a Junos PyEZ Docker image that enables you to run Junos PyEZ as a Docker container. The Docker container is a lightweight, self-contained system that bundles Junos PyEZ, its dependencies, and Python into a single portable container. The Docker image enables you to quickly run Junos PyEZ in interactive mode, as an executable package, or as a terminal on any platform that supports Docker.
25
To install Junos PyEZ on the configuration management server, see the following sections:
Installing Prerequisite Software on page 25
Installing Junos PyEZ on the Configuration Management Server on page 27
To install Junos PyEZ in a Python virtual environment, see the following sections:
Installing Prerequisite Software on page 25
Installing Junos PyEZ in a Python Virtual Environment on page 28
To use the Junos PyEZ Docker image, see the following section:
Using the Junos PyEZ Docker Image on page 29

Installing Prerequisite Software

Before you install the Junos PyEZ library on the configuration management server or in a virtual environment, ensure that the configuration management server has the following software installed:
Python 2.7, or Python 3.5 or later
All prerequisite software for the given operating system, which is outlined in Table 6 on page 26
NOTE: Python 3.x is only supported in Junos PyEZ Release 2.0 and later releases.
Table 6: Junos PyEZ Prerequisite Software
Package or LibraryOperating System
26
CentOS
Debian
Fedora
gcc
libffi-devel
libxml2-devel
libxslt-devel
openssl-devel
pip
python-devel
redhat-rpm-config
libxml2-dev
libxslt1-dev
libssl-dev
pip
python-devel (required for Python 2)
python3-devel (required for Python 3)
gcc
libffi-devel
libxml2-devel
libxslt-devel
openssl-devel
pip
python-devel (required for Python 2)
python3-devel (required for Python 3)
redhat-rpm-config
FreeBSD
libxml2
libxslt
py27-pip
Table 6: Junos PyEZ Prerequisite Software (continued)
Package or LibraryOperating System
27
OSX
NOTE: If Junos PyEZ does not
successfully install using pip, try using easy_install to install the lxml library and
then Junos PyEZ.
Ubuntu
Windows
pip
xcode
libffi-dev
libssl-dev
libxml2-dev
libxslt1-dev
python-dev (required for Python 2)
python3-dev (required for Python 3)
pip
ecdsa
pip
pycrypto

Installing Junos PyEZ on the Configuration Management Server

After you install the prerequisite software on the configuration management server, you can install the
latest release of Junos PyEZ from the Python Package Index (PyPI). You can also download the latest
version of the code from the Junos PyEZ GitHub repository. To install Junos PyEZ from GitHub, you must have Git installed on the configuration management server.
To install the current release of Junos PyEZ from PyPI, execute the following command (use sudo pip
where appropriate):
user@server:~$ sudo pip install junos-eznc
NOTE: To upgrade an existing version of Junos PyEZ, include the -U or --upgrade option when
executing the pip install command.
To install Junos PyEZ from the GitHub project master branch, execute the following command (use sudo
pip where appropriate):
user@server:~$ sudo pip install git+https://github.com/Juniper/py-junos-eznc.git
NOTE: The latest code in the GitHub source repository is under active development and might
not be stable.
NOTE: The pip command name might vary depending on your operating system and version of
Python. Before installing Junos PyEZ using pip, use the pip -V command to display the version, and verify that the pip command corresponds to the version of Python that you are using for Junos PyEZ on your system. If the version is not the same as the Python version used for Junos PyEZ, then try using the pip2 or pip3 commands instead of pip.
For additional information about installing Junos PyEZ, including additional installation options, see the INSTALL file for your specific operating system in the Junos PyEZ GitHub repository at
https://github.com/Juniper/py-junos-eznc.
28

Installing Junos PyEZ in a Python Virtual Environment

As an alternative to installing Python packages globally on a system, you can install the required packages and dependencies for a specific project in an isolated Python virtual environment. After you install the prerequisite software on the configuration management server, you can create a Python virtual environment for your Junos PyEZ project.
For example, to create a virtual Python 3 installation with Junos PyEZ on a Linux or macOS server:
1. Move into your existing project directory or create a new one, if none exists.
user@host:~$ mkdir junos-pyez user@host:~$ cd junos-pyez
2. Create a virtual Python installation and specify its name, which in this case is venv.
user@host:~/junos-pyez$ python3 -m venv venv
NOTE: Ubuntu and Debian systems might require you to install the python3-venv package
before you can create the virtual environment.
3. Activate the virtual environment by executing the script in the virtual environment’s bin directory that
is appropriate for your platform and shell.
user@host:~/junos-pyez$ source venv/bin/activate
(venv) user@host:~/junos-pyez$
4. Install Junos PyEZ.
To install the current release of Junos PyEZ from PyPI, execute the following command:
(venv) user@host:~/junos-pyez$ pip install junos-eznc
To install Junos PyEZ from the GitHub project master branch, execute the following command, which
requires that Git is installed:
(venv) user@host:~/junos-pyez$ pip install git+https://github.com/Juniper/py-junos-eznc.git
29
NOTE: The latest code in the GitHub source repository is under active development and
might not be stable.
5. Execute your Junos PyEZ commands or scripts within the virtual environment.
6. When you are finished working in the virtual environment, deactivate it to return to the main shell prompt.
(venv) user@host:~/junos-pyez$ deactivate user@host:~/junos-pyez$
NOTE: For Python 2 virtual environments, use the virtualenv tool.

Using the Junos PyEZ Docker Image

Docker is a software container platform that is used to package and run an application and its dependencies in an isolated container. Juniper Networks provides a Junos PyEZ Dockerfile as well as Junos PyEZ Docker
images, which are automatically built for every Junos PyEZ release. Starting in Junos PyEZ Release 2.1.8,
the Docker images include Python 3.6. In earlier release, the images include Python 2.7.x.
You can customize and use the Dockerfile to build your own Junos PyEZ Docker image, or you can use one of the prebuilt Docker images, which are stored on Docker Hub, to run Junos PyEZ as a Docker container. You can run the container in interactive mode, as an executable package, or as a terminal.
To use a prebuilt Junos PyEZ Docker image on your configuration management server:
1. Install Docker.
See the Docker website at https://www.docker.com for instructions on installing and configuring Docker on your specific operating system.
2. Download the juniper/pyez Docker image from Docker Hub.
To download the latest image, issue the following command:
user@server:~$ docker pull juniper/pyez
NOTE: The latest Junos PyEZ Docker image is built using the most recently committed
code in the Junos PyEZ source repository, which is under active development and might not be stable.
30
To download a specific image, append the appropriate release tag to the image name, for example,
2.1.2.
user@server:~$ docker pull juniper/pyez:tag
3. Move to the local directory that contains your scripts.
When you run the Docker container, the local scripts are mounted to /scripts in the container.
4. Run the container.
For instructions on running the container, see the official usage examples at DOCKER-EXAMPLES.md.
RELATED DOCUMENTATION
Setting Up Junos PyEZ Managed Nodes | 31
Understanding Junos PyEZ | 18
Junos PyEZ Modules Overview | 21
Authenticating Junos PyEZ Users | 52
Loading...
+ 278 hidden pages