示例:为 LSP 配置管理组
管理组(也称为链接着色或资源类)是手动分配的描述链接颜色的属性。从概念上讲,具有相同颜色的链接属于同一类。您可以使用管理组来实现各种基于策略的 标签交换路径 (LSP) 设置。
此提交脚本示例搜索apply-macro在层次结构级别包含参数[edit protocols mpls]的color语句。对于每个apply-macro语句,脚本使用提供的数据生成瞬时更改,并将宏扩展为 LSP 的标准 Junos OS 管理组。
要求
此示例使用运行 Junos OS 的设备。
概述和提交脚本
在此示例中,Junos OS 管理进程 (mgd) 检查配置并查找apply-macro语句。对于在层次结构级别包含[edit protocols mpls]参数的每个apply-macro语句color,脚本会生成瞬时更改,并使用语句中apply-macro提供的数据将宏扩展为 LSP 的标准 Junos OS 管理组。
要使此示例正常工作, apply-macro 必须在层次结构级别包含一个 [edit protocols mpls] 语句,其中包含一组地址、一个 color 参数和一个 group-value 参数。提交脚本将每个地址转换为 LSP 配置,并将参数转换为 color 管理组。
有关此脚本的逐行说明,请参阅 示例:使用提交脚本宏创建自定义配置语法。
示例脚本以 XSLT 和 SLAX 语法显示:
XSLT 语法
<?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"> <xsl:variable name="mpls" select="protocols/mpls"/> <xsl:for-each select="$mpls/apply-macro[data/name = 'color']"> <xsl:variable name="color" select="data[name = 'color']/value"/> <xsl:for-each select="$mpls/apply-macro[data/name = 'group-value']"> <xsl:variable name="group-value" select="data[name = 'group-value']/value"/> <transient-change> <protocols> <mpls> <admin-groups> <name> <xsl:value-of select="$color"/> </name> <group-value> <xsl:value-of select="$group-value"/> </group-value> </admin-groups> <xsl:for-each select="data[not(value)]/name"> <label-switched-path> <name> <xsl:value-of select="concat($color, '-lsp-', .)"/> </name> <to><xsl:value-of select="."/></to> <admin-group> <include-any> <xsl:value-of select="$color"/> </include-any> </admin-group> </label-switched-path> </xsl:for-each> </mpls> </protocols> </transient-change> </xsl:for-each> </xsl:for-each> </xsl:template> </xsl:stylesheet>
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;
for-each ($mpls/apply-macro[data/name = 'group-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;
}
}
}
}
}
}
}
}
}
配置
程序
分步过程
要下载、启用和测试脚本,请执行以下操作:
将脚本复制到文本文件中,根据需要将文件命名为 lsp-admin.xsl 或 lsp-admin.slax ,然后将其复制到设备上的 /var/db/scripts/commit/ 目录。
选择以下测试配置节,然后按 Ctrl+c 将其复制到剪贴板。
如果您使用的是脚本的 SLAX 版本,请将层次结构级别的文件名
[edit system scripts commit file]更改为 lsp-admin.slax。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 ...
出现提示时,使用鼠标和粘贴图标粘贴剪贴板的内容。
按回车键。
按 Ctrl+d。
提交配置。
user@host# commit
验证
验证配置
目的
验证脚本的行为是否符合预期。
行动
show protocols mpls | display commit-scripts发出配置模式命令并查看输出。通过添加该| display commit-scripts选项,您可以查看由瞬时更改生成的配置语句。
使用脚本生成的更改
发出 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;
}
没有脚本生成的更改
配置模式命令的 show protocols mpls | display commit-scripts no-transients 输出不包括以下 label-switched-path 语句:
[edit]
user@host# show protocols mpls | display commit-scripts no-transients
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;
}
在没有管道display commit-scripts no-transients命令的情况下发出show protocols mpls命令时,您会看到相同的输出,因为此脚本不会生成任何持久更改:
[edit]
user@host# show 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;
}