Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Announcement: Try the Ask AI chatbot for answers to your technical questions about Juniper products and solutions.

close
header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Automation Scripting User Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Required Boilerplate for Event Scripts

date_range 19-Nov-24

Define the boilerplate for event scripts.

Junos OS event scripts can be written in Extensible Stylesheet Language Transformations (XSLT), Stylesheet Language Alternative syntaX (SLAX), or Python. Event scripts must include the necessary boilerplate required for that script language for both basic script functionality as well as any optional functionality used within the script such as the Junos OS extension functions and named templates. This topic provides standard boilerplate that can be used in XSLT, SLAX, and Python event scripts.

SLAX and XSLT event scripts are based on Junos XML and Junos XML protocol tag elements. Like all XML elements, angle brackets enclose the name of a Junos XML or Junos XML protocol tag element in its opening and closing tags. This is an XML convention, and the brackets are a required part of the complete tag element name. They are not to be confused with the angle brackets used in the documentation to indicate optional parts of Junos OS CLI command strings.

XSLT Boilerplate for Event Scripts

The XSLT event script boilerplate is as follows:

content_copy zoom_out_map
 1    <?xml version="1.0" standalone="yes"?>
 2    <xsl:stylesheet version="1.0"
 3        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 4        xmlns:junos="http://xml.juniper.net/junos/*/junos"
 5        xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm"
 6        xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0">
 7        <xsl:import href="../import/junos.xsl"/>
 
 8        <xsl:template match="configuration">
 9            <event-script-results>
                  <!-- ... Insert your code here ... -->
10            </event-script-results>
11        </xsl:template>
          <!-- ... insert additional template definitions here ... -->
12    </xsl:stylesheet>

Line 1 is the Extensible Markup Language (XML) processing instruction (PI). This PI specifies that the code is written in XML using version 1.0. The XML PI, if present, must be the first non-comment token in the script file.

content_copy zoom_out_map
 1    <?xml version="1.0"?>

Line 2 opens the style sheet and specifies the XSLT version as 1.0.

content_copy zoom_out_map
 2    <xsl:stylesheet version="1.0"

Lines 3 through 6 list all the namespace mappings commonly used in event scripts. Not all of these prefixes are used in this example, but it is not an error to list namespace mappings that are not referenced. Listing all namespace mappings prevents errors if the mappings are used in later versions of the script.

content_copy zoom_out_map
 3        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 4        xmlns:junos="http://xml.juniper.net/junos/*/junos" 
 5        xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" 
 6        xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"> 

Line 7 is an XSLT import statement. It loads the templates and variables from the file referenced as ../import/junos.xsl, which ships as part of Junos OS (in the file /usr/libdata/cscript/import/junos.xsl). The junos.xsl file contains a set of named templates you can call in your scripts. These named templates are discussed in Understanding Named Templates in Junos OS Automation Scripts.

content_copy zoom_out_map
 7        <xsl:import href="../import/junos.xsl"/>

Line 8 defines a template that matches the </> element. The <xsl:template match="/"> element is the root element and represents the top level of the XML hierarchy. All XPath expressions in the script must start at the top level. This allows the script to access all possible Junos XML and Junos XML protocol remote procedure calls (RPCs). For more information, see XPath Overview.

content_copy zoom_out_map
 8        <xsl:template match="/">

After the <xsl:template match="/"> tag element, the <event-script-results> and </event-script-results> container tags must be the top-level child tags, as shown in Lines 9 and 10.

content_copy zoom_out_map
 9            <event-script-results>
                <!-- ... insert your code here ... -->
10           </event-script-results>

Line 11 closes the template.

content_copy zoom_out_map
11        </xsl:template>

Between Line 11 and Line 12, you can define additional XSLT templates that are called from within the <xsl:template match="/"> template.

Line 12 closes the style sheet and the event script.

content_copy zoom_out_map
12    </xsl:stylesheet>

SLAX Boilerplate for Event Scripts

The SLAX op script boilerplate is as follows:

content_copy zoom_out_map
version 1.2;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";
 
match / {
    <event-script-results> {
        /*
        * Insert your code here
        */
    }
}

Python Boilerplate for Event Scripts

Python event scripts do not have a required boilerplate, but they must import any objects that are used in the script. Python event scripts can import the following:

  • Junos_Context dictionary—Contains information about the script execution environment.

  • Junos_Trigger_Event and Junos_Received_Events objects—Contain details about the events that triggered the corresponding event policy.

  • Junos_Remote_Execution_Details—Generator function that is required to access remote execution details configured for an event script at the [edit event-options event-script file filename remote-execution] hierarchy level.

  • jcs library—Enables the script to use Junos OS extension functions and Junos OS named template functionality in the script.

  • jnpr.junos module and classes—Enables the script to use Junos PyEZ.

For example:

content_copy zoom_out_map
from junos import Junos_Context
from junos import Junos_Trigger_Event
from junos import Junos_Received_Events
from junos import Junos_Remote_Execution_Details
from jnpr.junos import Device
import jcs

if __name__ == '__main__':

Python automation scripts do not need to include an interpreter directive line (#!/usr/bin/env python) at the start of the script. However, the program will still execute correctly if one is present.

footer-navigation