示例:生成自定义错误消息
Junos OS 提交脚本可以在提交操作期间生成自定义错误消息,以便在配置违反自定义配置规则时向您发出提醒。发出错误消息会导致提交失败。此示例创建一个提交脚本,当设备配置中不包含特定语句时,该脚本将生成自定义错误消息,从而停止提交操作。
要求
使用 Python 脚本时为 Junos OS 16.1R3 或更高版本。
概述和提交脚本
使用提交脚本,编写一条自定义错误消息,当语句未包含在[edit interfaces t1-fpc/pic/port]层次结构级别时description显示:
该脚本以 XSLT、SLAX 和 Python 格式显示。
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="interface" select="interfaces/interface"/>
<xsl:for-each select="$interface[starts-with(name, 't1-')]">
<xsl:variable name="ifname" select="."/>
<xsl:if test="not(description)">
<xnm:error>
<xsl:call-template name="jcs:edit-path"/>
<xsl:call-template name="jcs:statement"/>
<message>Missing a description for this T1 interface.</message>
</xnm:error>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
SLAX 语法
version 1.2;
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 $interface = interfaces/interface;
for-each ($interface[starts-with(name, 't1-')]) {
var $ifname = .;
if (not(description)) {
<xnm:error> {
call jcs:edit-path();
call jcs:statement();
<message> "Missing a description for this T1 interface.";
}
}
}
}
Python 语法
from junos import Junos_Configuration
import jcs
def main():
root = Junos_Configuration
for element in root.xpath(
"./interfaces/interface[starts-with(name,'t1-')]"):
# Missing description
if element.find('description') is None:
# Emit error message to console
jcs.emit_error("Missing a description for this T1 interface: "
+ element.find('name').text)
if __name__ == '__main__':
main()
配置
程序
分步过程
下载、启用并测试脚本:要测试提交脚本是否正确生成错误消息,请确保候选配置包含引发错误的条件。对于此示例,请确保 T1 接口的配置不包括语 description 句。
要测试本主题中的示例,请执行以下操作:
将脚本复制到文本文件中,根据需要将文件命名为 description.xsl、 description.slax 或 description.py ,然后将其复制到设备上的 /var/db/scripts/commit/ 目录。
注意:未签名的 Python 脚本必须由 root 用户或 Junos OS
super-user登录类中的用户拥有,并且只有文件所有者才能对文件具有写入权限。在配置模式下,在
[edit system scripts commit]层次结构级别配置file语句和脚本文件名。[edit] user@host# set system scripts commit file description.xsl
如果脚本是用 Python 编写的,请启用未签名的 Python 脚本的执行。
[edit] user@host# set system scripts language python
注意:将
language python3语句配置为使用 Python 3 执行 Python 脚本,或将语句配置language python为使用 Python 2.7 执行 Python 脚本。有关详细信息,请参阅 语言。如果每个 T1 接口的配置都包含该
description语句,请删除现有 T1 接口的描述以进行测试。[edit] user@host# delete interfaces t1–0/0/1 description
发出
commit命令以提交配置。user@host# commit
验证
验证脚本执行
目的
验证提交脚本生成的错误消息。
行动
查看命令的 commit 输出。commit 脚本为每个不包含 description 语句的 T1 接口生成一条错误消息。任何错误都会导致提交过程失败。
[edit] user@host# commit [edit interfaces interface t1-0/0/1] 'description' Missing a description for this T1 interface. [edit interfaces interface t1-0/0/2] 'description' Missing a description for this T1 interface. error: 2 errors reported by commit scripts error: commit script failure
要显示错误消息的 XML 格式版本,请发出 commit check | display xml 命令。
[edit interfaces t1-0/0/1]
user@host# commit check | display xml
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.0R1/junos">
<commit-results>
<routing-engine junos:style="normal">
<name>re0</name>
<xnm:error>
<edit-path>
[edit interfaces interface t1-0/0/1]
</edit-path>
<statement>
description
</statement>
<message>
Missing a description for this T1 interface.
</message>
</xnm:error>
<xnm:error>
<edit-path>
[edit interfaces interface t1-0/0/2]
</edit-path>
<statement>
description
</statement>
<message>
Missing a description for this T1 interface.
</message>
</xnm:error>
<xnm:error xmlns="http://xml.juniper.net/xnm/1.1/xnm"
xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm">
<message>
2 errors reported by commit scripts
</message>
</xnm:error>
<xnm:error xmlns="http://xml.juniper.net/xnm/1.1/xnm"
xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm">
<message>
commit script failure
</message>
</xnm:error>
</routing-engine>
</commit-results>
<cli>
<banner>[edit interfaces]</banner>
</cli>
</rpc-reply>
要显示提交脚本处理的详细跟踪,请发出 commit check | display detail 命令。
[edit interfaces t1-0/0/1]
user@host# commit check | display detail
2009-06-15 15:56:09 PDT: reading commit script configuration
2009-06-15 15:56:09 PDT: testing commit script configuration
2009-06-15 15:56:09 PDT: opening commit script '/var/db/scripts/commit/error.xsl'
2009-06-15 15:56:09 PDT: reading commit script 'error.xsl'
2009-06-15 15:56:09 PDT: running commit script 'error.xsl'
2009-06-15 15:56:09 PDT: processing commit script 'error.xsl'
[edit interfaces interface t1-0/0/1]
'description'
Missing a description for this T1 interface.
[edit interfaces interface t1-0/0/2]
'description'
Missing a description for this T1 interface.
2009-06-15 15:56:09 PDT: 2 errors from script 'error.xsl'
error: 2 errors reported by commit scripts
error: commit script failure