示例:要求和限制配置语句
Junos OS 提交脚本会实施自定义配置规则。提交 候选配置 后,将由每个活动提交脚本进行检查。此示例使用提交脚本指定必需的和禁止的配置语句。
要求
此示例使用运行 Junos OS 并具有以太网管理接口 fxp0 的设备。
概述和提交脚本
此示例说明如何使用提交脚本指定必需的和禁止的配置语句。以下提交脚本可确保配置以太网管理接口 (fxp0) 并检测接口被不当禁用时。脚本还会检测 bgp
语句何时不在层次结构级别上。 [edit protocols]
在所有情况下,脚本都会生成一条错误消息,提交操作会失败。
示例脚本同时以 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:call-template name="error-if-missing"> <xsl:with-param name="must" select="interfaces/interface[name='fxp0']/ unit[name='0']/family/inet/address"/> <xsl:with-param name="statement" select="'interfaces fxp0 unit 0 family inet address'"/> </xsl:call-template> <xsl:call-template name="error-if-present"> <xsl:with-param name="must" select="interfaces/interface[name='fxp0']/disable | interfaces/interface[name='fxp0']/ unit[name='0']/disable"/> <xsl:with-param name="message"> <xsl:text>The fxp0 interface is disabled.</xsl:text> </xsl:with-param> </xsl:call-template> <xsl:call-template name="error-if-missing"> <xsl:with-param name="must" select="protocols/bgp"/> <xsl:with-param name="statement" select="'protocols bgp'"/> </xsl:call-template> </xsl:template> <xsl:template name="error-if-missing"> <xsl:param name="must"/> <xsl:param name="statement" select="'unknown'"/> <xsl:param name="message" select="'missing mandatory configuration statement'"/> <xsl:if test="not($must)"> <xnm:error> <edit-path><xsl:copy-of select="$statement"/></edit-path> <message><xsl:copy-of select="$message"/></message> </xnm:error> </xsl:if> </xsl:template> <xsl:template name="error-if-present"> <xsl:param name="must" select="1"/> <!-- error if param missing --> <xsl:param name="message" select="'invalid configuration statement'"/> <xsl:for-each select="$must"> <xnm:error> <xsl:call-template name="jcs:edit-path"/> <xsl:call-template name="jcs:statement"/> <message><xsl:copy-of select="$message"/></message> </xnm:error> </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 { call error-if-missing($must = interfaces/interface[name='fxp0']/unit[name='0']/family/inet/address, $statement = 'interfaces fxp0 unit 0 family inet address'); call error-if-present($must = interfaces/interface[name='fxp0']/disable | interfaces/interface[name='fxp0']/unit[name='0']/disable) { with $message = { expr "The fxp0 interface is disabled."; } } call error-if-missing($must = protocols/bgp, $statement = 'protocols bgp'); } error-if-missing ($must, $statement = 'unknown', $message = 'missing mandatory configuration statement') { if (not($must)) { <xnm:error> { <edit-path> { copy-of $statement; } <message> { copy-of $message; } } } } error-if-present ($must = 1, $message = 'invalid configuration statement') { for-each ($must) { <xnm:error> { call jcs:edit-path(); call jcs:statement(); <message> { copy-of $message; } } } }
配置
程序
逐步过程
要下载、启用和测试脚本:
将脚本复制到文本文件中,根据需要将文件命名 为 no-nukes.xsl 或 no-nukes.slax ,并将其复制到设备上的 /var/db/script/commit/ 目录中。
选择以下测试配置部分,然后按 Ctrl+c 将它们复制到该测试板。
如果您使用的脚本的 SLAX 版本,请从层级将文件名
[edit system scripts commit file]
更改为 no-nukes.slax。system { scripts { commit { file no-nukes.xsl; } } } interfaces { fxp0 { disable; unit 0 { family inet { address 10.0.0.1/24; } } } }
在配置模式下,发出
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
验证
验证提交脚本执行
目的
验证脚本是否按预期运行。
行动
查看命令的 commit
输出。脚本要求配置和启用以太网管理接口 (fxp0),并且语句 bgp
包含在 [edit protocols]
层次结构级别。示例配置部分包括 fxp0 接口,但将其禁用。此外, bgp
语句未在层次结构级别上 [edit protocols]
配置。运行脚本时,会生成错误,提交操作会失败。发出 commit
命令后,将显示以下输出:
[edit] user@host# commit [edit interfaces interface fxp0 disable] 'disable;' The fxp0 interface is disabled. protocols bgp missing mandatory configuration statement error: 2 errors reported by commit scripts error: commit script failure