このページで
例:論理インターフェイスと IP アドレスの自動設定
設定するすべてのインターフェイスには、少なくとも1つの論理ユニットと1つのIPアドレスが必要です。 非同期転送モード (ATM)インターフェイスでは、各論理インターフェイスに対してVCI(仮想回線識別子)も必要です。インターフェイス上に複数の論理ユニットを設定する必要がある場合は、コミットスクリプトとマクロを使用して、エラーなしでタスクを迅速に完了できます。
要件
この例では、物理ATMインターフェイスを備えたJunos OSを実行しているデバイスを使用しています。
概要とコミット スクリプト
以下のコミットスクリプトは、 apply-macro 物理ATMインターフェイスの名前と、インターフェイス上に多数の論理ユニットを設定する方法を指定するパラメーターのセットを提供するステートメントを展開します。ユニットとVCI番号は、変数から unit 変数に max 順番に番号が付けされ、 変数から始まるIPアドレスが address 与られます。論理ユニットをループするために、Extensible Stylesheet Language Transformations(XSLT)はテンプレートに実装されている再帰を <emit-interface> 使用します。次のアドレスの計算は、テンプレートで実行されます <next-address> 。
このスクリプトの例は、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:for-each select="interfaces/apply-macro"> <xsl:variable name="device" select="name"/> <xsl:variable name="address" select="data[name='address']/value"/> <xsl:variable name="max" select="data[name='max']/value"/> <xsl:variable name="unit" select="data[name='unit']/value"/> <xsl:variable name="real-max"> <xsl:choose> <xsl:when test="string-length($max) > 0"> <xsl:value-of select="$max"/> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="real-unit"> <xsl:choose> <xsl:when test="string-length($unit) > 0"> <xsl:value-of select="$unit"/> </xsl:when> <xsl:when test="contains($device, '.')"> <xsl:value-of select="substring-after($device, '.')"/> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="real-device"> <xsl:choose> <xsl:when test="contains($device, '.')"> <xsl:value-of select="substring-before($device, '.')"/> </xsl:when> <xsl:otherwise><xsl:value-of select="$device"/></xsl:otherwise> </xsl:choose> </xsl:variable> <transient-change> <interfaces> <interface> <name><xsl:value-of select="$real-device"/></name> <xsl:call-template name="emit-interface"> <xsl:with-param name="address" select="$address"/> <xsl:with-param name="unit" select="$real-unit"/> <xsl:with-param name="max" select="$real-max"/> </xsl:call-template> </interface> </interfaces> </transient-change> </xsl:for-each> </xsl:template> <xsl:template name="emit-interface"> <xsl:param name="$max"/> <xsl:param name="$unit"/> <xsl:param name="$address"/> <unit> <name><xsl:value-of select="$unit"/></name> <vci><xsl:value-of select="$unit"/></vci> <family> <inet> <address><xsl:value-of select="$address"/></address> </inet> </family> </unit> <xsl:if test="$max > $unit"> <xsl:call-template name="emit-interface"> <xsl:with-param name="address"> <xsl:call-template name="next-address"> <xsl:with-param name="address" select="$address"/> </xsl:call-template> </xsl:with-param> <xsl:with-param name="unit" select="$unit + 1"/> <xsl:with-param name="max" select="$max"/> </xsl:call-template> </xsl:if> </xsl:template> <xsl:template name="next-address"> <xsl:param name="address"/> <xsl:variable name="arg-prefix" select="substring-after($address, '/')"/> <xsl:variable name="arg-addr" select="substring-before($address, '/')"/> <xsl:variable name="addr"> <xsl:choose> <xsl:when test="string-length($arg-addr) > 0"> <xsl:value-of select="$arg-addr"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$address"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="prefix"> <xsl:choose> <xsl:when test="string-length($arg-prefix) > 0"> <xsl:value-of select="$arg-prefix"/> </xsl:when> <xsl:otherwise>32</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="a1" select="substring-before($addr, '.')"/> <xsl:variable name="a234" select="substring-after($addr, '.')"/> <xsl:variable name="a2" select="substring-before($a234, '.')"/> <xsl:variable name="a34" select="substring-after($a234, '.')"/> <xsl:variable name="a3" select="substring-before($a34, '.')"/> <xsl:variable name="a4" select="substring-after($a34, '.')"/> <xsl:variable name="r3"> <xsl:choose> <xsl:when test="$a4 < 255"> <xsl:value-of select="$a3"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$a3 + 1"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="r4"> <xsl:choose> <xsl:when test="$a4 < 255"> <xsl:value-of select="$a4 + 1"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="0"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:value-of select="$a1"/> <xsl:text>.</xsl:text> <xsl:value-of select="$a2"/> <xsl:text>.</xsl:text> <xsl:value-of select="$r3"/> <xsl:text>.</xsl:text> <xsl:value-of select="$r4"/> <xsl:text>/</xsl:text> <xsl:value-of select="$prefix"/> </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 {
for-each (interfaces/apply-macro) {
var $device = name;
var $address = data[name='address']/value;
var $max = data[name='max']/value;
var $unit = data[name='unit']/value;
var $real-max = {
if (string-length($max) > 0) {
expr $max;
} else {
expr "0";
}
}
var $real-unit = {
if (string-length($unit) > 0) {
expr $unit;
} else if (contains($device, '.')) {
expr substring-after($device, '.');
} else {
expr "0";
}
}
var $real-device = {
if (contains($device, '.')) {
expr substring-before($device, '.');
} else {
expr $device;
}
}
<transient-change> {
<interfaces> {
<interface> {
<name> $real-device;
call emit-interface($address, $unit = $real-unit, $max = $real-max);
}
}
}
}
}
emit-interface ($max, $unit, $address) {
<unit> {
<name> $unit;
<vci> $unit;
<family> {
<inet> {
<address> $address;
}
}
}
if ($max > $unit) {
call emit-interface($unit = $unit + 1, $max) {
with $address = {
call next-address($address);
}
}
}
}
next-address ($address) {
var $arg-prefix = substring-after($address, '/');
var $arg-addr = substring-before($address, '/');
var $addr = {
if (string-length($arg-addr) > 0) {
expr $arg-addr;
} else {
expr $address;
}
}
var $prefix = {
if (string-length($arg-prefix) > 0) {
expr $arg-prefix;
} else {
expr "32";
}
}
var $a1 = substring-before($addr, '.');
var $a234 = substring-after($addr, '.');
var $a2 = substring-before($a234, '.');
var $a34 = substring-after($a234, '.');
var $a3 = substring-before($a34, '.');
var $a4 = substring-after($a34, '.');
var $r3 = {
if ($a4 < 255) {
expr $a3;
} else {
expr $a3 + 1;
}
}
var $r4 = {
if ($a4 < 255) {
expr $a4 + 1;
} else {
expr 0;
}
}
expr $a1;
expr ".";
expr $a2;
expr ".";
expr $r3;
expr ".";
expr $r4;
expr "/";
expr $prefix;
}
構成
手順
手順
スクリプトをダウンロード、有効化、実行するには、以下の手順にしたがっています。
スクリプトをテキスト ファイルにコピーし、必要に応じて atm-logical.xsl または atm-logical.slax というファイルに名前を付け、デバイス上の /var/db/scripts/commit/ ディレクトリにダウンロードします。
以下のテスト構成スタンザを選択し、Ctrl+cを押してクリップボードにコピーします。
スクリプトの SLAX バージョンを使用している場合は、 階層レベルのファイル名を
[edit system scripts commit file]atm-logical.slax に変更します。system { scripts { commit { allow-transients; file atm-logical.xsl; } } } interfaces { apply-macro at-1/2/3 { address 10.12.13.14/20; max 200; unit 32; } at-1/2/3 { atm-options { pic-type atm2; vpi 0; } } }設定モードで、 コマンドを
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を押します。
コマンドを
commit発行して、設定をコミットします。user@host# commit
検証
設定の検証
目的
正しい変更が設定に統合されていることを確認します。
アクション
設定をコミットする前に、設定モード コマンドを発行することで、コミット スクリプトが正しい結果を show interfaces at-1/2/3 | display commit-scripts 生成することを確認できます。設定をコミットした後、 運用モードコマンドを発行してアクティブな設定を show configuration interfaces at-1/2/3 確認できます。次の出力が表示されます。
atm-options {
pic-type atm2;
vpi 0;
}
unit 32 {
vci 32;
family inet {
address 10.12.13.14/20;
}
}
unit 33 {
vci 33;
family inet {
address 10.12.13.15/20;
}
}
unit 34 {
vci 34;
family inet {
address 10.12.13.16/20;
}
}
unit 35 {
vci 35;
family inet {
address 10.12.13.17/20;
}
}
... Logical units 36 through 199 are omitted for brevity ...
unit 200 {
vci 200 ;
family inet {
address 10.12.13.182/20;
}
}
意味
オプションは | display commit-scripts 、すべてのコミットスクリプトが適用された後の設定データを表示します。出力には、永続的な変更と一時的な変更の両方が含まれます。適切 unit で、 vci 各 ATM インターフェイスで設定されている場合、コミット スクリプトはコミット操作中に正常に実行されます。設定をコミットした後、 運用モードコマンドを発行してアクティブな設定を show configuration interfaces at-1/2/3 確認できます。