示例:配置双路由引擎
如果设备具有冗余(也称为 双)路由引擎,则 Junos OS 配置可能很复杂。此示例说明如何使用提交脚本来简化和控制双路由引擎平台的配置。
要求
此示例使用通过双路由引擎运行 Junos OS 的设备。
概述和提交脚本
Junos OS 支持两个特殊配置组: re0
和 re1
。使用语句应用 apply-groups [ re0 re1 ]
这些组时,如果路由引擎名称与组名称匹配,它们将生效。在层次结构级别包含 [edit groups re0]
的语句仅在名为 RE0 的路由引擎上继承,在层次结构级别包含 [edit groups re1]
的语句仅在名为 RE1 的路由引擎上继承。
此示例包括两个提交脚本。如果在目标配置而不是配置组中配置了system host-name
语句、任何 IP 版本 4 (IPv4) 接口地址或 fxp0 接口配置,则第一个脚本 dual-re.xsl 将生成警告。
第二个脚本 dual-re2.xsl,首先检查主机名配置是否已配置,然后检查是否在配置组中配置了主机名配置。 otherwise
如果根本没有配置主机名,则构造将生成错误消息。如果已在配置组中配置主机名,则第一个 when
构造允许脚本不执行任何操作。当在目标配置中配置主机名时,第二个 when
构造将生效。在这种情况下,脚本会生成 瞬时更改 ,将主机名配置放入 re0
和 re1
配置组中,将配置的主机名复制到这些组中,将每个组主机名与 和 -RE0
-RE1
连接起来,并停用目标配置中的主机名,以便可以 继承配置组主机名。
示例脚本以 XSLT 和 SLAX 语法显示:
XSLT 语法:双 re.xsl 脚本
<?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="system/host-name | interfaces/interface/unit/family/inet/address | interfaces/interface[name = 'fxp0']"> <xsl:if test="not(@junos:group) or not(starts-with(@junos:group, 're'))"> <xnm:warning> <xsl:call-template name="jcs:edit-path"> <xsl:with-param name="dot" select=".."/> </xsl:call-template> <xsl:call-template name="jcs:statement"/> <message> <xsl:text>statement should not be in target</xsl:text> <xsl:text> configuration on dual RE system</xsl:text> </message> </xnm:warning> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>
XSLT 语法:双 re2.xsl 脚本
<?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="hn" select="system/host-name"/> <xsl:choose> <xsl:when test="$hn/@junos:group"/> <xsl:when test="$hn"> <transient-change> <groups> <name>re0</name> <system> <host-name> <xsl:value-of select="concat($hn, '-RE0')"/> </host-name> </system> </groups> <groups> <name>re1</name> <system> <host-name> <xsl:value-of select="concat($hn, '-RE1')"/> </host-name> </system> </groups> <system> <host-name inactive="inactive"/> </system> </transient-change> </xsl:when> <xsl:otherwise> <xnm:error> <message>Missing [system host-name]</message> </xnm:error> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
SLAX 语法:dual-re.xsl 脚本
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 (system/host-name | interfaces/interface/unit/family/inet/address | interfaces/interface[name = 'fxp0']) { if (not(@junos:group) or not(starts-with(@junos:group, 're'))) { <xnm:warning> { call jcs:edit-path($dot = ..); call jcs:statement(); <message> { expr "statement should not be in target"; expr " configuration on dual RE system"; } } } } }
SLAX 语法:双 re2.xsl 脚本
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 $hn = system/host-name; if ($hn/@junos:group) { } else if ($hn) { <transient-change> { <groups> { <name> "re0"; <system> { <host-name> $hn _ '-RE0'; } } <groups> { <name> "re1"; <system> { <host-name> $hn _ '-RE1'; } } <system> { <host-name inactive="inactive">; } } else { <xnm:error> { <message> "Missing [system host-name]"; } } } }
配置
程序
分步过程
要下载、启用和运行脚本,请执行以下操作:
将脚本复制到两个文本文件中,根据需要将文件命名为 dual-re.xsl 和 dual-re2.xsl 或 dual-re.slax 和 dual-re2.slax ,并将它们复制到设备上的 /var/db/scripts/commit/ 目录。
选择以下测试配置节,然后按 Ctrl+c 将其复制到剪贴板。
如果您使用的是脚本的 SLAX 版本,请将层次结构级别的文件名
[edit system scripts commit file]
更改为 dual-re.slax 和 dual-re2.slax。groups { re0 { interfaces { fxp0 { unit 0 { family inet { address 10.0.0.1/24; } } } } } } apply-groups re0; system { host-name router1; scripts { commit { file dual-re.xsl; file dual-re2.xsl; } } } interfaces { fe-0/0/0 { unit 0 { family inet { address 192.168.220.1/30; } } } }
在配置模式下,发出
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 ...
出现提示时,使用鼠标和粘贴图标粘贴剪贴板的内容。
按回车键。
按 Ctrl+d。
提交配置。
user@host# commit
验证
验证提交脚本更改
目的
验证脚本的行为是否符合预期。
行动
查看命令的 commit
输出。提交操作完成后,设备主机名将更改为路由器 1-RE0。
[edit] user@host# commit [edit system] 'host-name router1;' warning: statement should not be in target configuration on dual RE system [edit interfaces interface fe-0/0/0 unit 0 family inet] 'address 192.168.220.1/30;' warning: statement should not be in target configuration on dual RE system commit complete