创建提交脚本宏以读取自定义语法并生成相关配置语句
通过提交脚本宏,您可以将自定义配置语法扩展为标准 Junos OS 配置语句。语句中的 apply-macro 自定义语法本身不会对设备产生操作影响。要赋予语法以意义,必须有一个相应的提交脚本,该脚本使用该语法作为数据来生成相关的 Junos OS 语句。
要编写这样的脚本:
- 在脚本的开头,包括 提交脚本所需的样板中的相应提交脚本样板。为方便起见,现将其转载如下:
XSLT 样板
SLAX 样板<?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"> <!-- ... insert your code here ... --> </xsl:template> </xsl:stylesheet>
Python 样板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 { /* * insert your code here */ }from junos import Junos_Configuration import jcs if __name__ == '__main__': # insert your code here
- 在注释 “insert your code here,”所指示的位置,包含一些编程指令,用于检查指定层次结构级别上语
apply-macro句的配置,并将配置更改为包含标准 Junos OS 语法。有关使用这两种类型的指令并包括对 XSLT 语法的逐行分析的示例,请参阅 示例:使用提交脚本宏创建自定义配置语法。
- 使用有意义的名称保存脚本。
- 将脚本复制到硬盘上的 /var/db/scripts/commit 目录或闪存驱动器上的 /config/scripts/commit 目录。
有关设置提交脚本的存储位置的信息,请参阅 存储和启用 Junos 自动化脚本 和 在闪存中存储脚本。
- 通过在
[edit system scripts commit]层次结构级别配置file filename语句来启用脚本。[edit system scripts] user@host# set commit file filename
- 如果脚本生成瞬时更改,请配置
allow-transients语句。在
[edit system scripts commit]层次结构级别配置语句,使所有提交脚本能够进行瞬时更改。[edit system scripts] user@host# set commit allow-transients
或者,在支持的设备和版本上,在
[edit system scripts commit file filename]层次结构级别配置语句,以仅允许单个脚本进行瞬时更改。[edit system scripts] user@host# set commit file filename allow-transients
- 如果脚本是用 Python 编写的,请启用未签名的 Python 脚本的执行。
[edit system scripts] user@host# set language (python | python3)
- 提交配置。
[edit system scripts] user@host# commit and-quit
如果所有提交脚本均未出错地运行,则任何持久性更改都将加载到候选配置中,并且任何瞬时更改都将加载到检出配置中,但不会加载到候选配置中。然后,提交过程会通过验证配置并将更改传播到运行 Junos OS 的设备上受影响的进程来继续。