示例:使用 Commit 脚本宏创建自定义配置语法
Junos OS 配置可包含 apply-macro
带有自定义配置语法的语句。该语句本身 apply-macro
对设备没有操作影响。提交脚本宏处理自定义配置语法,并将其扩展为标准 Junos OS 配置语句,然后作为持久或瞬时更改添加。此示例演示了如何使用提交脚本宏来检查 apply-macro
语句并生成 Junos OS 配置语句。
要求
此示例使用以下硬件和软件组件:
运行 Junos OS 的设备。
使用 Python 脚本时,Junos OS 版本 16.1R3 或更高版本。
概述和提交脚本
表 1 显示了包含自定义语法的宏,以及对标准 Junos OS 命令行界面 (CLI) 语法的相应扩展。
自定义宏语法 |
扩展 Junos OS CLI 语法 |
---|---|
protocols { mpls { apply-macro blue-type-lsp { 10.1.1.1; 10.2.2.2; 10.3.3.3; 10.4.4.4; color blue; group-value 0; } } } |
protocols { mpls { admin-groups { blue 0; } label-switched-path blue-lsp-10.1.1.1 { to 10.1.1.1; admin-group include-any blue; } label-switched-path blue-lsp-10.2.2.2 { to 10.2.2.2; admin-group include-any blue; } label-switched-path blue-lsp-10.3.3.3 { to 10.3.3.3; admin-group include-any blue; } label-switched-path blue-lsp-10.4.4.4 { to 10.4.4.4; admin-group include-any blue; } } } |
在此示例中,Junos OS 管理 (mgd) 进程会检查配置,查找apply-macro
语句。对于在层次结构级别中[edit protocols mpls]
包含参数的每个apply-macro
语句color
,脚本都会生成瞬时更改,使用语句中apply-macro
提供的数据将宏扩展为 LSP 的标准 Junos OS 管理组。
对于此工作示例,必须在层次结构级别中包含一个apply-macro
语句,其中包含一组地址、一个color
和一个group-value
[edit protocols mpls]
参数。提交脚本将每个地址转换为 LSP 配置,而脚本将该参数转换color
为管理组。
以下是在 表 1 中扩展宏的提交脚本说明以及脚本的逐行说明:
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 <xsl:variable name="mpls" select="protocols/mpls"/> 10 <xsl:for-each select="$mpls/apply-macro[data/name = 'color']"> 11 <xsl:variable name="color" select="data[name = 'color']/value"/> 12 <xsl:variable name="group-value" select="data[name = \ 'group-value']/value"/> 13 <transient-change> 14 <protocols> 15 <mpls> 16 <admin-groups> 17 <name> 18 <xsl:value-of select="$color"/> 19 </name> 20 <group-value> 21 <xsl:value-of select="$group-value"/> 22 </group-value> 23 </admin-groups> 24 <xsl:for-each select="data[not(value)]/name"> 25 <label-switched-path> 26 <name> 27 <xsl:value-of select="concat($color, '-lsp-', .)"/> 28 </name> 29 <to><xsl:value-of select="."/></to> 30 <admin-group> 31 <include-any> 32 <xsl:value-of select="$color"/> 33 </include-any> 34 </admin-group> 35 </label-switched-path> 36 </xsl:for-each> 37 </mpls> 38 </protocols> 39 </transient-change> 40 </xsl:for-each> 41 </xsl:template> 42 </xsl:stylesheet>
第 1 行到 8 行(以及第 43 行和第 44 行)是您在每个 XSLT 提交脚本中都包含的样板。为了简洁起见,此处省略了第 1 至 8 行。
第 9 行将层次结构级别分配 [edit protocols mpls]
给一个称为的 mpls
变量。
9 <xsl:variable name="mpls" select="protocols/mpls"/>
第 10 行在[edit protocols mpls]
层级选择包含color
参数的每个apply-macro
语句。表 1 中的样本配置仅包含一个apply-macro
语句。因此,此<xsl:for-each>
编程指令仅生效一次。
10 <xsl:for-each select="$mpls/apply-macro[data/name = 'color']">
第 11 行将参数的 color
值(在此情况下 blue
)分配给称为 的 color
变量。
11 <xsl:variable name="color" select="data[name = 'color']/value"/>
第 12 行将参数的 group-value
值(在此情况下 0
)分配给称为的 group-value
变量。
12 <xsl:variable name="group-value" select="data[name = \ 'group-value']/value"/>
第 13 行到 15 行在 [edit protocols mpls]
层次结构级别生成瞬时更改。
13 <transient-change> 14 <protocols> 15 <mpls>
第 16 行到 23 行将语句添加到 admin-groups
配置中,并将变量的 color
值分配给组名称,并将变量的 group-value
值分配给组值。
16 <admin-groups> 17 <name> 18 <xsl:value-of select="$color"/> 19 </name> 20 <group-value> 21 <xsl:value-of select="$group-value"/> 22 </group-value> 23 </admin-groups>
生成的配置语句如下:
admin-groups { blue 0; }
第 24 行选择未为其分配值的每个参数的名称,在这种情况下,这些参数是四个 IP 地址。此 <xsl:for-each>
编程说明使用宏的递归,并依次选择每个 IP 地址。每个 color
和 group-value
参数都有一个分配的值(blue
和 0
分别),因此此行不适用于它们。
24 <xsl:for-each select="data[not(value)]/name">
第 25 行在配置中添加语 label-switched-path
句。
25 <label-switched-path>
第 26 行至第 28 行分配 label-switched-path
一个将变量的值 color
、文本 -lsp
和当前 IP 地址(当前由第 24 行选定)(表示为 “ ) .
的名称。
26 <name> 27 <xsl:value-of select="concat($color, '-lsp-', .)"/> 28 </name>
第 29 行将语句添加到 to
配置中,并设置其值到当前由第 24 行选择的 IP 地址。
29 <to><xsl:value-of select="."/></to>
第 30 行到 34 行将语句添加到 admin-group include-any
配置中,并将其值设置为变量的 color
值。
30 <admin-group> 31 <include-any> 32 <xsl:value-of select="$color"/> 33 </include-any> 34 </admin-group>
生成的配置语句(一次通过)如下所示:
label-switched-path blue-lsp-10.1.1.1 { to 10.1.1.1; admin-group include-any blue; }
第 35 行到 42 行是关闭标记。
35 </label-switched-path> 36 </xsl:for-each> 37 </mpls> 38 </protocols> 39 </transient-change> 40 </xsl:for-each> 41 </xsl:template> 42 </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 configuration { var $mpls = protocols/mpls; for-each ($mpls/apply-macro[data/name = 'color']) { var $color = data[name = 'color']/value; var $group-value = data[name='group-value']/value; <transient-change> { <protocols> { <mpls> { <admin-groups> { <name> $color; <group-value> $group-value; } for-each (data[not(value)]/name) { <label-switched-path> { <name> $color _ '-lsp-' _ .; <to> .; <admin-group> { <include-any> $color; } } } } } } } }
Python 语法
以下 Python 语法可生成相同的配置更改:
from junos import Junos_Configuration import jcs def main(): # Get configuration root object root = Junos_Configuration for element in root.xpath("./protocols/mpls/ \ apply-macro[data/name='color']"): color = element.find("data[name='color']/value").text group_value = element.find("data[name='group-value']/value").text lsp_config ="" for element2 in element.xpath("data[not(value)]/name"): lsp_config = lsp_config + """ <label-switched-path> <name>{0}-lsp-{1}</name> <to>{1}</to> <admin-group> <include-any>{0}</include-any> </admin-group> </label-switched-path> """.format(color, element2.text) change_xml = """ <protocols> <mpls> <admin-groups> <name>{0}</name> <group-value>{1}</group-value> </admin-groups> {2} </mpls> </protocols> """.format(color, group_value, lsp_config).strip() jcs.emit_change(change_xml, "transient-change", "xml") if __name__ == '__main__': main()
有关此示例的详细信息,请参阅 示例:为 LSP 配置管理组。
配置
程序
逐步过程
要下载、启用和测试脚本:
将脚本复制到文本文件中,命名文件 lsp-admin.xsl、 lsp-admin.slax 或 lsp-admin.py ,并将其复制到设备上的 /var/db/scripts/commit 目录。
注意:未签名的 Python 脚本必须归 Junos OS
super-user
登录类中的 root 或用户所有,并且只有文件所有者才能为该文件写入许可。如果脚本写在 Python 中,则允许执行未签名的 Python 脚本。
[edit] user@host# set system scripts language python
注意:将语句配置
language python3
为使用 Python 3 执行 Python 脚本,或将language python
语句配置为使用 Python 2.7 执行 Python 脚本。有关更多信息,请参阅 语言。选择以下测试配置部分,然后按 Ctrl+c 将其复制到剪贴板。如果您使用脚本的 SLAX 或 Python 版本,请在
[edit system scripts commit file]
层次结构级别更新文件名。system { scripts { commit { allow-transients; file lsp-admin.xsl; } } } protocols { mpls { apply-macro blue-type-lsp { 10.1.1.1; 10.2.2.2; 10.3.3.3; 10.4.4.4; color blue; group-value 0; } } }
在配置模式下
load merge terminal
,发出 命令将部分合并到设备配置中。[edit] user@host# load merge terminal [Type ^D at a new line to end input] ... Paste the contents of the clipboard here ...
出现提示时,使用鼠标和粘贴图标粘贴剪贴板的内容。
按 Enter。
按下 Ctrl+d。
提交配置。
user@host# commit
验证
验证脚本执行
目的
验证脚本是否按预期运行。
行动
要显示脚本创建的配置语句,请发出 show protocols mpls | display commit-scripts
命令。
[edit] user@host# show protocols mpls | display commit-scripts apply-macro blue-type-lsp { 10.1.1.1; 10.2.2.2; 10.3.3.3; 10.4.4.4; color blue; group-value 0; } admin-groups { blue 0; } label-switched-path blue-lsp-10.1.1.1 { to 10.1.1.1; admin-group include-any blue; } label-switched-path blue-lsp-10.2.2.2 { to 10.2.2.2; admin-group include-any blue; } label-switched-path blue-lsp-10.3.3.3 { to 10.3.3.3; admin-group include-any blue; } label-switched-path blue-lsp-10.4.4.4 { to 10.4.4.4; admin-group include-any blue; }