事件脚本所需的样板
总结 定义事件脚本的样板。
Junos OS 事件脚本可以用可扩展样式表语言转换 (XSLT)、样式表替代语言 syntaX (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="/">
在 tag 元素之后,<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.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 / { <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
library - 使脚本能够在脚本中使用 Junos OS 扩展函数和 Junos OS 命名模板功能。jnpr.junos
module and classes - 使脚本能够使用 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
)。但是,如果存在,程序仍将正确执行。