このページで
例:シンプルなインターフェイス設定に基づいた複雑な設定の作成
このコミット スクリプトの例では、マクロを使用して、シンプルなインターフェイス設定を自動的に拡張します。
要件
この例では、Junos OSを実行しているデバイスを使用しています。
概要とコミット スクリプト
この例では、コミットスクリプトとマクロを使用して、デフォルトのカプセル化タイプを割り当て、インターフェイスに複数のルーティングプロトコルを設定し、複数の設定グループを適用する一時的な変更を生成することで、シンプルなインターフェイス設定を自動的に拡張します。Junos OS管理(mgd)プロセスは設定を検査し、 階層レベルに含まれるステートメントを[edit interfaces interface-name]探apply-macro paramsします。
スクリプトがステートメントを apply-macro params 見つけると、次のアクションが実行されます。
設定グループを
interface-detailsインターフェイスに適用します。階層レベルの パラメーターの
description値が[edit interfaces interface-name description]含まれます。階層レベルの パラメーターの
encapsulation値が[edit interfaces interface-name encapsulation]含まれます。このパラメータがencapsulationステートメントにapply-macro params含まれていない場合、スクリプトはカプセル化をcisco-hdlcデフォルトに設定します。論理ユニット番号を に
0設定し、パラメーターが ステートメントにinet-addressapply-macro params含まれているかどうかをテストします。その場合、スクリプトには 階層レベルの パラメーターのinet-address値が[edit interfaces interface-name unit 0 family inet address]含まれます。階層レベルのインターフェイス名を
[edit protocols rsvp interface]含みます。階層レベルに
level 1 enableおよびmetricステートメントを[edit protocols isis interface interface-name]含みます。階層レベルに
level 2 enableおよびmetricステートメントを[edit protocols isis interface interface-name]含みます。または
isis-level-1-metricパラメーターが ステートメントにisis-level-1apply-macro params含まれているかどうかをテストします。これらのパラメーターの 1 つまたは両方が含まれている場合、スクリプトには 階層レベルのlevel 1ステートメントが[edit protocols isis interface interface-name]含まれます。パラメーターがisis-level-1含まれている場合、スクリプトには、 階層レベルでの パラメーター (enableまたはdisable)[edit protocols isis interface interface-name level 1]のisis-level-1値も含まれます。パラメーターがisis-level-1-metric含まれている場合、スクリプトには、 階層レベルの[edit protocols isis interface interface-name level 1 metric]パラメーターのisis-level-1-metric値も含まれます。または
isis-level-2-metricパラメーターが ステートメントにisis-level-2apply-macro params含まれているかどうかをテストします。これらのパラメーターの 1 つまたは両方が含まれている場合、スクリプトには 階層レベルのlevel 2ステートメントが[edit protocols isis interface interface-name]含まれます。パラメーターがisis-level-2含まれている場合、スクリプトには、 階層レベルでの パラメーター (enableまたはdisable)[edit protocols isis interface interface-name level 2]のisis-level-2値も含まれます。パラメーターがisis-level-2-metric含まれている場合、スクリプトには、 階層レベルの[edit protocols isis interface interface-name level 2 metric]パラメーターのisis-level-2-metric値も含まれます。階層レベルのインターフェイス名を
[edit protocols ldp interface]含みます。
このスクリプトの例は、XSLT と SLAX の両方の構文で示されています。
XSLT 構文
<?xml version="1.0" standalone="yes"?>
<xsl:stylesheet version="1.0"
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">
<xsl:import href="../import/junos.xsl"/>
<xsl:template match="configuration">
<xsl:variable name="top" select="."/>
<xsl:for-each select="interfaces/interface/apply-macro[name = 'params']">
<xsl:variable name="description"
select="data[name = 'description']/value"/>
<xsl:variable name="inet-address"
select="data[name = 'inet-address']/value"/>
<xsl:variable name="encapsulation"
select="data[name = 'encapsulation']/value"/>
<xsl:variable name="isis-level-1"
select="data[name = 'isis-level-1']/value"/>
<xsl:variable name="isis-level-1-metric"
select="data[name = 'isis-level-1-metric']/value"/>
<xsl:variable name="isis-level-2"
select="data[name = 'isis-level-2']/value"/>
<xsl:variable name="isis-level-2-metric"
select="data[name = 'isis-level-2-metric']/value"/>
<xsl:variable name="ifname" select="concat(../name, '.0')"/>
<transient-change>
<interfaces>
<interface>
<name><xsl:value-of select="../name"/></name>
<apply-groups>
<name>interface-details</name>
</apply-groups>
<xsl:if test="$description">
<description>
<xsl:value-of select="$description"/>
</description>
</xsl:if>
<encapsulation>
<xsl:choose>
<xsl:when test="string-length($encapsulation) > 0">
<xsl:value-of select="$encapsulation"/>
</xsl:when>
<xsl:otherwise>cisco-hdlc</xsl:otherwise>
</xsl:choose>
</encapsulation>
<unit>
<name>0</name>
<xsl:if test="string-length($inet-address) > 0">
<family>
<inet>
<address>
<xsl:value-of select="$inet-address"/>
</address>
</inet>
</family>
</xsl:if>
</unit>
</interface>
</interfaces>
<protocols>
<rsvp>
<interface>
<name><xsl:value-of select="$ifname"/></name>
</interface>
</rsvp>
<isis>
<interface>
<name><xsl:value-of select="$ifname"/></name>
<xsl:if test="$isis-level-1 or $isis-level-1-metric">
<level>
<name>1</name>
<xsl:if test="$isis-level-1">
<xsl:element name="{$isis-level-1}"/>
</xsl:if>
<xsl:if test="$isis-level-1-metric">
<metric>
<xsl:value-of select="$isis-level-1-metric"/>
</metric>
</xsl:if>
</level>
</xsl:if>
<xsl:if test="$isis-level-2 or $isis-level-2-metric">
<level>
<name>2</name>
<xsl:if test="$isis-level-2">
<xsl:element name="{$isis-level-2}"/>
</xsl:if>
<xsl:if test="$isis-level-2-metric">
<metric>
<xsl:value-of select="$isis-level-2-metric"/>
</metric>
</xsl:if>
</level>
</xsl:if>
</interface>
</isis>
<ldp>
<interface>
<name><xsl:value-of select="$ifname"/></name>
</interface>
</ldp>
</protocols>
</transient-change>
</xsl:for-each>
</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";
match configuration {
var $top = .;
for-each (interfaces/interface/apply-macro[name = 'params']) {
var $description = data[name = 'description']/value;
var $inet-address = data[name = 'inet-address']/value;
var $encapsulation = data[name = 'encapsulation']/value;
var $isis-level-1 = data[name = 'isis-level-1']/value;
var $isis-level-1-metric = data[name = 'isis-level-1-metric']/value;
var $isis-level-2 = data[name = 'isis-level-2']/value;
var $isis-level-2-metric = data[name = 'isis-level-2-metric']/value;
var $ifname = ../name _ '.0';
<transient-change> {
<interfaces> {
<interface> {
<name> ../name;
<apply-groups> {
<name> "interface-details";
}
if ($description) {
<description> $description;
}
<encapsulation> {
if (string-length($encapsulation) > 0) {
expr $encapsulation;
} else {
expr "cisco-hdlc";
}
}
<unit> {
<name> "0";
if (string-length($inet-address) > 0) {
<family> {
<inet> {
<address> $inet-address;
}
}
}
}
}
}
<protocols> {
<rsvp> {
<interface> {
<name> $ifname;
}
}
<isis> {
<interface> {
<name> $ifname;
if ($isis-level-1 or $isis-level-1-metric) {
<level> {
<name> "1";
if ($isis-level-1) {
<xsl:element name="{$isis-level-1}">;
}
if ($isis-level-1-metric) {
<metric> $isis-level-1-metric;
}
}
}
if ($isis-level-2 or $isis-level-2-metric) {
<level> {
<name> "2";
if ($isis-level-2) {
<xsl:element name="{$isis-level-2}">;
}
if ($isis-level-2-metric) {
<metric> $isis-level-2-metric;
}
}
}
}
}
<ldp> {
<interface> {
<name> $ifname;
}
}
}
}
}
}
トポロジ
構成
手順
手順
スクリプトをダウンロード、有効化、テストするには、以下の手順にしたがっています。
スクリプトをテキスト ファイルにコピーし、 if-params.xsl または if-params.slax に名前を付け、デバイス上の /var/db/scripts/commit/ ディレクトリにコピーします。
以下のテスト構成スタンザを選択し、Ctrl+cを押してクリップボードにコピーします。
スクリプトの SLAX バージョンを使用している場合は、 階層レベルのファイル名を
[edit system scripts commit file]if-params.slax に変更します。system { scripts { commit { allow-transients; file if-params.xsl; } } } groups { interface-details { interfaces { <so-*/*/*> { clocking internal; } } } } interfaces { so-1/2/3 { apply-macro params { description "Link to Hoverville"; encapsulation ppp; inet-address 10.1.2.3/28; isis-level-1 enable; isis-level-1-metric 50; isis-level-2-metric 85; } } }設定モードで、 コマンドを
load merge terminal発行して、スタンザをデバイス設定にマージします。[edit] user@host# load merge terminal [Type ^D at a new line to end input] ... Paste the contents of the clipboard here ...
プロンプトで、マウスと貼り付けアイコンを使用して、クリップボードの内容を貼り付けます。
Enter キーを押します。
Ctrl+dを押します。
設定をコミットします。
user@host# commit
検証
設定の検証
目的
スクリプトが想定どおりに動作していることを確認します。
アクション
設定モード コマンドを show interfaces | display commit-scripts | display inheritance 発行します。オプションは | display commit-scripts 、一時変更によって生成されるステートメントを含め、コンフィギュレーション内のすべてのステートメントを表示します。オプションは | display inheritance 、継承された設定データと、その設定が継承された送信元グループに関する情報を表示します。また、このオプションは、インターフェイス範囲設定データを拡張形式で示し、設定が拡張された送信元インターフェイス範囲に関する情報も表示します。次の出力が表示されます。
[edit]
user@host# show interfaces | display commit-scripts | display inheritance
so-1/2/3 {
apply-macro params {
clocking internal;
description "Link to Hoverville";
encapsulation ppp;
inet-address 10.1.2.3/28;
isis-level-1 enable;
isis-level-1-metric 50;
isis-level-2-metric 85;
}
description "Link to Hoverville";
##
## 'internal' was inherited from group 'interface-details'
##
clocking internal;
encapsulation ppp;
unit 0 {
family inet {
address 10.1.2.3/28;
}
}
}
設定モード コマンドを show protocols | display commit-scripts 発行します。次の出力が表示されます。
[edit]
user@host# show protocols | display commit-scripts
rsvp {
interface so-1/2/3.0;
}
isis {
interface so-1/2/3.0 {
level 1 {
enable;
metric 50;
}
level 2 metric 85;
}
}
ldp {
interface so-1/2/3.0;
}