Required Boilerplate for Op Scripts
Define the boilerplate for op scripts.
You can write Junos OS op scripts in Extensible Stylesheet Language Transformations (XSLT), Stylesheet Language Alternative syntaX (SLAX), or Python. Op scripts must include the necessary boilerplate required for that script language. Include boilerplate 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 you can use in XSLT, SLAX, and Python op scripts.
XSLT Boilerplate for Op Scripts
The XSLT op script boilerplate is as follows:
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="/"> 9 <op-script-results> <!-- ... insert your code here ... --> 10 </op-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.
1 <?xml version="1.0"?>
Line 2 opens the style sheet and specifies the XSLT version as 1.0.
2 <xsl:stylesheet version="1.0"
Lines 3 through 6 list all the namespace mappings commonly used in op scripts. Not all of these prefixes are used in this example. Listing all namespace mappings prevents errors if you use the mappings in later versions of the script.
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 ../import/junos.xsl file, 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. For more information about the named templates, see Understanding Named Templates in Junos OS Automation Scripts.
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. Starting at the root element enables the script to
access all possible Junos XML and Junos XML protocol remote procedure calls (RPCs).
For more information, see XPath Overview.
8 <xsl:template match="/">
After the <xsl:template match="/"> element, the
<op-script-results> and
</op-script-results> container tags must be the top-level
child tags, as shown in Lines 9 and 10.
9 <op-script-results> <!-- ... insert your code here ... --> 10 </op-script-results>
Line 11 closes the template.
11 </xsl:template>
Between Line 11 and Line 12, you can define additional XSLT templates that you call
from within the <xsl:template match="/"> template.
Line 12 closes the style sheet and the op script.
12 </xsl:stylesheet>
SLAX Boilerplate for Op Scripts
The SLAX op script boilerplate is as follows:
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 / {
<op-script-results> {
/*
* Insert your code here
*/
}
}Python Boilerplate for Op Scripts
Python op scripts do not have a required boilerplate. However Python op script must import any objects that the script uses. Python op scripts can import the following:
-
Junos_Contextdictionary—Contains information about the script execution environment. -
jcslibrary—Enables the script to use Junos OS extension functions and named templates in the script. -
jnpr.junosmodule and classes—Enable the script to use Junos PyEZ.
For example:
from junos import Junos_Context 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 still executes correctly if one is present.