Understanding Python Automation Scripts for Devices Running Junos OS
As an alternative to SLAX and XSLT, you can use Python to author Junos OS commit, event, op, and SNMP scripts on certain devices that include the Python extensions package in the software image. Supported Python versions include Python 2.7.x. In order to execute Python scripts on a device, you must:
Configure the language python statement at the [edit system scripts] hierarchy level
Enable individual scripts by configuring the script filename under the hierarchy level appropriate to the script type
For detailed information about the requirements for executing Python automation scripts on devices running Junos OS, see Requirements for Executing Python Automation Scripts on Devices Running Junos OS.
Python is a practical, feature-rich language with extensive library support that enables you to create customized and readable commit, event, op, and SNMP scripts on devices running Junos OS. Python automation scripts support Junos PyEZ APIs and constructs, which make it easy to execute RPCs and perform operational and configuration tasks on devices running Junos OS. In addition, Python automation scripts can leverage all of the features and constructs in the Python language, which provides increased flexibility over SLAX and XSLT. Python scripts can also leverage the lxml library, which simplifies XPath handling, and the Jinja2 library, which is a template engine that enables you to generate content from predefined templates. For more information about the modules that are available to Python scripts on devices running Junos OS, see Overview of Python Modules on Devices Running Junos OS.
Python 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 all Python variables are mutable,
unlike most SLAX and XSLT variables. Loop control is limited in SLAX
and XSLT scripts, but Python scripts can utilize statements like
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.
Starting in Release 19.3R1, devices running Junos OS with upgraded FreeBSD support using IPv6 in Python automation scripts. IPv6 support enables a Python automation script to establish connections and perform operations using IPv6 addresses.
Devices that support Python automation scripts include the
junos module, which contains the
Junos class and script inputs, and the
which provides access to a subset of the extension functions and templates
that are available to SLAX and XSLT scripts. For more information
about extensions functions and templates, see Understanding Extension Functions in Junos OS Automation Scripts and Understanding Named Templates in Junos OS Automation Scripts.
Junos OS automatically provides input to automation scripts
when they are executed. Table 1 outlines the objects defined in the
junos module that are accessible to the different Python automation scripts.
Table 1: Python Automation Script Input
Device-specific information about the script execution environment such as the device hostname, the script type, and the user executing the script.
Post-inheritance candidate configuration in Junos XML format.
Details of the event that triggered the policy.
Details of correlated events that occurred before the triggering event.
Generator function that produces a sequence of remote devices.
Connection details for a local or remote host configured for an event script at the [edit event-options event-script file filename remote-execution] hierarchy level.
For detailed information about the different script inputs and how to use them in automation scripts, see:
For more information about the Python programming language, see https://www.python.org/.
For more information about Junos PyEZ, see Junos PyEZ.