示例:使用 Op 脚本查找到多个目标的 LSP
此示例使用 操作脚本 检查到多个目标的标签交换路径 (LSP)。
要求
此示例使用运行 Junos OS 的设备。
概述和操作脚本
以下示例脚本(在 XSLT 和 SLAX 中均显示)检查到多个目标的 LSP。脚本需要一个强制命令行论点,即指定 LSP 端点的地址。地址参数可以包括可选的前缀长度。如果未指定地址,则脚本将生成错误消息并停止执行。
该get-configuration变量存储远程过程调用 (RPC),以检索[edit protocols mpls]设备提交的配置的层次结构级别。此配置存储在变量中config。变get-route-information量存储相当于操作模式命令的 show route address terse RPC,其中指定address了destination标记的值。脚本将此值设置为命令行上的用户指定的地址。脚本调用 get-route-information RPC 并将输出存储在变量中rpc-out。如果不rpc-out包含任何错误,脚本将检查节点上route-table/rt/rt-destination存在的所有主机路由条目。
对于每个主机路由条目,如果在活动配置中配置了目标的 LSP,则脚本将生成一条“发现”消息,并在输出中使用目标地址和相应的 LSP 名称。如果未配置到目标的 LSP,则输出将生成包含目标地址和主机名的“缺失”消息。
XSLT 语法
<?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 语法
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: ' _ .;
}
}
}
}
}
}
配置
程序
逐步过程
要下载、启用和测试脚本:
将 XSLT 或 SLAX 脚本复制到文本文件中,根据需要命名文件 lsp.xsl 或 lsp.slax ,然后将其复制到设备上的 /var/db/scripts/op/ 目录。
在配置模式下,在
[edit system scripts op]层次结构级别中包括file语句,并相应地包含 lsp.xsl 或 lsp.slax。[edit system scripts op] user@host# set file lsp.(slax | xsl)
commit and-quit发出 命令提交配置并返回到操作模式。[edit] user@host# commit and-quit
执行操作脚本,方法是
op lsp address address发出操作模式命令。
验证
验证脚本执行
目的
验证脚本是否按预期运行。
行动
op lsp address address发出操作模式命令执行脚本。输出因配置而异。
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