Navigation
Guide That Contains This Content
[+] Expand All
[-] Collapse All

    Understanding Python Automation Scripts for Devices Running Junos OS

    Starting in Junos OS Release 16.1, 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, and you must 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 Available 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 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.

    Devices that support Python automation scripts include the junos module, which contains the Junos class and script inputs, and the jcs module, which provides access to a subset of the extension functions and templates that are available to SLAX and XSLT 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

    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 policy.

    event-script-input/trigger-event

    Junos_Received_Events

    lxml.etree _Element

    Event

    Details of correlated events that occurred before the triggering 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 more information about the Python programming language, see https://www.python.org/.

    For more information about Junos PyEZ, see Junos PyEZ.

    Modified: 2017-01-17