Supported Platforms
Related Documentation
- ACX, EX, M, MX, SRX, T Series
- Junos Script Automation: Understanding Extension Functions in the jcs and slax Namespaces
- SLAX Parameters Overview
- XSLT Parameters Overview
- XSLT Variables Overview
- ACX, EX, M, MX, PTX, SRX, T Series
- Junos Script Automation: Named Templates in the jcs Namespace Overview
- SLAX Variables Overview
Junos Script Automation: Global Parameters and Variables in the junos.xsl File
The junos.xsl
import file declares several predefined parameters and a global
variable of type node-set, which provide information about the Junos
OS environment that is useful for creating scripts that respond to
a variety of complex scenarios. The global parameters and variable
are available for use in any commit, op, or event script that imports
the junos.xsl
file.
To use the parameters or variable in a script, you must import
the junos.xsl
file by including the <xsl:import> tag in the style sheet declaration
of an XSLT script or by including the import statement in a SLAX script and specifying the junos.xsl
file location as shown in the following
sample code:
XSLT Syntax
<?xml version="1.0"?>
<xsl:stylesheet version="1.0">
<xsl:import href="../import/junos.xsl"/>
...
</xsl: stylesheet>
SLAX Syntax
version 1.0;
import "../import/junos.xsl";
The default arguments are described in detail in the following sections:
Global Parameters
Several predefined global parameters are available for use in commit, op, and event scripts. The parameters provide information about the Junos OS environment. Table 1 describes the built-in arguments.
Table 1: Predefined Parameters Available to Automation Scripts
Name | Description | Example |
---|---|---|
$hostname | Hostname of the local device | Tokyo |
$localtime | Local time when the script is executed | Fri Dec 10 11:42:21 2010 |
$localtime-iso | Local time, in ISO format, when the script is executed | 2010-12-10 11:42:21 PST |
$product | Model of the local device | m10i |
$script | Filename of the executing script |
|
$user | Local name of the user executing the script | root |
The predefined global parameters are declared in the junos.xsl
file. You do not need to declare these
parameters in a script in order to use them. Access the value of the
global parameters in a script by prefixing the parameter name with
the dollar sign ($), as shown in the following example:
SLAX syntax:
if ($user != "root") { var $script-message = $user _ " does not have permission to execute " _ $script; expr jcs:output($script-message); }
XSLT syntax:
<xsl:if test="$user != 'root'"> <xsl:variable name="script-message"
select="concat($user, ' does not have permission to execute ', $script)"/> <xsl:value-of select="jcs:output($script-message)"/> </xsl:if>
Global Variable
Starting with Junos OS Release 11.1, Junos OS also provides
a single global variable, $junos-context, which is accessible for use in all commit, op, or event scripts
that import the junos.xsl
file. The $junos-context variable is a node-set, which has elements
that mirror the original global parameters described in Global Parameters as well as additional
elements with information about the Junos OS environment, such as
whether a script is executed on the master Routing
Engine.
The $junos-context variable contains the <junos-context> node and the following hierarchy, which is common to and embedded in the source tree of all scripts:
<junos-context> <chassis></chassis> <hostname></hostname> <localtime></localtime> <localtime-iso></localtime-iso> <pid></pid> <product></product> <re-master/> <routing-engine-name></routing-engine-name> <script-type></script-type> <tty></tty> <user-context> <class-name></class-name> <login-name></login-name> <uid></uid> <user></user> </user-context> </junos-context>
Additionally, script-specific information is available depending on the type of script executed. For op scripts, the <op-context> element is also included in the source tree provided to an op script:
<junos-context> <op-context> <via-url/> </op-context> </junos-context>
For commit scripts, the <commit-context> element is also included in the source tree provided to a commit script:
<junos-context> <commit-context> <commit-comment>"This is a test commit"</commit-comment> <commit-boot/> <commit-check/> <commit-sync/> <commit-confirm/> <database-path/> </commit-context> </junos-context>
Table 2 identifies each node of the $junos-context variable node-set, provides a brief description of the node, and gives examples of values for any elements that are not input to a script as an empty tag.
Table 2: Global Variable $junos-context Available to Automation Scripts
Parent Node | Node | Description | Example content |
---|---|---|---|
<junos-context> | <chassis> | Specifies whether the script is executed on a component of a routing matrix, the Root System Domain (RSD), or a Protected System Domain (PSD) | scc, lcc (TX Matrix) |
<hostname> | Hostname of the local device | Tokyo | |
<localtime> | Local time when the script is executed | Fri Dec 10 11:42:21 2010 | |
<localtime-iso> | Local time, in ISO format, when the script is executed | 2010-12-10 11:42:21 PST | |
<pid> | cscript process ID | 5257 | |
<product> | Model of the local device | m10i | |
<re-master/> | Empty element included if the script is executed on the master Routing Engine | ||
<routing-engine-name> | Routing Engine on which the script is executed | re0 | |
<tty> | TTY of the user’s session | /dev/ttyp1 | |
<script-type> | Type of script being executed | op | |
<junos-context> | <class-name> | Login class of the user executing the script | superuser |
<login-name> | Login name of the user executing the script. For AAA access, this is the RADIUS/TACACS username. | jsmith | |
<uid> | User ID number of the user executing the script as defined in the device configuration | 2999 | |
<user> | Local name of the user executing the script. Junos OS uses the local name for authentication. It might differ from the login-name used for AAA authentication. | root | |
<junos-context> (op scripts only) | <via-url> | Empty element included if the remote op script is executed using the op url command | |
<junos-context> (commit scripts only) | <commit-boot/> | Empty element included when the commit occurs at boot time | |
<commit-check/> | Empty element included when a commit check is performed | ||
<commit-comment> | User comment regarding the commit | Commit to fix forwarding issue | |
<commit-confirm/> | Empty element included when a commit confirmed is performed | ||
<commit-sync/> | Empty element included when a commit synchronize is performed | ||
<database-path/> | Element specifying the location of the session’s pre-inheritance candidate configuration. For normal configuration sessions, the value of the element is the location of the normal candidate database. For private configuration sessions, the value of the element is the location of the private candidate database. When the <get-configuration> database-path attribute is set to this value, the commit script retrieves the corresponding pre-inheritance candidate configuration. |
The $junos-context variable is a node-set. Therefore, you can access the child elements throughout a script by including the proper XPath expression. The following example commit script writes a message to the system log file if the commit is performed during initial boot-up. The message is given a facility value of daemon and a severity value of info. For more information, see syslog().
version 1.0; 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 configuration { if ($junos-context/commit-context/commit-boot) { expr jcs:syslog("daemon.info", "This is boot-time commit"); } else { /* Do this ... */ } }
Related Documentation
- ACX, EX, M, MX, SRX, T Series
- Junos Script Automation: Understanding Extension Functions in the jcs and slax Namespaces
- SLAX Parameters Overview
- XSLT Parameters Overview
- XSLT Variables Overview
- ACX, EX, M, MX, PTX, SRX, T Series
- Junos Script Automation: Named Templates in the jcs Namespace Overview
- SLAX Variables Overview
Published: 2013-03-05
Supported Platforms
Related Documentation
- ACX, EX, M, MX, SRX, T Series
- Junos Script Automation: Understanding Extension Functions in the jcs and slax Namespaces
- SLAX Parameters Overview
- XSLT Parameters Overview
- XSLT Variables Overview
- ACX, EX, M, MX, PTX, SRX, T Series
- Junos Script Automation: Named Templates in the jcs Namespace Overview
- SLAX Variables Overview