Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

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.

Table 1: Python Versions on Devices Running Junos OS

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

Note:

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:

Table 2: junos Module Objects

Python Object

Type

Script Type

Description

XSLT/SLAX Equivalent

Junos_Context

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

Junos_Configuration

lxml.etree _Element

Commit

Post-inheritance candidate configuration in Junos XML format.

commit-script-input/configuration

Junos_Trigger_Event

lxml.etree _Element

Event

Details of the event that triggered the corresponding event policy.

event-script-input/trigger-event

Junos_Received_Events

lxml.etree _Element

Event

Details of correlated events that occurred before the trigger event.

event-script-input/received-events

Junos_Remote_Execution_Details

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 [edit event-options event-script file filename remote-execution] hierarchy level.

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.

Table 3: General Resources

Resource

Documentation

Python script execution requirements

Requirements for Executing Python Automation Scripts on Junos Devices

Python modules on devices running Junos OS

Overview of Python Modules on Junos Devices

IPv6 support in Python scripts

IPv6 Support in Python Automation Scripts

Junos PyEZ documentation

Junos PyEZ

Python programming language documentation

https://www.python.org/

Table 4: Resources by Script Type

Script Type

Documentation

Junos OS commit, event, op, and SNMP Scripts

Junos OS Automation Scripting User Guide

Juniper Extension Toolkit (JET) scripts

Juniper Extension Toolkit documentation

YANG action and translation scripts

NETCONF XML Management Protocol Developer Guide