Extension Functions in the jcs and slax Namespaces
The Junos extension functions are discussed in detail in the following sections:
- base64-decode() Function (slax Namespace)
- base64-encode() Function (slax Namespace)
- break-lines() Function (jcs and slax Namespaces)
- close() Function (jcs Namespace)
- dampen() Function (jcs and slax Namespaces)
- document() Function (slax Namespace)
- empty() Function (jcs and slax Namespaces)
- evaluate() Function (slax Namespace)
- execute() Function (jcs Namespace)
- first-of() Function (jcs and slax Namespaces)
- get-command() Function (jcs and slax Namespaces)
- get-hello() Function (jcs Namespace)
- get-input() Function (jcs and slax Namespaces)
- get-protocol() Function (jcs Namespace)
- get-secret() Function (jcs and slax Namespaces)
- hostname() Function (jcs Namespace)
- invoke() Function (jcs Namespace)
- open() Function (jcs Namespace)
- output() Function (jcs and slax Namespaces)
- parse-ip() Function (jcs Namespace)
- printf() Function (jcs and slax Namespaces)
- progress() Function (jcs and slax Namespaces)
- regex() Function (jcs and slax Namespaces)
- sleep() Function (jcs and slax Namespaces)
- split() Function (jcs and slax Namespaces)
- sysctl() Function (jcs and slax Namespaces)
- syslog() Function (jcs and slax Namespaces)
- trace() Function (jcs and slax Namespaces)
base64-decode() Function (slax Namespace)
Namespaces
SLAX Syntax
Release Information
Function introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.
Description
Decode BASE64 encoded data. BASE64 is a means of encoding arbitrary data into a radix-64 format that is more easily transmitted, typically using STMP or HTTP.
Include the optional control string argument to replace any non-XML control characters in the decoded string with the specified string. If the argument is an empty string, non-XML characters are removed. The decoded data is returned to the caller.
Parameters
control-string | — | (Optional) String to replace non-XML control characters in the decoded string. Use an empty string argument to remove the non-XML characters. |
string | — | BASE64 encoded data. |
Return Value
string | — | Decoded data. |
— |
Usage Examples
var $real-data = slax:base64-decode($encoded-data, "@");
base64-encode() Function (slax Namespace)
Namespaces
SLAX Syntax
Release Information
Function introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.
Description
Encode a string of data in the BASE64 encoding format. BASE64 is a means of encoding arbitrary data into a radix-64 format that is more easily transmitted, typically using STMP or HTTP.
Parameters
string | — | Input data string. |
Return Value
string | — | Encoded data. |
Usage Examples
var $encoded-data = slax:base64-encode($real-data);
break-lines() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Break a simple element into multiple elements, delimited by newlines. This is especially useful for large output elements such as those returned by the show pfe command.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
expression | — | Original output. |
Return Value
$lines | — | Output broken up into lines. |
Usage Examples
var $lines = jcs:break-lines($output);
for-each ($lines) {
...
}
close() Function (jcs Namespace)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 9.3.
Description
Close a previously opened connection handle.
Parameters
connection | — | Connection handle generated by a call to the jcs:open() function. |
Usage Examples
The following example closes the connection handle $connection, which was originally generated by a call to the jcs:open() function:
var $connection = jcs:open(); ... var $result = jcs:close($connection);
dampen() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 9.4.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Prevent the same operation from being repeatedly
executed within a script. The dampen() function
returns false if the number of calls to
the jcs:dampen() function exceeds a max number of calls in the time
interval interval.
Otherwise the function returns true. The
function parameters include an arbitrary string that is used to distinguish
different calls to the jcs:dampen() function.
This tag is stored in the /var/run
directory on the device.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
interval | — | Time interval, in minutes. |
max | — | Maximum number of calls to the jcs:dampen() function with a given tag allowed before the function returns false. This limit is based on the number of calls within a specified time interval. |
tag-string | — | Arbitrary string used to distinguish different calls to the jcs:dampen() function. |
Return Value
result | — | Boolean value based on the number of calls to jcs:dampen() with a given tag and within a specified time. If the number of calls for a given tag exceeds max, the return value is false. If the number of calls is less than max, the return value is true. |
Usage Examples
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, the function 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 */
}
document() Function (slax Namespace)
Namespaces
SLAX Syntax
Release Information
Function introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.
Description
Read data from a file or URL. The data can be encoded in any character set and can be BASE64 encoded. The default character set is "utf‑8". Optional arguments specify the character encoding scheme and the encoding format, and define the replacement string for non-XML control characters. Table 1 lists the available options.
Parameters
options | — | (Optional) Specify the character encoding scheme and format of the data, and define the replacement string for non-XML control characters. |
url | — | File or URL from which to read data. Table 1: Options for slax:document Function
|
Return Value
string | — | String representing the data. |
Usage Examples
var $data = slax:document($url); var $options := { <encoding> "ascii"; <format> "base64"; <non-xml> "#"; } var $data2 = slax:document($url, $options);
empty() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Test for the presence of a value and return true if the node set or string argument evaluates to an empty value.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
(node-set | string) | — | Argument to test for the presence of a value. |
Return Value
result | — | Boolean value, which is true if the argument is empty. |
Usage Examples
In the following example, if $set is empty, the script executes the enclosed code block:
if ( jcs:empty($set) ) {
/* Code to handle true value ($set is empty) */
}
The following example tests whether the description node for interface fe-/0/0/0 is empty. If the description is missing, a <message> tag is output.
if (jcs:empty(interfaces/interface[name="fe-0/0/0"]/description)) { <message> "interface " _ name _ " is missing description"; }
evaluate() Function (slax Namespace)
Namespaces
SLAX Syntax
Release Information
Function introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.
Description
Evaluate a SLAX expression and return the results of the expression. This permits expressions using the extended syntax provided by SLAX in addition to what is allowed in XPath.
Parameters
expression | — | SLAX expression to evaluate. |
Return Value
object | — | Result of the expression. |
Usage Examples
var $result = slax:evaluate("expr[name == '&']");
execute() Function (jcs Namespace)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 9.3.
Description
Execute a remote procedure call (RPC) within the context of a specified connection handle. Any number of RPCs may be executed within the context of the connection handle until it is closed with the jcs:close() function.
Parameters
connection | — | Connection handle generated by a call to the jcs:open() function. |
rpc | — | Remote procedure call (RPC) to execute. |
Return Value
result | — | Results of the executed RPC, which include the contents of the <rpc-reply> element, but not the <rpc-reply> tag itself. This $result variable is the same as that produced by the jcs:invoke() function. By default, the results are in XML format equivalent to the output produced with the | display xml option in the CLI. |
Usage Examples
In the following example, the $rpc variable is declared and initialized with the Junos XML <get-interface-information> element. A call to the jcs:open() function generates a connection handle to the remote device at IP address 10.10.10.1. The user’s login and password are provided as arguments to jcs:open() to provide access to the remote device. The code calls jcs:execute() and passes in the connection handle and RPC as arguments. Junos OS on the remote device processes the RPC and returns the results, which are stored in the $results variable.
var $rpc = <get-interface-information>; var $connection = jcs:open('10.10.10.1', 'bsmith', 'test123'); var $results = jcs:execute($connection, $rpc); expr $results;
The equivalent XSLT code is:
<xsl:variable name="connection" select="jcs:open('10.10.10.1', 'bsmith', 'test123')"/> <xsl:variable name="rpc"> <get-interface-information/> </xsl:variable> <xsl:variable name="results" select="jcs:execute($connection, $rpc)"/> <xsl:value-of select="$results"/>
first-of() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Return the first nonempty (non-null) item in a list. If all objects in the list are empty, the default expression is returned. This function provides the same functionality as an if / else-if / else construct but in a much more concise format.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
expression | — | Default value returned if all objects in the list are empty. |
object | — | List of objects. |
Return Value
result | — | First nonempty (non-null) item in the object list. If all objects in the list are empty, the default expression is returned. |
Usage Examples
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, for each physical interface, the script checks for a description of each 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 a message that no description was found.
var $rpc = <get-interface-information>; var $results = jcs:invoke($rpc); for-each ($results/physical-interface/logical-interface) { var $description = jcs:first-of(description, ../description, "no description found"); }
The equivalent XSLT code is:
<xsl:variable name="rpc"> <get-interface-information/> </xsl:variable> <xsl:variable name="results" select="jcs:invoke($rpc)"/> <xsl:for-each select="$results/physical-interface/logical-interface"> <xsl:variable name="description"
select="jcs:first-of(description, ../description, 'no description found')"/> </xsl:for-each>
The code for the description variable declaration in the previous examples would be equivalent to the following more verbose if / else-if / else construct:
var $description = { if (description) { expr description; } else if (../description) { expr ../description; } else { expr "no description found"; } }
See also Example: Displaying DNS Hostname Information Using an Op Script.
get-command() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
Release Information
Function introduced in version 1.1 of the SLAX language, which is supported in Junos OS Release 12.2 and later releases.
Description
Prompt the user for input and return the input as a string. If the readline (or libedit) library was found at install time, the return string is entered in the readline history, and will be available using the readline history keystrokes (Ctrl+P and Ctrl+N).
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
string | — | Prompt text. |
Return Value
string | — | Command text entered by the user. |
Usage Examples
var $response = slax:get-command("# ");
get-hello() Function (jcs Namespace)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 11.4.
Description
Return the session ID and the capabilities of the NETCONF server during a NETCONF session.
During session establishment, the NETCONF server and client application each emit a <hello> element to specify which operations, or capabilities, they support from among those defined in the NETCONF specification or published as proprietary extensions. The <hello> element encloses the <capabilities> element and the <session-id> element, which specifies the session ID for this NETCONF session.
Within the <capabilities> element, a <capability> element specifies each supported function. Each capability defined in the NETCONF specification is represented by a uniform resource name (URN). Capabilities defined by individual vendors are represented by uniform resource identifiers (URIs), which can be URNs or URLs.
Parameters
connection | — | Connection handle generated by a call to the jcs:open() function. |
Return Value
capabilities | — | XML node set that specifies which operations, or capabilities, the NETCONF server supports. The node set also includes the session ID. |
Usage Examples
In the following code snippet, the user, bsmith, establishes a NETCONF session on the default port with the remote device, fivestar, which is running Junos OS. Since the code does not specify a value for the password, the user is prompted for a password during script execution. Once authentication is established, the code calls the jcs:get-hello() function and stores the return value in the variable $hello, which is then printed to the CLI.
var $netconf := { <method> "netconf"; <username> "bsmith"; } var $connection = jcs:open("fivestar", $netconf); var $hello = jcs:get-hello($connection); expr jcs:output($hello); expr jcs:close($connection);
The CLI displays the following output:
bsmith@fivestar's password: urn:ietf:params:xml:ns:netconf:base:1.0 urn:ietf:params:xml:ns:netconf:capability:candidate:1.0 urn:ietf:params:xml:ns:netconf:capability:confirmed-commit:1.0 urn:ietf:params:xml:ns:netconf:capability:validate:1.0 urn:ietf:params:xml:ns:netconf:capability:url:1.0?protocol=http,ftp,file http://xml.juniper.net/netconf/junos/1.0 http://xml.juniper.net/dmi/system/1.0 20847
get-input() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 9.4.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Invoke a CLI prompt and wait for user input. The user input is defined as a string for subsequent use. If the script is run non-interactively, the function returns an empty value. This function cannot be used with event scripts.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
string | — | CLI prompt text. |
Return Value
user-input | — | Text typed by the user and stored as a string. The return value will be empty if the script is run non-interactively. |
Usage Examples
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: ");
get-protocol() Function (jcs Namespace)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 11.4.
Description
Return the session protocol associated with the connection handle. The protocol values are junoscript, netconf, and junos-netconf.
Parameters
connection | — | Connection handle generated by a call to the jcs:open() function. |
Return Value
protocol | — | Session protocol associated with the connection handle. The values are junoscript, netconf, and junos-netconf. |
Usage Examples
In the following code snippet, the user, bsmith, establishes a NETCONF session on the default port with the remote device, fivestar. Since the code does not specify a value for the password, the user is prompted for a password during script execution. Once authentication is established, the code calls the jcs:get-protocol() function and stores the return value in the variable $protocol, which is then printed to the CLI.
var $netconf := { <method> "netconf"; <username> "bsmith"; } var $connection = jcs:open("fivestar", $netconf); var $protocol = jcs:get-protocol($connection); expr jcs:output($protocol); expr jcs:close($connection);
The CLI displays the following output:
bsmith@fivestar's password: netconf
get-secret() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 9.5R2.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Invoke a CLI prompt and wait 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 passwords. The user input is defined as a string for subsequent use. This function cannot be used with event scripts.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
string | — | CLI prompt text. |
Return Value
user-input | — | Text typed by the user and stored as a string. |
Usage Examples
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: ");
hostname() Function (jcs Namespace)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 9.3.
Description
Return the fully qualified domain name associated with a given IPv4 or IPv6 address. The DNS server must be configured on the device in order to resolve the domain name.
Parameters
expression | — | IPv4 or IPv6 address. |
Return Value
name | — | Hostname associated with the IP address. |
Usage Examples
The following example initializes the variable address with the IP address 10.10.10.1. The $address variable is passed as the argument to the jcs:hostname() function. If the DNS server is configured on the device, the function will resolve the IP address and return the fully qualified domain name, which is stored in the variable host.
var $address = "10.10.10.1"; var $host = jcs:hostname($address);
In XSLT:
<xsl:variable name="address" select="10.10.10.1"> <xsl:variable name="host" select="jcs:hostname($address)"/>
invoke() Function (jcs Namespace)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6.
Description
Invoke a remote procedure call (RPC) on the local device. The function is called with one argument, either a string containing a Junos XML API RPC, or a tree containing an RPC. The result contains the contents of the <rpc-reply> element, not including the <rpc-reply> tag. An RPC allows you to perform functions equivalent to Junos OS operational mode commands.
Parameters
rpc | — | String containing a Junos XML API RPC or a tree containing an RPC. |
Return Value
result | — | Results of the executed RPC, which include the contents of the <rpc-reply> element, but not the <rpc-reply> tag itself. By default, the results are in XML format equivalent to the output produced with the | display xml option in the CLI. |
Usage Examples
The following example tests to see if the interface argument is included on the command line when the script is executed. If the argument is provided, the output of the show interfaces terse operational mode command is narrowed to include only information about the specified interface.
<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 the Junos XML API RPC get-software-information, and stores the unmodified output in the variable sw:
<xsl:variable name="sw" select="jcs:invoke('get-software-information')"/>
open() Function (jcs Namespace)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 9.3.
Support for NETCONF sessions added in Junos OS Release 11.4R1.
Support for routing instances added in Junos OS Release 12.2R1.
Description
Return a connection handle that can be used to execute remote procedure calls (RPCs) using the jcs:execute() extension function. To execute an RPC on a remote device, an SSH session must be established. In order for the script to establish the connection, you must either configure the SSH host key information for the remote device on the local device where the script will be executed, or the SSH host key information for the remote device must exist in the known hosts file of the user executing the script.
To redirect the SSH connection to originate from within a specific routing instance, include the routing instance name in the connection parameters. The routing instance must be configured at the [edit routing-instances] hierarchy level, and the remote device must be reachable either using the routing table for that routing instance or from one of the interfaces configured under that routing instance.
Starting with Junos OS Release 11.4, the new parameter, session-options, supports the option to create a session either with the Junos XML protocol server on devices running Junos OS or with the NETCONF server on devices where NETCONF service over SSH is enabled. Previously, the function supported only sessions with the Junos XML protocol server on devices running Junos OS.
The connection handle is closed with the jcs:close() function.
Parameters
passphrase | — | (Optional) User’s login passphrase. If you do not specify a passphrase and it is required for authentication, you should be prompted for one during script execution by the device to which you are connecting. |
remote-hostname | — | Domain name or IP address of the remote router, switch, or security device. If you are opening a local connection, do not pass this value. If you specify a session type, this parameter is required. |
routing-instance-name | — | (Optional) Routing instance from within which the SSH connection originates. |
session-options | — | (Optional) XML node set that specifies the session protocol and connection parameters. The structure of the node set is: var $session-options := { <instance> "routing-instance-name"; <method> ("junoscript" | "netconf" | "junos-netconf"); <passphrase> "passphrase"; <password> "password"; <port> "port-number"; <routing-instance> "routing-instance-name"; <username> "username"; }
|
username | — | (Optional) User’s login name. If you do not specify a username and it is required for the connection, the script uses the local name of the user executing the script. |
Return Value
connection | — | Connection handle to the remote host. |
Usage Examples
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 the passphrase “test123” obtains a connection handle to the remote device, fivestar:
var $connection = jcs:open("fivestar", "bsmith", "test123");
The following example shows how the user, bsmith, with the passphrase “test123” creates a junos-netconf session with a device running Junos OS:
var $options := { <method> "junos-netconf"; <username> "bsmith"; <passphrase> "test123"; } var $connection = jcs:open("fivestar", $options);
output() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Display one or more lines of output text, either to the CLI user (when used in op scripts), or to the output file (when used in event scripts). The function can be called with either a single string argument or with multiple string arguments. Multiple arguments are concatenated into one combined string. A newline terminates the output text.
jcs:output() is not supported in commit scripts. Commit scripts use the <xnm:warning> and <xnm:error> result tree elements to display text to the CLI user.
The behavior of jcs:output() differs from the<output> result tree element in that jcs:output() displays its text immediately, rather than waiting until the conclusion of the script. This makes it suitable for scripts where user interaction is required, such as when the jcs:get-input() function is used, or when status messages should be displayed in the midst of script processing. While jcs:output() does return a node set, it is always empty and can be ignored. Therefore, the jcs:output() function is normally called with the expr statement, rather than assigning its result to a variable.
The following escape characters are supported in the output text:
- \\ –Backslash (as of Junos OS Release 10.2)
- \r –Carriage Return
- \" –Double-quote (as of Junos OS Release 10.1R2)
- \n –Newline
- \' – Single-quote
- \t –Tab
Starting with Junos OS Release 10.2, the maximum length for output text is 10 KB, and longer strings are truncated to the supported length.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
string | — | Text that is output immediately to the CLI session. |
Usage Examples
SLAX syntax:
expr jcs:output('The VPN is up.');
XSLT syntax:
<xsl:value-of select="jcs:output('The VPN is up.')"/>
parse-ip() Function (jcs Namespace)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 9.0.
Description
Parse an IPv4 or IPv6 address.
Parameters
ipaddress | — | IPv4 or IPv6 address. |
netmask | — | Netmask defining the network prefix portion of the address. |
prefix-length | — | Prefix length defining the number of bits used in the network prefix portion of the address. |
Return Value
result | — | An array containing:
|
Usage Examples
In the following examples, 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");
- $addr[1] contains the host address 10.1.2.10.
- $addr[2] contains the protocol family inet.
- $addr[3] contains the prefix length 24.
- $addr[4] contains the network address 10.1.2.0.
- $addr[5] contains the netmask for IPv4 255.255.255.0.
var $addr = jcs:parse-ip("2001:DB8::c50:8a:800:200C:417A/32");
- $addr[1] contains the host address 2001:db8:0:c50:8a:800:200c:417a.
- $addr[2] contains the protocol family inet6.
- $addr[3] contains the prefix length 32.
- $addr[4] contains the network address 2001:db8::.
- $addr[5] is blank for IPv6 ("").
printf() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Generate formatted output text. Most standard printf formats are supported, in addition to some Junos OS–specific formats. The function returns a formatted string but does not print it on call. To use the following Junos OS modifiers, place the modifier between the percent sign (%) and the conversion specifier.
- j1—Operator that emits the field only if it changed from the last time the function was called. This assumes that the expression’s format string is unchanged.
- jc—Operator that capitalizes the first letter of the associated output string.
- jt{TAG}—Operator that emits the tag if the associated argument is not empty.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
expression | — | Format string containing an arbitrary number of format specifiers and associated arguments to output. |
Usage Examples
In the following example, the j1 operator suppresses printing the interface identifier so-0/0/0 in the second line of output, because the identifier argument has not changed from the first printing. The jc operator capitalizes the output strings up and down. The jt{--} operator does not print the {--} tag in the first line of output, because the associated output argument is an empty string. However, the tag is printed in the second line because the associated output is the non-empty string test.
<xsl:value-of select="jcs:printf('%-24j1s %-5jcs %-5jcs %s%jt{ -- }s\n',
'so-0/0/0', 'up', 'down', '10.1.2.3', '')"/>
<xsl:value-of select="jcs:printf('%-24j1s %-5jcs %-5jcs %s%jt{ -- }s\n',
'so-0/0/0', 'down', 'down', '10.1.2.3', 'test')"/>
produces the following output:
so-0/0/0 Up Down 10.1.2.3 Down Down 10.1.2.3 -- test
progress() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Issue a progress message containing the single argument immediately to the CLI session provided that the detail flag was specified when the script was invoked.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
string | — | Text output to CLI session |
Usage Examples
SLAX syntax:
expr jcs:progress('Working...');
XSLT syntax:
<xsl:value-of select="jcs:progress('Working...')"/>
The script must be invoked with the detail flag in order for the progress message to appear in the CLI session.
user@host> op script1.slax detail
2010-10-01 16:27:54 PDT: running op script 'script1.slax' 2010-10-01 16:27:54 PDT: opening op script '/var/db/scripts/op/script1.slax' 2010-10-01 16:27:54 PDT: reading op script 'script1.slax' 2010-10-01 16:27:54 PDT: Working... 2010-10-01 16:28:14 PDT: inspecting op output 'script1.slax' 2010-10-01 16:28:14 PDT: finished op script 'script1.slax'
regex() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Evaluate a regular expression against a given string argument and return any matches. This function requires two arguments: the regular expression and the string to which the regular expression is compared.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
pattern | — | Regular expression that is evaluated against the string argument. |
string | — | String within which to search for matches of the specified regular expression. |
Return Value
result | — | Array of strings that match the given regex pattern within the string argument. |
Usage Examples
In the following example, the regex pattern consists of four distinct groups. The first group consists of the entire expression. The three subsequent groups are each of the parentheses–enclosed expressions within the main expression. The results for each jcs:regex() function call contain an array of the matches of the regex pattern to each of the specified strings.
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]
sleep() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Cause the script to pause for a specified number of seconds and (optionally) milliseconds. You can use this function to help determine how a device component works over time. To do this, write a script that issues a command, calls the jcs:sleep() function, and then reissues the same command.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
milliseconds | — | (Optional) Number of milliseconds the script should sleep. |
seconds | — | Number of seconds the script should sleep. |
Usage Examples
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:
SLAX syntax:
expr jcs:sleep(1); expr jcs:sleep(0, 10);
XSLT syntax:
<xsl:value-of select="jcs:sleep(1)"/>
<xsl:value-of select="jcs:sleep(0, 10)"/>
split() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 8.4
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Split a string into an array of substrings delimited by a regular expression pattern. 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.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
expression | — | Regular expression pattern used as the delimiter. |
limit | — | (Optional) Number of substrings into which to break the original string. |
string | — | Original string. |
Return Value
$substrings | — | Array of limit number of substrings. If limit is not specified, the result array size is equal to the number of substrings extracted from the original string as determined by the specified delimiter. |
Usage Examples
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"
sysctl() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Return the given 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 prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
sysctl-value | — | sysctl value to convert to a string or integer. |
Return Value
$value | — | Returned string or integer value. |
Usage Examples
var $value = jcs:sysctl("kern.hostname", "s");
syslog() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Log messages with the specified priority to the system log file. The priority can be expressed as a facility.severity string or as a calculated integer. 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 message arguments, and the concatenated string is written to the system log file. The syslog file is specified at the [edit system syslog] hierarchy level of the configuration.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
message | — | String or variable that is output to the system log file. |
message2 | — | (Optional) Any additional number of strings or variable names passed as arguments to the function. These are concatenated with the message argument and output to the system log file. |
priority | — | Priority given to the syslog message. The priority can be specified as a facility.severity string, or it can expressed as an integer calculated from the corresponding numeric values of the facility and severity strings. Table 2 and Table 3 show the facility and severity strings available and their corresponding numeric values. The integer value of the priority parameter is calculated by multiplying the facility string numeric value by 8 and adding the severity string numeric value. For example, if the facility.severity string pair is "pfe.alert", the priority value is 161 ((20 x 8)+1). Table 2: Facility Strings
Table 3: Severity Strings
|
Usage Examples
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 similar to the previous example. In this example, however, there are additional string arguments. For this case, the concatenated string "mymessage mymessage2" is output to the system log file.
expr jcs:syslog("pfe.alert", "mymessage ", "mymessage2");
trace() Function (jcs and slax Namespaces)
Namespaces
SLAX Syntax
XSLT Syntax
Release Information
Function introduced in Junos OS Release 7.6.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Description
Issue a trace message, which is sent to the trace file. You must configure traceoptions under the respective script type in the configuration hierarchy in order to output a message to the trace file using the jcs:trace() function. The output goes to the configured trace file. If traceoptions is enabled, but no trace file is explicitly configured, the output goes to the default trace file for that script type.
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
expression | — | String that is output to the trace file. |
Usage Examples
SLAX syntax:
expr jcs:trace('test');
XSLT syntax:
<xsl:value-of select="jcs:trace('test')"/>