Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

How to Use the SLAX Processor (slaxproc)

The SLAX processor (slaxproc) is a command-line tool that can format SLAX scripts, validate SLAX script syntax, run and debug SLAX scripts, and convert between SLAX and XSLT. The slaxproc mode defines the function that the processor performs. The following sections outline commonly used modes. For a complete list of slaxproc modes, see Understanding the SLAX Processor (slaxproc).

You can use the libslax library to develop SLAX scripts on a network management system. The Junos OS and Junos OS Evolved software also include the libslax tools. To verify the installed SLAX and libslax versions, issue the slaxproc --version command. For example, to print the SLAX and libslax versions on a device running Junos OS, you can issue the following shell command:

Similarly on a device running Junos OS Evolved:

Validate SLAX Script Syntax

The SLAX processor provides an option to check the syntax of a SLAX script.

To check the syntax of a SLAX script:

  • Issue the slaxproc command with the --check or -c mode option and the script filename.

If the script syntax is correct, the SLAX processor issues a "script check succeeds" message. Otherwise, the processor issues a list of error messages detected during script parsing. Fix any indicated errors, and repeat the check.

Convert Scripts Between XSLT and SLAX Formats

The SLAX processor supports converting scripts between SLAX and XSLT. When you convert a script, you can reference the file arguments positionally or use the command-line file options. The options use --input or -i and --output or -o to specify the original input script and the converted output script, respectively. If you use the file options, you can reference the files in any order, and you can intersperse the file options among other command-line options.

If you do not provide an argument specifying an input file or an output file, the processor uses standard input (stdin) or standard output (stdout). When using stdin, press Ctrl+d to signal the end-of-file.

To convert a SLAX script to XSLT, issue the slaxproc command with the --slax-to-xslt or -x mode option. To reference the files positionally, specify the input SLAX file as the first argument. The second argument is the output path and filename of the converted XSLT script. To reference the files using the file options, include the options in any order. For example:

To convert an XSLT script to SLAX, issue the slaxproc command with the --xslt-to-slax or -s mode option. To reference the files positionally, specify the input XSLT file as the first argument. The second argument is the output path and filename of the converted SLAX script. To reference the files using file options, include the options in any order.

Optionally, when converting a script from XSLT to SLAX, include the --write-version or -w option to specify the SLAX version of the converted script. The default version is the latest SLAX version on the device. Use the -p option for partial input when you do not require the SLAX script boilerplate in the output.

The following example converts the XSLT script script1.xsl to the SLAX script script1.slax. The SLAX script includes the statement "version 1.3;" as the first line of the script.

The slaxproc --xslt-to-slax mode with the -p option is useful for quickly converting Junos OS hierarchies from XML format into SLAX. The following example provides the Junos OS [edit policy-options] hierarchy in XML format as input to the SLAX processor. The -p option indicates partial script input as opposed to a full script.

The SLAX processor returns the SLAX formatting for the hierarchy.

Run SLAX Scripts

The SLAX processor supports executing SLAX scripts from the command line. This mode is the default. To explicitly use this mode, issue the slaxproc command with the --run or -r mode.

When you execute a script, you can reference the file arguments positionally or use the command-line file options. The file options use --name or -n, --input or -i, and --output or -o to specify the SLAX script file, and the input and output files, respectively. If you use the file options, you can reference the files in any order, and you can intersperse the file options among other command-line options.

If the script does not require an input file, use the -E option to indicate an empty input document. Additionally, if the input or output argument has the value "‑", the processor uses stdin or stdout. When using stdin, press Ctrl+d to signal the end-of-file.

The syntax for executing a script is:

To execute a script using the slaxproc command-line tool:

  1. Create a script using your favorite editor.
  2. (Optional) Check the script syntax by invoking the processor with the --check or -c mode option. Fix any indicated errors.
  3. Execute the script and provide the required input and output files as well as any required slaxproc options.
    • To execute a script named script1.slax using input.xml as the input document and output.xml as the output document, issue either of the following commands. The two commands are identical in execution.

    • To execute a script that requires no input file, include the -E option to indicate an empty input document. For example:

    • To execute a script and use standard input as the input document, issue the slaxproc command with no input file argument. At the prompt, enter the input and press Ctrl+d to signal the end-of-file. For example:

Run the Profiler on a Script

The SLAX profiler can report information about the activity and performance of a script. The profiler tracks script execution until the script terminates. Starting with SLAX version 1.3, the slaxproc command includes options to run the profiler on a script and save the output to a file. In earlier SLAX versions, you must invoke the SLAX debugger to use the profiler.

The slaxproc command options include:

  • --profile <file>—Run the profiler and save the output to the specified file.

  • --profile-mode <mode>—Run the profiler using the given mode and save the output to the profile.output file.

The general syntax is:

For example, the following command runs the profiler for the script1.slax script and saves the profiler output to the script1-profile file.

The following command runs the profiler in brief mode for the script1.slax script and saves the profiler output to the default output file profile.output.

The following command runs the profiler in brief mode for the script1.slax script and saves the profiler output to the script1-profile file.

For detailed information about the profiler and its output, see SLAX Debugger, Profiler, and callflow.

Format SLAX Scripts

The SLAX processor provides the option to format a script to correct the indentation and spacing to the preferred style. When you format a script, you can reference the file arguments positionally or use the command-line file options. The file options use --input or -i and --output or -o to specify the original input file and the formatted output file, respectively. If you use the file options, you can reference the files in any order on the command line.

To format a SLAX script, issue the slaxproc command with the --format or -F mode option. To reference the files positionally, specify the original SLAX script as the first argument. The second argument is the output path and filename of the formatted SLAX script. To reference the files using the file options, include the options in any order. For example:

Given the following unformatted SLAX script as input:

The SLAX processor outputs the following formatted SLAX script:

Starting with SLAX version 1.3, when you format a SLAX script, you can specify the number of spaces to use for indentation as well as the line width. The default indentation is four spaces. To define the indentation, use the --indent-width option. To limit the line width, use the --width option. The processor attempts to limit each line to the specified width, but long tokens might exceed the limit.

For example, suppose you use the previous example's original script as input and specify an indentation of two spaces and a line width of 40 characters. The SLAX processor returns the following output: