このページの目次
例:Op スクリプトを使用して複数の宛先への LSP を検索する
この例では、 opスクリプト を使用して、複数の宛先へのラベルスイッチパス(LSP)をチェックします。
要件
この例では、Junos OS を実行しているデバイスを使用します。
概要と Op スクリプト
XSLT と SLAX の両方で示されている次のスクリプト例では、複数の宛先への LSP をチェックします。このスクリプトは、LSP エンドポイントを指定するアドレスである必須のコマンドライン引数を 1 つ取ります。address 引数には、オプションのプレフィックス長を含めることができます。アドレスを指定しない場合、スクリプトはエラー メッセージを生成し、実行を停止します。
この変数にはget-configuration、デバイスのコミットされた設定の階層レベルを取得する[edit protocols mpls]ためのリモート プロシージャ コール(RPC)が格納されます。この設定は、 変数に格納されますconfig。この変数にはget-route-information、 show route address terse タグの値がdestination指定する address操作モード コマンドに相当する RPC が格納されます。スクリプトは、この値をコマンド ラインでユーザーが指定したアドレスに設定します。スクリプトは RPC をget-route-information呼び出し、出力を変数に格納しますrpc-out。にエラーが含まれていない場合rpc-out、スクリプトはノードに存在するすべてのホスト ルート エントリーを調べますroute-table/rt/rt-destination。
各ホスト ルート エントリーに対して、宛先への LSP がアクティブな設定で設定されている場合、スクリプトは宛先アドレスと対応する LSP 名を出力した「Found」メッセージを生成します。宛先への LSP が設定されていない場合、出力は宛先アドレスとホスト名を含む「Missing」メッセージを生成します。
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スクリプトを実行します。
検証
スクリプト実行の検証
目的
スクリプトが期待どおりに動作することを確認します。
アクション
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