Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

Example: Customizing Output of the show interfaces terse Command Using an Op Script

 

This example uses an op script to customize the output of the show interfaces terse command. A line-by-line explanation of the XSLT script is provided.

Requirements

This example uses a device running Junos OS.

Overview and Op Script

By default, the layout of the show interfaces terse command looks like this:

user@host> show interfaces terse

In Junos XML, the output fields are represented as follows:

XSLT Syntax

The following script customizes the output of the show interfaces terse command. A line-by-line explanation of the script is provided.

Line-by-Line Explanation

Lines 1 through 7, Line 20, and Lines 105 and 106 are the boilerplate that you include in every op script. For more information, see Required Boilerplate for Op Scripts.

Lines 8 through 17 declare a variable called arguments, containing two arguments to the script: interface and protocol. This variable declaration causes interface and protocol to appear in the command-line interface (CLI) as available arguments to the script.

Lines 18 and 19 declare two parameters to the script, corresponding to the arguments created in Lines 8 through 17. The parameter names must exactly match the argument names.

Lines 20 through 31 declare a variable named rpc. The show interfaces terse command is assigned to the rpc variable. If you include the interface argument when you execute the script, the value of the argument (the interface name) is passed into the script.

Line 32 declares a variable named out and applies to it the execution of the rpc variable (show interfaces terse command).

Line 33 specifies that the output level of the show interfaces command being modified is terse (as opposed to extensive, detail, and so on).

Lines 34 through 39 specify that if you include the protocol argument when you execute the script and if the protocol value that you specify is inet, inet6, mpls, or tnp, the intf template is applied to each instance of that protocol type in the output.

Lines 40 through 47 specify that if you include the protocol argument when you execute the script and if the protocol value that you specify is something other than inet, inet6, mpls, or tnp, an error message is generated.

Lines 48 through 52 specify that if you do not include the protocol argument when you execute the script, the intf template is applied to each logical interface in the output.

Lines 53 through 56 are closing tags.

Line 57 opens the intf template. This template customizes the output of the show interfaces terse command.

Line 58 declares a variable called status, the purpose of which is to specify how the interface status is reported. Lines 59 through 78 contain a <xsl:choose> instruction that populates the status variable by considering all the possible states. As always in XSLT, the first <xsl:when> instruction that evaluates as TRUE is executed, and the remainder are ignored. Each <xsl:when> instruction is explained separately.

Lines 60 through 62 specify that if admin-status is 'up' and oper-status is 'up', no output is generated. In this case, the status variable remains empty.

Lines 63 through 65 specify that if admin-status is 'down', the status variable contains the text offline.

Lines 66 through 68 specify that if oper-status is 'down' and the physical interface admin-status is 'down', the status variable contains the text p-offline. (../ selects the physical interface.)

Lines 69 through 71 specify that if oper-status is 'down' and the physical interface oper-status is 'down', the status variable contains the text p-down. (../ selects the physical interface.)

Lines 72 through 74 specify that if oper-status is 'down', the status variable contains the text down.

Lines 75 through 77 specify that if none of the test cases are true, the status variable contains oper-status and admin-status concatenated with a slash as a separator.

Lines 78 and 79 are closing tags.

Lines 80 through 89 define a variable called desc. An <xsl:choose> instruction populates the variable by selecting the most specific interface description available. If a logical interface description is included in the configuration, it is used to populate the desc variable. If not, the physical interface description is used. If no physical interface description is included in the configuration, the variable remains empty. As always in XSLT, the first <xsl:when> instruction that evaluates as TRUE is executed, and the remainder are ignored.

The remainder of the script specifies how the operational mode output is displayed.

Lines 90 and 91 specify that the logical interface name is displayed first in the output.

Lines 92 through 94 test whether the desc variable has a nonzero number of characters. If the number of characters is more than zero, the interface description is displayed in the standard location of the admin-status field. (In standard output, the admin-status field is displayed on the second line.)

Line 95 specifies that the interface status as defined in the status variable is displayed next.

Lines 96 through 103 specify that if you include the protocol argument when you execute the script, only interfaces with that protocol configured are displayed. If you do not include the protocol argument, all interfaces are displayed.

Lines 104 through 106 are closing tags.

SLAX Syntax

The SLAX version of the script is as follows:

Configuration

Step-by-Step Procedure

To download, enable, and test the script:

  1. Copy the XSLT or SLAX script into a text file, name the file interface.xsl or interface.slax as appropriate, and copy it to the /var/db/scripts/op/ directory on the device.
  2. In configuration mode, include the file statement at the [edit system scripts op] hierarchy level and interface.xsl or interface.slax as appropriate.

  3. Issue the commit and-quit command to commit the configuration and to return to operational mode.

  4. Execute the op script by issuing the op interface operational mode command.

Verification

Verifying the Commit Script Output

Purpose

Verify that the script behaves as expected.

Action

Issue the show interfaces terse and op interface operational commands and compare the output. The show interfaces terse command displays the standard output. The op interface command displays the customized output.

user@host> show interfaces terse
user@host> op interface

Issue the op interface operational command for different hierarchy levels and review the output. For example:

user@host> op interface interface fxp0
user@host> op interface protocol inet