示例:设置最低 MTU
最大传输单元 (MTU) 是可在网络上的一个物理帧中传输的最大数据量或数据包大小(以字节为单位)。在此示例中,提交脚本测试 SONET/SDH 接口的 MTU。如果 MTU 小于指定的最低值,则提交脚本报告错误并导致提交操作失败。
要求
此示例使用使用 SONET/SDH 接口运行 Junos OS 的设备。
概述和提交脚本
此示例测试 SONET/SDH 接口的 MTU,报告 MTU 低于参数值 min-mtu
(此处设置为 2048)并导致提交操作失败。环 for
路选择所有从 so- 开始的 SONET/SDH 接口,并且这些接口的 MTU 语句已定义且小于的 min-mtu
值。对于选定接口,脚本将生成错误,包括配置层次结构中的接口位置以及为该接口配置的 MTU。
该示例脚本同时显示在 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:param name="min-mtu" select="2048"/> <xsl:template match="configuration"> <xsl:for-each select="interfaces/interface[starts-with(name, 'so-') and mtu and mtu < $min-mtu]"> <xnm:error> <xsl:call-template name="jcs:edit-path"/> <xsl:call-template name="jcs:statement"> <xsl:with-param name="dot" select="mtu"/> </xsl:call-template> <message> <xsl:text>SONET interfaces must have a minimum MTU of </xsl:text> <xsl:value-of select="$min-mtu"/> <xsl:text>.</xsl:text> </message> </xnm:error> </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"; param $min-mtu = 2048; match configuration { for-each (interfaces/interface[starts-with(name, 'so-') and mtu and mtu < $min-mtu]) { <xnm:error> { call jcs:edit-path(); call jcs:statement($dot = mtu); <message> { expr "SONET interfaces must have a minimum MTU of "; expr $min-mtu; expr "."; } } } }
配置
程序
逐步过程
要下载、启用和测试脚本:
将脚本复制到文本文件中,根据需要命名文件 so-mtu.xsl 或 so-mtu.slax ,然后将其复制到设备上的 /var/db/scripts/commit/ 目录。
选择以下测试配置部分,然后按 Ctrl+c 将其复制到剪贴板。
如果您使用脚本的 SLAX 版本,请将层次结构级别的文件
[edit system scripts commit file]
名更改为 so-mtu.slax。system { scripts { commit { file so-mtu.xsl; } } } interfaces { so-1/2/2 { mtu 2048; } so-1/2/3 { mtu 576; } }
在配置模式下
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
验证
验证提交脚本输出
目的
验证脚本是否按预期运行。
行动
查看命令的 commit
输出。样本配置部分配置了两个 SONET/SDH 接口,so-1/2/2 和 so-1/2/3。so-1/2/3 接口配置为 576 的 MTU,因此脚本生成错误消息,提交操作失败。发出 commit
命令后,将显示以下输出:
[edit] user@host# commit [edit interfaces interface so-1/2/3] 'mtu 576;' SONET interfaces must have a minimum MTU of 2048. error: 1 error reported by commit scripts error: commit script failure