事件脚本所需的样板
总结 定义事件脚本的样板。
Junos OS脚本可以在可扩展样式表语言转换 (XSLT)、样式表语言替代 syntaX (SLAX) 或 Python 中编写。事件脚本必须包含该脚本语言所需的必要样板,这既包括基本脚本功能,也包括脚本内使用的任何可选功能,例如 Junos OS 扩展功能和 命名模板。本主题提供可在 XSLT、SLAX 和 Python 事件脚本中使用的标准样板。
SLAX 和 XSLT 事件脚本基于 Junos XML 和Junos XML 协议标记元素。如同所有 XML 元素,尖括号在其开始标记和结束标记中Junos或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 op 脚本样板如下所示:
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
)。但是,如果存在程序,程序仍将正确执行。