Global Parameters and Variables in Junos OS Automation Scripts
Junos OS automatically provides input to automation scripts when they are executed. The script can reference this input, which includes device-specific information about the script execution environment such as the device hostname, the script type, and the user executing the script. This information is useful for creating scripts that respond to a variety of complex scenarios.
SLAX and XSLT scripts that import the junos.xsl file can reference this information using the $junos-context
global variable, which is a node-set. The junos.xsl import file also declares several predefined global parameters that
enable the scripts to more easily reference a subset of this information.
Python scripts can reference this information through the junos.Junos_Context
dictionary, which must be imported
into the script.
To use the pre-defined parameters or global variable in SLAX
and XSLT scripts, 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.2; import "../import/junos.xsl";
Python
To reference the information in Python scripts, import
the Junos_Context
dictionary.
from junos import Junos_Context
The script input is described in detail in the following sections:
Global Parameters Available in SLAX and XSLT Scripts
Several predefined global parameters are available for use in SLAX and XSLT automation scripts that import the junos.xsl file. The parameters provide information about the Junos OS environment. Table 1 describes the built-in arguments.
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 |
test.slax |
$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 Available in Automation Scripts
Commit, event, and op scripts can access specific environment
information that is provided to the script upon execution. To access
this information, Python scripts must import and reference the junos.Junos_Context
dictionary, and SLAX and XSLT scripts
that import the junos.xsl file can
reference the $junos-context
global variable. $junos-context
and Junos_Context
contain identical information but in a format that is suitable for
the respective script language.
The $junos-context
variable is a
node-set that 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.
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)psd, rsd (JCS)others |
<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 primary Routing Engine |
||
<routing-engine-name> |
Routing Engine on which the script is executed |
re0 |
|
<script-type> |
Type of script being executed |
op |
|
<tty> |
TTY of the user’s session |
/dev/ttyp1 |
|
<junos-context> <user-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 |
root |
|
<junos-context> <op-context> (op scripts only) |
<via-url> |
Empty element included if the remote op script is executed using
the |
|
<junos-context> <commit-context> (commit scripts only) |
<commit-boot/> |
Empty element included when the commit occurs at boot time |
|
<commit-check/> |
Empty element included when a |
||
<commit-comment> |
User comment regarding the commit |
Commit to fix forwarding issue |
|
<commit-confirm/> |
Empty element included when a |
||
<commit-sync/> |
Empty element included when a |
||
<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 |
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 SLAX 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.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 configuration { if ($junos-context/commit-context/commit-boot) { expr jcs:syslog("daemon.info", "This is boot-time commit"); } else { /* Do this ... */ } }
Python scripts must import the Junos_Context
dictionary from the junos
module to access
the environment information provided to scripts. The names of the
keys in the Junos_Context
dictionary are
identical to the names of the $junos-context
nodes outlined in Table 2. Nodes
with child elements that are nested under the junos-context
node such as user-context
, op-context
, and commit-context
map to items in Junos_Context
, where
the key is the node name and the value is a dictionary of the node’s
child elements. For example:
'user-context': {'login-name': 'bsmith', 'user': 'bsmith', 'class-name': 'j-superuser', 'uid': '9999'}
The following example output displays the Junos_Context
dictionary for an op script that was executed locally. Note that
the op script input contains the op-context
key, which in this scenario is empty.
{'product': 'm7i', 'user-context': {'login-name': 'bsmith', 'user': 'bsmith', 'class-name': 'j-superuser', 'uid': '9999'}, 'routing-engine-name': 're0', 'script-type': 'op', 're-master': None, 'hostname': 'R1', 'pid': '7136', 'tty': '/dev/ttyp1', 'chassis': 'others', 'op-context': ' ', 'localtime': 'Thu Jan 22 11:45:47 2015', 'localtime-iso': '2015-01-22 11:45:47 PST'}
The following example output displays the Junos_Context
dictionary for a commit script that was executed during a commit
check
operation. Note that the commit script input contains
the commit-context
key.
{'product': 'm7i', 'user-context': {'login-name': 'bsmith', 'user': 'bsmith', 'class-name': 'j-superuser', 'uid': '9999'}, 'routing-engine-name': 're0', 'script-type': 'commit', 're-master': None, 'hostname': 'R1', 'pid': '11201', 'tty': '/dev/ttyp1', 'commit-context': {'database-path': '/var/run/db/juniper.db', 'commit-check': None}, 'chassis': 'others', 'localtime': 'Thu Jan 22 16:23:55 2015', 'localtime-iso': '2015-01-22 16:23:55 PST'}
To access individual values in the dictionary, specify the key name. For example:
Junos_Context['hostname'] Junos_Context['user-context']['class-name']