JUNOS Extension Functions Overview

The JUNOS extension functions are used in commit, op and event scripts to accomplish scripting tasks more easily. Extension functions allow you to perform operations that are difficult or impossible to perform in XPath. The functions use the jcs prefix to avoid conflicting with standard XSLT functions. To use functions in the jcs namespace in your scripts, you must map to the jcs namespace in your style sheet declaration as shown in the following examples:

XSLT Syntax

<?xml version=”1.0”?>
<xsl:stylesheet version=”1.0”
         xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0">
    ...
</xsl: stylesheet>

SLAX Syntax

version 1.0;
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

You then include variable declarations, a variable call with the select="jcs:function()" attribute for XSLT scripts or a simple function call for SLAX scripts, and pass along any required or optional arguments.

The JUNOS extension functions are discussed in detail in the following sections:

jcs:break-lines() Function

The jcs:break-lines() function breaks a single element into multiple elements, delimited by newlines. This function is especially useful in dealing with large output elements, such as those returned by the show pfe command. The function syntax is as follows:

var $lines = jcs:break-lines($output);

where:

jcs:close() Function

The jcs:close() function closes a previously opened connection handle. The function syntax is as follows:

expr jcs:close($connection);

where $connection is a connection handle generated by a call to the jcs:open() function.

jcs:dampen() Function

The jcs:dampen() function is used in a script to prevent the same operation from being repeatedly executed. The function returns true or false based on whether the number of calls to the jcs:dampen() function exceeds a max number of calls in a time interval interval. The function parameters include an arbitrary string, $tag, that is used to distinguish different calls to the jcs:dampen() function. This tag is stored in the /var/run directory on the router. The function syntax is as follows:

var $rc = jcs:dampen($tag, max, interval);

where:

The max parameter specifies the maximum number of times that the jcs:dampen() function with the corresponding $tag parameter can be called within the time interval interval before it returns false. In the following example, if the jcs:dampen() function with the tag 'mytag1' is called less than three times in a 10-minute interval, the function returns true. If the function is called more than three times within 10 minutes, it returns false.

if (jcs:dampen('mytag1', 3, 10)) {
    /* Code for situations when jcs:dampen() with */
    /* the tag 'mytag1' is called less than three times */
    /* within 10 minutes */
} else {
     /* Code for situations when jcs:dampen() with */
    /* the tag 'mytag1' exceeds the three call maximum */
    /* limit within 10 minutes */
}

jcs:empty() Function

The jcs:empty() function returns true if a node set or string argument is empty. The function syntax is as follows:

jcs:empty($set)

where $set is the node set or string to test.

The following example shows how the jcs:empty() function can be used:

if (jcs:empty($set)) {
/* Code to handle true value ($set is empty) */
}

jcs:execute() Function

The jcs:execute() function executes an RPC within the context of a specified connection handle. Any number of RPCs can be executed within the context of the specified connection handle until that connection handle is closed with the jcs:close() function. The function syntax is as follows:

var $results = jcs:execute($connection, $rpc);

where:

jcs:first-of() Function

The jcs:first-of() function returns the first nonempty (non-null) item in a list. The function syntax is as follows:

var $result = jcs:first-of(object, "expression")

where:

In the following example, if the value of a is empty, b is checked. If the value of b is empty, c is checked. If the value of c is empty, d is checked. If the value of d is empty, the string "none" is returned.

jcs:first-of($a, $b, $c, $d, "none")

In the following example, the function returns the description of a logical interface. If a logical interface description does not exist, the function returns the description of the (parent) physical interface. If the parent physical interface description does not exist, the function returns the concatenation of the physical interface name with a period (.) and the logical unit number.

<xsl:variable name="description"
        select="jcs:first-of(description, ../description, concat(../name, '.', name))"/>

jcs:get-input() Function

The jcs:get-input() function invokes a CLI prompt and waits for user input. The user input is defined as a string for subsequent use. This function cannot be used with event scripts. The function syntax is as follows:

var $user-input =  jcs:get-input(prompt);

where:

In the following example, the user is prompted to enter a login name. The user’s input is stored in the variable $username:

var $username = jcs:get-input("Enter login id: ");

jcs:get-secret() Function

The jcs:get-secret() function invokes a CLI prompt and waits for user input. Unlike the jcs:get-input() function, the input is not echoed back to the user, which makes the function useful for obtaining a password. The user input is defined as a string for subsequent use. This function cannot be used with event scripts. The function syntax is as follows:

var $user-input = jcs:get-secret(prompt);

where:

The following example shows how to prompt for a password that is not echoed back to the user:

var $password = jcs:get-secret("Enter password: ");

jcs:hostname() Function

The jcs:hostname() function returns the fully qualified domain name associated with an IPv4 or IPv6 address. The function syntax is as follows:

var $name = jcs:hostname($address);

where:

jcs:invoke() Function

The jcs:invoke() function invokes an RPC. The function can be called with one argument, either a string containing a JUNOS XML or JUNOScript RPC method name or a tree containing an RPC. The result is the contents of the <rpc-reply> element, not including the <rpc-reply> tag element itself.

In the following example, there is a test to see if the interface argument is included on the command line when the script is executed. If it is, the operational mode output of the show interfaces terse command is narrowed to include information about that interface only.

<xsl:param name="interface"/>
<xsl:variable name="rpc">
    <get-interface-information>
        <terse/>
        <xsl:if test="$interface">
            <interface-name>
                <xsl:value-of select="$interface"/>
            </interface-name>
        </xsl:if>
    </get-interface-information>
</xsl:variable>
<xsl:variable name="out" select="jcs:invoke($rpc)"/>

In this example, the jcs:invoke() function calls an RPC without modifying the output:

<xsl:variable name="sw" select="jcs:invoke('get-software-information')"/>

jcs:open() Function

The jcs:open() function returns a connection handle that can be used to execute RPCs using the jcs:execute() function. The connection handle is closed with the jcs:close() function. The function syntax is as follows:

var $connection = jcs:open(remote-hostname, username, passphrase) 

where:

The following example shows how to connect to a local device:

var $connection = jcs:open()

The following example shows how to connect to a remote device:

var $connection = jcs:open(remote-hostname)

The following example shows how the user bsmith with a passphrase password obtains a connection handle to the server fivestar:

var $connection = jcs:open("fivestar", "bsmith", "password") 

jcs:output() Function

The jcs:output() function generates unformatted output text. The text appears in the CLI. The function syntax is as follows:

expr jcs:output(string);

where string is the text that is output to the CLI.

For example:

expr jcs:output("The VPN is up.")

jcs:parse-ip() Function

The jcs:parse-ip() function evaluates an IPv4 or IPv6 address and returns an array containing the following information:

The function syntax is as follows:

var $output = jcs:parse-ip("ipaddress/(prefix-length | netmask)");

where:

In the following example, an IPv4 address and an IPv6 address are parsed, and the resulting output is detailed:

var $addr = jcs:parse-ip("10.1.2.10/255.255.255.0");
var $addr = jcs:parse-ip("080:0:0:0:8:800:200C:417A/100");

jcs:printf() Function

The jcs:printf() function generates formatted output text. Most standard printf formats are supported, in addition to some JUNOS Software–specific formats.

The %j1 operator emits the field only if the field was changed from the last time the function was run.

The %jc operator capitalizes the first letter of the format output.

The %jt{TAG} operator emits the tag if the field is not empty.

<xsl:value-of select="jcs:printf('%-24j1s %-5jcs %-5jcs %s%jt{ - -> }s\n',
             'so-0/0/0', 'up', 'down', '10.1.2.3', '')"/>

jcs:progress() Function

The jcs:progress() function issues a progress message containing its single argument to the script log file. The function syntax is as follows:

expr jcs:progress(string);

where string is the text that is output to the script log file.

For example:

expr jcs:progress('Working...');

jcs:regex() Function

The jcs:regex() function evaluates a regular expression against a given string argument and returns any matches. The function syntax is as follows:

var $pattern = "(evaluation-pattern)";
var $output = ($pattern, string)

where:

For example:

var $pattern = "([0-9]+)(:*)([a-z]*)";
var $a = jcs:regex($pattern, "123:xyz");
var $b = jcs:regex($pattern, "r2d2");
var $c = jcs:regex($pattern, "test999!!!");
 
$a[1] == "123:xyz" # string that matches the full reg expression
$a[2] == "123" # ([0-9]+)
$a[3] == ":" # (:*)
$a[4] == "xyz" # ([a-z]*)
$b[1] == "2d" # string that matches the full reg expression
$b[2] == "2" # ([0-9]+)
$b[3] == "" # (:*) [empty match]
$b[4] == "d" # ([a-z]*)
$c[1] == "999" # string that matches the full reg expression
$c[2] == "999" # ([0-9]+)
$c[3] == "" # (:*) [empty match]
$c[4] == "" # ([a-z]*) [empty match]

jcs:sleep() Function

The jcs:sleep() function causes the script to sleep for a specified number of seconds and (optionally) milliseconds. You can use this function to help determine how a routing component works over time. To do this, write a script that issues a command, calls the jcs:sleep() function, and reissues the same command. The function syntax is as follows:

jcs:sleep(seconds, <milliseconds>)

The default time unit is seconds. The milliseconds argument is optional. In the following example, jcs:sleep(1) causes the script to sleep for 1 second, and jcs:sleep(0, 10) causes the script to sleep for 10 milliseconds.

<xsl:value-of select="jcs:sleep(1)"/>
<xsl:value-of select="jcs:sleep(0, 10)"/>

jcs:split() Function

The jcs:split() function splits a string into an array of substrings delimited by the regular expression pattern. The function syntax is as follows:

jcs:split(expression, string, <limit>)

where:

If the optional integer argument limit is specified, the function splits the entire string into limit number of substrings. If there are more than limit number of matches, the substrings include the first limit-1 matches as well as the remaining portion of the original string for the last match.

In the following example, the original string is "123:abc:456:xyz:789". The jcs:split() function breaks this string into substrings that are delimited by the regular expression pattern, which in this case is a colon(:). The optional parameter limit is not specified, so the function returns an array containing all the substrings that are bounded by the delimiter(:).

var $pattern = "(:)";
var $substrings = jcs:split($pattern, "123:abc:456:xyz:789");

returns:

$substrings[1] == "123"
$substrings[2] == "abc"
$substrings[3] == "456"
$substrings[4] == "xyz"
$substrings[5] == "789"

The following example uses the same original string and regular expression as the previous example, but in this case, the optional parameter limit is included. Specifying limit=2 causes the function to return an array containing only two substrings. The substrings include the first match, which is "123" (the same first match as in the previous example) and a second match, which is the remaining portion of the original string after the first occurrence of the delimiter.

var $pattern = "(:)";
var $substrings = jcs:split($pattern, "123:abc:456:xyz:789", 2);

returns:

$substrings[1] == "123"
$substrings[2] == "abc:456:xyz:789"

jcs:sysctl() Function

The jcs:sysctl() function returns a sysctl value as a string or an integer. Use the "i" argument to specify an integer. Use the "s" argument to specify a string. The function syntax is as follows:

var $value = jcs:sysctl(sysctl-value, "s");

where:

For example:

var $value = jcs:sysctl("kern.hostname", "s");

jcs:syslog() Function

The jcs:syslog() function logs messages with a specified priority to the system log file. The function syntax is as follows:

jcs:syslog(priority, message, <message2>, <message3>, ... )

where:

The message argument is a string or variable that is written to the system log file. Optionally, additional strings or variables can be included in the argument list. The message argument is concatenated with any additional parameters, and the concatenated string is written to the system log file. The syslog is specified at the [edit system syslog] hierarchy level of the router configuration file.

The following three examples log pfe messages with an alert priority. The string "mymessage" is output to the system log file. All three examples are equivalent:

expr jcs:syslog("pfe.alert", "mymessage");
 
expr jcs:syslog(161, "mymessage");
 
var $message = "mymessage";
expr jcs:syslog("pfe.alert", $message);

The following example logs pfe messages with an alert priority as in the previous example. In this example, however, there are additional string parameters. For this case, the concatentated string "mymessage mymessage2" is output to the system log file.

expr jcs:syslog("pfe.alert", "mymessage ", "mymessage2");

Table 6: Facility Strings

Facility StringDescriptionNumeric Value

auth

Authorization system

4

change

Configuration change log

22

conflict

Configuration conflict log

21

daemon

Various system processes

3

external

Local external applications

18

firewall

Firewall filtering system

19

ftp

FTP processes

11

interact

Commands executed by the UI

23

pfe

Packet Forwarding Engine

20

user

User processes

1

Table 7: Severity Strings

Severity StringDescriptionNumeric Value

alert

Conditions that should be corrected immediately

1

crit

Critical conditions

2

debug

Debug messages

7

emerg or panic

Panic conditions

0

err or error

Error conditions

3

info

Informational messages

6

notice

Conditions that should be specially handled

5

warn or warning

Warning messages

4

jcs:trace() Function

The jcs:trace() function issues a trace message, which is sent to the trace file.

For example:

<xsl:value-of select="jcs:trace('test')"/>