Understanding Python Automation Scripts for Junos Devices
Python Scripts Overview
As an alternative to SLAX and XSLT, you can create and execute Python scripts on Junos devices that support the Python extensions package in the software image. Python is a practical, feature-rich language with extensive library support that enables you to create custom scripts. Table 1 outlines the Python versions available on Junos devices and notes the release in which support for that version was added or removed for certain types of scripts.
Python version |
Release |
Change |
Affected Script Types |
---|---|---|---|
Python 2.7.x |
Junos OS Release 16.1R1 and later |
Support added |
Commit, event, op, and SNMP scripts Juniper Extension Toolkit (JET) scripts YANG action and translation scripts |
Junos OS Release 20.2R1 and later |
Support removed |
YANG action and translation scripts |
|
Junos OS Release 21.1R1 and later |
Support removed |
Commit, event, op, and SNMP scripts Juniper Extension Toolkit (JET) scripts |
|
Junos OS Evolved Release 22.3R1 and later |
Support removed |
Commit, event, op, and SNMP scripts Juniper Extension Toolkit (JET) scripts YANG action and translation scripts |
|
Python 3.x |
Junos OS Release 19.4R1 and later |
Support added |
Commit, event, op, and SNMP scripts |
Junos OS Release 20.2R1 and later |
Support added |
Juniper Extension Toolkit (JET) scripts YANG action and translation scripts |
|
Junos OS Evolved Release 21.1R1 and later |
Support added See Feature Explorer for supported platforms. |
Commit, event, op, and SNMP scripts Juniper Extension Toolkit (JET) scripts YANG action and translation scripts |
The Python 2.7 end-of-life and end-of-support date was January 1, 2020. The official upgrade path for Python 2.7 is to Python 3. We recommend that you migrate supported script types from Python 2 to Python 3.
Python scripts can leverage all of the features and constructs in the Python language, which
provides flexibility over SLAX and XSLT and enables you to perform operations that
are difficult or impossible to perform in SLAX and XSLT. SLAX and XSLT are designed
for processing XML data, but Python is suited for processing any format. Python
supports an extensive list of data types, including lists, dictionaries, sets, and
tuples, and many Python variables are mutable, unlike many SLAX and XSLT variables.
SLAX and XSLT scripts have limited loop control, but Python scripts can utilize
statements like break
and continue
to precisely
control loop behavior. Python also has extensive support for file operations, and
you can invoke the standard Python debugger on the command line to debug Python op
scripts..
In addition, Junos OS supports the following in Python scripts:
Junos PyEZ library—simplifies executing RPCs and performing operational and configuration tasks on devices running Junos OS.
lxml
library—simplifies XPath handling.Jinja2 library—template engine that enables you to generate content from predefined templates, which can be useful for generating Junos OS configuration data.
IPv6—Starting in Junos OS Release 19.3R1, devices running Junos OS with upgraded FreeBSD support using IPv6 in Python automation scripts. For more information see IPv6 Support in Python Automation Scripts.
Non-default routing instances—Starting in Junos OS Release 19.3R1, Python scripts on supported devices can use the set_routing_instance() extension function to connect to a remote device through a nondefault routing instance.
Additional Python modules—For information about the modules that are available to Python scripts on devices running Junos OS, see Overview of Python Modules on Junos Devices.
To prevent the execution of unauthorized Python code on devices running Junos OS, unsigned Python scripts must meet certain requirements before you can execute the script on a device. For detailed information about the requirements for executing Python automation scripts on devices running Junos OS, see Requirements for Executing Python Automation Scripts on Junos Devices.
To execute unsigned Python scripts using either Python 2.7 or Python 3, you must configure the
language python
or language python3
statement,
respectively, at the [edit system scripts]
hierarchy level. If you
configure the language python3
statement, the device uses Python 3
to execute scripts that support this Python version, and it uses Python 2.7 to
execute scripts that do not support Python 3 in the given Junos OS release, as noted
in Table 1.
For JET scripts, you must configure the version of the statement
that corresponds to the Python version the script supports. For example,
if you configure the language python3
statement
for a JET script that only supports Python 2.7, you will receive a
runtime error when you execute the script.
Python Script Inputs for Commit, Event, Op, and SNMP Scripts
Devices running Junos OS that support Python automation scripts include the following modules, which you can import into commit, event, op, and SNMP scripts:
jcs
module—provides access to a subset of the extension functions and templates that are available to SLAX and XSLT scripts.For information about Junos OS extension functions and templates, see Understanding Extension Functions in Junos OS Automation Scripts and Understanding Named Templates in Junos OS Automation Scripts.
junos
module—contains theJunos
class and script inputs outlined in Table 2.
Python Object |
Type |
Script Type |
Description |
XSLT/SLAX Equivalent |
---|---|---|---|---|
|
Dictionary |
Commit Event Op |
Device-specific information about the script execution environment such as the device hostname, the script type, and the user executing the script. |
commit-script-input/junos-context event-script-input/junos-context op-script-input/junos-context |
|
lxml.etree _Element |
Commit |
Post-inheritance candidate configuration in Junos XML format. |
commit-script-input/configuration |
|
lxml.etree _Element |
Event |
Details of the event that triggered the corresponding event policy. |
event-script-input/trigger-event |
|
lxml.etree _Element |
Event |
Details of correlated events that occurred before the trigger event. |
event-script-input/received-events |
|
Generator function that produces a sequence of remote devices. |
Event |
Connection details for a local or remote host configured for
an event script at the |
event-script-input/remote-execution-details |
For detailed information about the different script inputs and how to import and use them in automation scripts, see:
Python Script Resources
Table 3 provides resources to help you create and execute Python scripts on devices running Junos OS. Table 4 provides resources for each of the different types of scripts.
Resource |
Documentation |
---|---|
Python script execution requirements |
Requirements for Executing Python Automation Scripts on Junos Devices |
Python modules on devices running Junos OS |
|
IPv6 support in Python scripts |
|
Junos PyEZ documentation |
|
Python programming language documentation |
Script Type |
Documentation |
---|---|
Junos OS commit, event, op, and SNMP Scripts |
|
Juniper Extension Toolkit (JET) scripts |
|
YANG action and translation scripts |