事件脚本所需的样板
总结 定义事件脚本的样板。
Junos OS 事件脚本可使用可扩展样式表语言转换 (XSLT)、样式表语言替代语法 (SLAX) 或 Python 编写。事件脚本必须包括该脚本语言所需的必要样板,用于基本脚本功能以及脚本中使用的任何可选功能,例如 Junos OS 扩展功能和命名模板。本主题提供标准样板,可用于 XSLT、SLAX 和 Python 事件脚本。
SLAX 和 XSLT 事件脚本基于 Junos XML 和 Junos XML 协议标记元素。与所有 XML 元素一样,尖括号在其开口和关闭标记中附有 Junos XML 或 Junos XML 协议标记元素的名称。这是 XML 约定,并且托架是完整标记元素名称的必需部分。它们不会与文档中用于指示 Junos OS CLI 命令字符串可选部分的尖括号混淆。
用于事件脚本的 XSLT 样板
XSLT 事件脚本样板如下:
1 <?xml version="1.0" standalone="yes"?> 2 <xsl:stylesheet version="1.0" 3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 4 xmlns:junos="http://xml.juniper.net/junos/*/junos" 5 xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" 6 xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"> 7 <xsl:import href="../import/junos.xsl"/> 8 <xsl:template match="configuration"> 9 <event-script-results> <!-- ... Insert your code here ... --> 10 </event-script-results> 11 </xsl:template> <!-- ... insert additional template definitions here ... --> 12 </xsl:stylesheet>
第 1 行是可扩展标记语言 (XML) 处理说明 (PI)。此 PI 指定该代码使用版本 1.0 在 XML 中编写。XML PI(如果存在)必须是脚本文件中的第一个非评论令牌。
1 <?xml version="1.0"?>
第 2 行打开样式表,并将 XSLT 版本指定为 1.0。
2 <xsl:stylesheet version="1.0"
第 3 行至第 6 行列出了事件脚本中常用的所有名称空间映射。此示例中未使用所有这些前缀,但列出未引用的名称空间映射并非错误。如果映射在脚本的后续版本中使用,列出所有名称空间映射可防止发生错误。
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 4 xmlns:junos="http://xml.juniper.net/junos/*/junos" 5 xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" 6 xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0">
第 7 行是 XSLT 导入语句。它会从引用的文件中加载模板和变量。 /import/junos.xsl,作为 Junos OS 的一部分发货(在文件 /usr/libdata/cscript/import/junos.xsl 中)。 junos.xsl 文件包含一组可在脚本中调用的命名模板。在 《了解 Junos OS 自动化脚本中的命名模板》中讨论了这些名为模板的模板。
7 <xsl:import href="../import/junos.xsl"/>
第 8 行定义了与元素匹配的 </>
模板。该 <xsl:template match="/">
元素是根元素,表示 XML 层次结构的顶层。脚本中的所有 XPath 表达式都必须从顶层开始。这允许脚本访问所有可能的 Junos XML 和 Junos XML 协议远程过程调用 (RPC)。有关更多信息,请参阅 XPath 概述。
8 <xsl:template match="/">
标记 <xsl:template match="/">
元素之后, <event-script-results>
和 </event-script-results>
容器标记必须是顶级儿童标记,如第 9 行和第 10 行所示。
9 <event-script-results> <!-- ... insert your code here ... --> 10 </event-script-results>
第 11 行关闭模板。
11 </xsl:template>
在 11 号线和 12 号线之间,您可以定义从模板中 <xsl:template match="/">
调用的附加 XSLT 模板。
第 12 行关闭样式表和事件脚本。
12 </xsl:stylesheet>
事件脚本的 SLAX 样板
相应的 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 / { <event-script-results> { /* * Insert your code here */ } }
事件脚本的 Python 样板
Python 事件脚本没有所需的样板,但它们必须导入脚本中使用的任何对象。Python 事件脚本可导入以下内容:
Junos_Context
字典 — 包含有关脚本执行环境的信息。Junos_Trigger_Event
和Junos_Received_Events
对象 — 包含触发相应事件策略的事件的详细信息。Junos_Remote_Execution_Details
— 生成器功能,这是访问在层次结构级别上为事件脚本配置的远程执行详细信息所必需的[edit event-options event-script file filename remote-execution]
。jcs
库 — 允许脚本使用 Junos OS 扩展功能和脚本中名为模板功能的 Junos OS。jnpr.junos
模块和类 — 允许脚本使用 Junos PyEZ。
例如:
from junos import Junos_Context from junos import Junos_Trigger_Event from junos import Junos_Received_Events from junos import Junos_Remote_Execution_Details from jnpr.junos import Device import jcs if __name__ == '__main__':
Python 自动化脚本不需要在脚本开始时包含解释器指令行 (#!/usr/bin/env python
)。但是,如果存在程序,程序仍将正确执行。