Topic: tech juniper jaut prev next

tech juniper jaut > Module 06: SLAX (Stylesheet Language Alternative Syntax)

Module 06: SLAX (Stylesheet Language Alternative Syntax)

Commit, op, event, SNMP scripts are supported on-box.

Op scripts add commands to the CLI. They can display information and change the configuration as needed. They can be used for troubleshooting, for instance, by running ‘ping’, ‘show interfaces’, ‘show bgp …’.

When the user commits a configuration, the candidate configuration is processed through each of the commit scripts. Each commit script can make changes to the configuration, or generate a warning or error. A commit script can, for example, check connectivity to a server and refuse to make changes if connectivity is lost.

Use the ‘apply-macro’ configuration command to expand a configuration.

Event scripts are triggered when a specific event occurs, for example, if an account is deleted on r1, the script could delete the account with the same name on r2. Event scripts can gather and send more detailed information about events and take automatic actions to resolve events.

SNMP scripts are called by an SNMP manager calling an SNMP agent. They are used to create and handle custom OID’s.

Three scripting languages are supported: XSLT, SLAX and Python.

Match templates specify the XML elements to which they apply. This script will print ‘Found’ when a ‘configuration’ element is found.

match configuration {
    <output> "Found";
}

Named templates are executed when called as functions. They are called using the ‘call’ command from other templates.

match configuration {
    call print-message($elementname="configuration");
}
template print-message($elementname="unknown") {
    <output> "Found " _ $elementname;
}

XML elements are represented by an opening element, followed by the content in curly braces.

<configuration> {
    <version> "...";
    <telnet>;
}

Op scripts should write to the element;

match / {
    <op-script-results> {
        <output> "Hello";
    }
}

Variables are immutable, except those created with the ‘mvar’ keyword (SLAX 1.1 and later);

var $hello="World";

The variable ‘$new-hostname’ has result tree fragment as its type.

var $new-hostname = {
    if (system/host-name) {
        <data> system/host-name;
    }
    else {
        <data> "unknown";
    }
}

Variables are scoped inside code blocks. Global variables are available everywhere. ‘expr’ is used to evaluate XPath.

var $globalvariable {
    expr name;
}

The XML output of a command, such as a function call, can be stored in a variable, and then accessed as XPath.

var $swinfo = jcs:invoke("get-software-information");
<output> $swinfo/product-model;

Type conversion happens automatically, with the exception of result tree fragment to node set. XPath works on node set but not on result tree fragment. Result tree fragment can be converted to node set using the ‘:=’ operator.

var $xml := <outer> {
                ...
}
<output> $xml/outer/...

If statements; empty node sets convert to false, non-empty node sets convert to true.

if ($results//name) {
    <output> "Found a name";
}
/* may be followed by 'else' or 'else if' */

For each statements operate over a set of nodes.

for-each ($config/system/syslog/file) {
    /* print the value of the 'name' node within each 'file' */
    <output> name;
}

var $results = jcs:invoke(<get-configuration changed="changed">);
for-each ($results/system/scripts/commit/file[@junos:changed]) {
    /* print the name of each 'file' config item that has been changed */
    <output> name;
}

Junos extension and template library provides additional functions (templates). The result must be designated as output or assigned to a variable.

JUISE can execute SLAX locally, without a Junos box. It helps with the software development lifecycle.

jcs:output sends output to the CLI while the script is running. sends the output after the script has completed.