Example: Find LSPs to Multiple Destinations Using an Op Script
This example uses an op script to check for label-switched paths (LSPs) to multiple destinations.
Requirements
This example uses a device running Junos OS.
Overview and Op Script
The following example script, which is shown in both XSLT and SLAX, checks for LSPs to multiple destinations. The script takes one mandatory command-line argument, the address specifying the LSP endpoint. The address argument can include an optional prefix length. If no address is specified, the script generates an error message and halts execution.
The get-configuration
variable stores
the remote procedure call (RPC) to
retrieve the [edit protocols mpls]
hierarchy level of the
device’s committed configuration. This configuration is stored
in the config
variable. The get-route-information
variable stores the RPC equivalent
of the show route address terse
operational
mode command, where the value of the destination
tag specifies address. The script sets this
value to the address specified by the user on the command line. The
script invokes the get-route-information
RPC and stores the output in the rpc-out
variable. If rpc-out
does not contain
any errors, the script examines all host route entries present at
the route-table/rt/rt-destination
node.
For each host route entry, if an LSP to the destination is configured in the active configuration, the script generates a “Found” message with the destination address and corresponding LSP name in the output. If an LSP to the destination is not configured, the output generates a “Missing” message containing the destination address and hostname.
XSLT Syntax
<?xml version="1.0" standalone="yes"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:junos="http://xml.juniper.net/junos/*/junos" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0" version="1.0"> <xsl:import href="../import/junos.xsl"/> <xsl:variable name="arguments"> <argument> <name>address</name> <description>LSP endpoint</description> </argument> </xsl:variable> <xsl:param name="address"/> <xsl:template match="/"> <op-script-output> <xsl:choose> <xsl:when test="$address = ''"> <xnm:error> <message>missing mandatory argument 'address'</message> </xnm:error> </xsl:when> <xsl:otherwise> <xsl:variable name="get-configuration"> <get-configuration database="committed"> <configuration> <protocols> <mpls/> </protocols> </configuration> </get-configuration> </xsl:variable> <xsl:variable name="config" select="jcs:invoke($get-configuration)"/> <xsl:variable name="mpls" select="$config/protocols/mpls"/> <xsl:variable name="get-route-information"> <get-route-information> <terse/> <destination> <xsl:value-of select="$address"/> </destination> </get-route-information> </xsl:variable> <xsl:variable name="rpc-out" select="jcs:invoke($get-route-information)"/> <xsl:choose> <xsl:when test="$rpc-out//xnm:error"> <xsl:copy-of select="$rpc-out//xnm:error"/> </xsl:when> <xsl:otherwise> <xsl:for-each select="$rpc-out/route-table/rt/rt-destination"> <xsl:choose> <xsl:when test="contains(.,'/32')"> <xsl:variable name="dest" select="substring-before(.,'/')"/> <xsl:variable name="lsp" select="$mpls/label-switched-path[to = $dest]"/> <xsl:choose> <xsl:when test="$lsp"> <output> <xsl:value-of select="concat('Found: ', $dest, ' (',$lsp/to, ') --> ', $lsp/name)"/> </output> </xsl:when> <xsl:otherwise> <xsl:variable name="name" select="jcs:hostname($dest)"/> <output> <xsl:value-of select="concat('Name: ', $name)"/> </output> <output> <xsl:value-of select="concat('Missing: ', $dest)"/> </output> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <output> <xsl:value-of select="concat('Not a host route: ', .)"/> </output> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:otherwise> </xsl:choose> </xsl:otherwise> </xsl:choose> </op-script-output> </xsl:template> </xsl:stylesheet>
SLAX Syntax
version 1.0; ns junos = "http://xml.juniper.net/junos/*/junos"; ns xnm = "http://xml.juniper.net/xnm/1.1/xnm"; ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; import "../import/junos.xsl"; var $arguments = { <argument> { <name> "address"; <description> "LSP endpoint"; } } param $address; match / { <op-script-output> { if ($address = '') { <xnm:error> { <message> "missing mandatory argument 'address'"; } } else { var $get-configuration = { <get-configuration database="committed"> { <configuration> { <protocols> { <mpls>; } } } } var $config = jcs:invoke($get-configuration); var $mpls = $config/protocols/mpls; var $get-route-information = { <get-route-information> { <terse>; <destination> $address; } } var $rpc-out = jcs:invoke($get-route-information); if ($rpc-out//xnm:error) { copy-of $rpc-out//xnm:error; } else { for-each ($rpc-out/route-table/rt/rt-destination) { if (contains(.,'/32')) { var $dest = substring-before(.,'/'); var $lsp = $mpls/label-switched-path[to = $dest]; if ($lsp) { <output> 'Found: ' _ $dest _ ' (' _ $lsp/to _ ') - -> ' _ $lsp/name; } else { var $name = jcs:hostname($dest); <output> 'Name: ' _ $name; <output> 'Missing: ' _ $dest; } } else { <output> 'Not a host route: ' _ .; } } } } } }
Configuration
Procedure
Step-by-Step Procedure
To download, enable, and test the script:
Copy the XSLT or SLAX script into a text file, name the file lsp.xsl or lsp.slax as appropriate, and copy it to the /var/db/scripts/op/ directory on the device.
In configuration mode, include the
file
statement at the[edit system scripts op]
hierarchy level and lsp.xsl or lsp.slax as appropriate.[edit system scripts op] user@host# set file lsp.(slax | xsl)
Issue the
commit and-quit
command to commit the configuration and to return to operational mode.[edit] user@host# commit and-quit
Execute the op script by issuing the
op lsp address address
operational mode command.
Verification
Verifying Script Execution
Purpose
Verify that the script behaves as expected.
Action
Issue the op lsp address address
operational mode command to execute the script. The output
varies depending on the configuration.
user@R4> op lsp address 10.168.215.0/24 Found: 192.168.215.1 (192.168.215.1) --> R4>R1 Found: 192.168.215.2 (192.168.215.2) --> R4>R2 Name: R3 Missing: 10.168.215.3 Name: R5 Missing: 10.168.215.4 Name: R6 Missing: 10.168.215.5