示例:使用 SLAX 和 XSLT 操作脚本更改配置
此示例说明如何使用 SLAX 操作脚本对 Junos OS 配置进行结构化更改。
设备配置
分步过程
要下载、启用并测试脚本,请执行以下操作:
将脚本复制到文本文件中,将文件命名为 config-change.slax,并将其复制到设备上的 /var/db/scripts/op/ 目录中。
在配置模式下,在
[edit system scripts op file]
层次结构级别配置脚本的文件名。[edit system scripts op] user@host# set file config-change.slax
发出
commit and-quit
命令以提交配置并返回到操作模式。[edit] user@host# commit and-quit
在运行脚本之前,请发出
show interfaces interface-name
操作模式命令,并记录脚本将要禁用的接口的当前状态。执行 op 脚本。
user@host> op config-change This script disables the interface specified by the user. The script modifies the candidate configuration to disable the interface and commits the configuration to activate it. Enter interface to disable: so-0/0/0
要求
此示例使用运行 Junos OS 的设备。
概述和 op 脚本
SLAX 和 XSLT 操作脚本可以使用 jcs:load-configuration
位于 junos.xsl 导入文件中的模板对 Junos OS 配置进行结构化更改。此示例创建一个 SLAX 操作脚本,该脚本使用模板 jcs:load-configuration
禁用运行 Junos OS 的设备上的接口。模板所需的 jcs:load-configuration
所有值都定义为变量,然后将这些变量传递到模板中。
在此示例中, usage
变量使用脚本函数的一般描述进行初始化。运行脚本时,它会调用 jcs:output()
函数以将使用说明输出到 CLI。这使您能够验证是否出于正确的目的使用脚本。
该脚本调用 jcs:get-input()
该函数,该函数会提示输入要禁用的接口名称,并将接口名称存储在变量中 interface
。该 config-changes
变量存储要在设备上加载的 Junos XML 配置数据并引用该 interface
变量。 jcs:load-configuration
模板调用将参数的 configuration
值设置为变量中存储的数据 config-changes
。
load-action
变量设置为 merge
,用于将新配置数据与候选配置合并。这相当于 CLI 配置模式命令 load merge
。
该 options
变量定义提交操作的选项。它使用 :=
运算符创建一个节点集,该节点集作为参数的 commit-options
值传递给模板。此示例包含用于 log
将提交描述添加到提交日志以供将来参考的标记。
对该函数的 jcs:open()
调用将打开与本地设备上 Junos OS 管理进程 (mgd) 的连接,并返回存储在变量中的 conn
连接句柄。然后,脚本将调用模板 jcs:load-configuration
。
:=
运算符将模板调用的结果jcs:load-configuration
复制到临时变量,并在该变量上运行node-set
函数。然后,生成的节点集存储在变量中results
。:=
运算符确保results
变量是节点集而不是结果树片段,以便脚本可以访问内容。
该 jcs:close()
函数关闭与设备的连接。默认情况下, jcs:load-configuration
模板不会将消息输出到 CLI。此示例在响应中搜索并打印 xmn:warning
和 xnm:error
消息,以快速识别提交的任何问题。
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"; ns ext = "http://xmlsoft.org/XSLT/namespace"; import "../import/junos.xsl"; match / { <op-script-results> { var $usage = "This script disables the specified interface." _ "The script modifies the candidate configuration to disable " _ "the interface and commits the configuration to activate it."; var $temp = jcs:output($usage); var $interface = jcs:get-input("Enter interface to disable: "); var $config-changes = { <configuration> { <interfaces> { <interface> { <name> $interface; <disable>; } } } } var $load-action = "merge"; var $options := { <commit-options> { <log> "disabling interface " _ $interface; } } var $conn = jcs:open(); var $results := { call jcs:load-configuration( $action=$load-action, $commit-options=$options, $configuration=$config-changes, $connection=$conn); } var $close-results = jcs:close($conn); if ($results//xnm:error) { for-each ($results//xnm:error) { <output> message; } } if ($results//xnm:warning) { for-each ($results//xnm:warning) { <output> message; } } } }
验证
验证提交
目的
验证提交是否成功。
行动
应在脚本中包含代码,用于分析模板返回 jcs:load-configuration
的节点集,以查找任何错误或警告。这使您可以更轻松地确定提交是否成功。如果没有警告或错误消息,可以通过多种方式验证提交是否成功。
检查提交日志以验证提交是否成功。如果在参数中
commit-options
包含该log
选项,则该消息应与提交信息一起在提交日志中可见。user@host> show system commit 0 2010-09-22 17:08:17 PDT by user via junoscript disabling interface so-0/0/0
检查 syslog 消息文件,以验证是否已记录提交操作。在这种情况下,您还会看到
SNMP_TRAP_LINK_DOWN
禁用接口 so-0/0/0 的消息。根据 traceoptions 的配置设置,此消息可能会或可能不会出现在日志文件中。user@host> show log messages | last Sep 22 17:08:13 host file[7319]: UI_COMMIT: User 'user' requested 'commit' operation (comment: disabling interface so-0/0/0) Sep 22 17:08:16 host mib2d[1434]: SNMP_TRAP_LINK_DOWN: ifIndex 526, ifAdminStatus down(2), ifOperStatus down(2), ifName so-0/0/0
验证配置更改
目的
验证配置中是否集成了正确的更改。
行动
显示配置并验证指定接口的更改是否可见。
user@host> show configuration interfaces so-0/0/0 disable;
在此示例中,您还可以发出
show interfaces interface-name
操作模式命令来检查接口是否已禁用。在这种情况下,在禁用接口 之前 捕获的输出显示接口为Enabled
。user@host> show interfaces so-0/0/0 Physical interface: so-0/0/0, Enabled, Physical link is Up Interface index: 128, SNMP ifIndex: 526 Link-level type: PPP, MTU: 4474, Clocking: Internal, SONET mode, Speed: OC3, Loopback: None, FCS: 16, Payload scrambler: Enabled Device flags : Present Running Interface flags: Point-To-Point SNMP-Traps Internal: 0x4000 Link flags : Keepalives CoS queues : 4 supported, 4 maximum usable queues Last flapped : 2010-09-14 10:33:25 PDT (1w1d 06:27 ago) Input rate : 0 bps (0 pps) Output rate : 0 bps (0 pps) SONET alarms : None SONET defects : None
运行脚本以禁用接口 后 捕获的输出显示该接口现在是
Administratively down
。user@host> show interfaces so-0/0/0 Physical interface: so-0/0/0, Administratively down, Physical link is Up Interface index: 128, SNMP ifIndex: 526 Link-level type: PPP, MTU: 4474, Clocking: Internal, SONET mode, Speed: OC3, Loopback: None, FCS: 16, Payload scrambler: Enabled Device flags : Present Running Interface flags: Down Point-To-Point SNMP-Traps Internal: 0x4000 Link flags : Keepalives CoS queues : 4 supported, 4 maximum usable queues Last flapped : 2010-09-14 10:33:25 PDT (1w1d 06:40 ago) Input rate : 0 bps (0 pps) Output rate : 0 bps (0 pps) SONET alarms : None SONET defects : None