Use Extension Functions in Junos OS Automation Scripts
Junos OS provides extension functions that can be used in commit, op, event, and SNMP scripts to more easily accomplish scripting tasks on devices running Junos OS. The following sections outline how to import and use the extension functions for different script languages:
Using Extension Functions in SLAX and XSLT Scripts
To use the extension functions in SLAX and XSLT scripts, the script must declare the appropriate
namespace Uniform Resource Identifier (URI) in the style sheet
declaration. Junos OS extension functions, which have functionality that is
specific to devices running Junos OS, are defined in the namespace with the
associated URI http://xml.juniper.net/junos/commit-scripts/1.0
.
SLAX extension functions are defined in the namespace with the associated URI
http://xml.libslax.org/slax
.
SLAX and XSLT scripts generally map the jcs
or slax
prefix to its respective URI
to avoid name conflicts with standard XSLT functions and user-defined templates. The scripts then qualify the
extension functions with the appropriate prefix, which is expanded
into its associated URI reference during processing.
For example, the following SLAX namespace statement maps
the jcs
prefix to the namespace URI that
defines the Junos OS extension functions used in automation scripts:
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
The following SLAX namespace statement maps the slax
prefix to the namespace URI that defines SLAX
extension functions:
ns slax = "http://xml.libslax.org/slax";
To call an extension function in a SLAX or XSLT script, include
any required variable declarations, call the function using jcs:function-name()
or slax:function-name()
as appropriate,
and pass along any required or optional arguments. Arguments must
be passed into the function in the precise order specified by the
function definition. This is different from a template, where the
parameters are assigned by name and can appear in any order. The return
value of an extension function must always either be assigned to a
variable or designated as output.
The following example maps the jcs
prefix to the namespace identified by the URI
http://xml.juniper.net/junos/commit-scripts/1.0
. The script
then calls the jcs:invoke()
function with one argument.
XSLT Syntax
<?xml version=”1.0”?> <xsl:stylesheet version=”1.0” xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"> ... <xsl:variable name="result" select="jcs:invoke('get-software-information')"/> ... </xsl: stylesheet>
SLAX Syntax
version 1.2; ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; ... var $result = jcs:invoke('get-software-information'); ...
The following example maps the slax
prefix to the namespace identified by the
URI http://xml.libslax.org/slax
. The script then calls the
slax:get-input()
function with one string argument.
SLAX Syntax
version 1.2; ns slax = "http://xml.libslax.org/slax"; ... var $input = slax:get-input($prompt); ...
Using Extension Functions in Python Scripts
Python automation scripts that import the jcs
module can use a Python version of supported Junos OS and SLAX
extension functions. To determine which extension functions are supported
in Python scripts, see Understanding Extension Functions in Junos OS Automation Scripts.
To call the equivalent extension function in a Python script, first include the import
jcs
statement and any required variable declarations. Then call the
function using jcs.function_name()
, and pass
along any required or optional arguments. Note that in Python scripts, the
extension function names must use underscores instead of hyphens. For
example:
Python Syntax
import jcs if __name__ == '__main__': name = jcs.get_input("Enter name: ") jcs.output(name)