示例:生成自定义警告消息
Junos OS 提交脚本可以在提交操作期间生成自定义警告消息,以便在配置不符合自定义配置规则时向您发出提醒。提交过程不受警告的影响。此示例创建一个提交脚本,当设备配置中不包含特定语句时,该脚本将生成自定义警告消息。
要求
使用 Python 脚本时为 Junos OS 16.1R3 或更高版本。
概述和提交脚本
使用提交脚本,编写一条自定义警告消息,当语句未包含在[edit chassis]
层次结构级别时source-route
显示该消息。
该脚本以 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:if test="not(chassis/source-route)"> <xnm:warning> <xsl:call-template name="jcs:edit-path"> <xsl:with-param name="dot" select="chassis"/> </xsl:call-template> <message>IP source-route processing is not enabled.</message> </xnm:warning> </xsl:if> </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 { if (not(chassis/source-route)) { <xnm:warning> { call jcs:edit-path($dot = chassis); <message> "IP source-route processing is not enabled."; } } }
Python 语法
from junos import Junos_Configuration import jcs def main(): root = Junos_Configuration if not(root.xpath("./chassis/source-route")): jcs.emit_warning("IP source-route processing is not enabled.") if __name__ == '__main__': main()
配置
程序
分步过程
下载、启用并测试脚本。若要测试提交脚本是否正确生成警告消息,请确保候选配置包含引发警告的条件。对于此示例,请确保 source-route
语句未包含在 [edit chassis]
层次结构级别。
要测试本主题中的示例,请执行以下操作:
将脚本复制到文本文件中,将文件命名为 source-route.xsl、 source-route.slax 或 source-route.py ,然后将其复制到设备上的 /var/db/scripts/commit/ 目录中。
注意:未签名的 Python 脚本必须由 root 用户或 Junos OS
super-user
登录类中的用户拥有,并且只有文件所有者才能对文件具有写入权限。在配置模式下,在
[edit system scripts commit]
层次结构级别配置file
语句和脚本文件名。[edit] user@host# set system scripts commit file source-route.xsl
如果脚本是用 Python 编写的,请启用未签名的 Python 脚本的执行。
[edit] user@host# set system scripts language python
注意:将
language python3
语句配置为使用 Python 3 执行 Python 脚本,或将语句配置language python
为使用 Python 2.7 执行 Python 脚本。有关详细信息,请参阅 语言。source-route
如果语句包含在[edit chassis]
层次结构级别,请发出delete chassis source-route
配置模式命令。[edit] user@host# delete chassis source-route
验证
验证脚本执行
目的
验证提交脚本生成的警告消息。
行动
执行commit check
或 commit
命令并查看输出。当配置的[edit chassis]
层次结构级别不包含语句时source-route
,commit 脚本将生成警告消息。该警告不会影响提交过程。
[edit] user@host# commit check [edit chassis] warning: IP source-route processing is not enabled. configuration check succeeds
[edit] user@host# commit [edit chassis] warning: IP source-route processing is not enabled. commit complete
要显示警告消息的 XML 格式版本,请发出 commit check | display xml
命令。
[edit] 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:warning> <edit-path> [edit chassis] </edit-path> <message> IP source-route processing is not enabled. </message> </xnm:warning> <commit-check-success/> </routing-engine> </commit-results> </rpc-reply>
要显示提交脚本处理的详细跟踪,请发出 commit check | display detail
命令。
[edit] user@host# commit check | display detail 2009-06-15 14:40:29 PDT: reading commit script configuration 2009-06-15 14:40:29 PDT: testing commit script configuration 2009-06-15 14:40:29 PDT: opening commit script '/var/db/scripts/commit/source-route-warning.xsl' 2009-06-15 14:40:29 PDT: reading commit script 'source-route-warning.xsl' 2009-06-15 14:40:29 PDT: running commit script 'source-route-warning.xsl' 2009-06-15 14:40:29 PDT: processing commit script 'source-route-warning.xsl' [edit chassis] warning: IP source-route processing is not enabled. 2009-06-15 14:40:29 PDT: no errors from source-route-warning.xsl 2009-06-15 14:40:29 PDT: saving commit script changes 2009-06-15 14:40:29 PDT: summary: changes 0, transients 0 (allowed), syslog 0 2009-06-15 14:40:29 PDT: no commit script changes 2009-06-15 14:40:29 PDT: exporting juniper.conf 2009-06-15 14:40:29 PDT: expanding groups 2009-06-15 14:40:29 PDT: finished expanding groups 2009-06-15 14:40:29 PDT: setup foreign files 2009-06-15 14:40:29 PDT: propagating foreign files 2009-06-15 14:40:30 PDT: complete foreign files 2009-06-15 14:40:30 PDT: daemons checking new configuration configuration check succeeds