示例:在防火墙中添加决赛然后接受术语
此提交脚本示例会向尚未以显式then accept语句结尾的任何防火墙过滤器添加then accept语句。
要求
此示例使用运行 Junos OS 的设备。
概述和提交脚本
Junos OS 中的每个防火墙过滤器在过滤器末尾都有隐式丢弃操作,相当于以下显式过滤器术语:
term implicit-rule {
then discard;
}
因此,如果数据包与过滤器中没有一个术语匹配,则该术语将被丢弃。在某些情况下,您可能希望通过添加最后一个术语来接受与防火墙过滤器的一系列匹配条件不匹配的所有数据包,从而覆盖默认值。在此示例中,提交脚本会向尚未以显式then accept语句结尾的任何防火墙过滤器添加最终then accept语句。
该示例脚本同时显示在 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:apply-templates select="firewall/filter | firewall/family/inet | firewall/family/inet6" mode="filter"/> </xsl:template> <xsl:template match="filter" mode="filter"> <xsl:param name="last" select="term[position() = last()]"/> <xsl:comment> <xsl:text>Found </xsl:text> <xsl:value-of select="name"/> <xsl:text>; last </xsl:text> <xsl:value-of select="$last/name"/> </xsl:comment> <xsl:if test="$last and ($last/from or $last/to or not($last/then/accept))"> <xnm:warning> <xsl:call-template name="jcs:edit-path"/> <message> <xsl:text>filter is missing final 'then accept' rule</xsl:text> </message> </xnm:warning> <xsl:call-template name="jcs:emit-change"> <xsl:with-param name="content"> <term> <name>very-last</name> <junos:comment> <xsl:text>This term was added by a commit script</xsl:text> </junos:comment> <then> <accept/> </then> </term> </xsl:with-param> </xsl:call-template> </xsl:if> </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 {
apply-templates firewall/filter | firewall/family/inet | firewall/family/inet6 {
mode "filter";
}
}
match filter {
mode "filter";
param $last = term[position() = last()];
<xsl:comment> {
expr "Found ";
expr name;
expr "; last ";
expr $last/name;
}
if ($last and ($last/from or $last/to or not($last/then/accept))) {
<xnm:warning> {
call jcs:edit-path();
<message> "filter is missing final 'then accept' rule";
}
call jcs:emit-change() {
with $content = {
<term> {
<name> "very-last";
<junos:comment> "This term was added by a commit script";
<then> {
<accept>;
}
}
}
}
}
}
配置
程序
逐步过程
要下载、启用和测试脚本:
将脚本复制到文本文件中,根据需要命名文件 add-accept.xsl 或 add-accept.slax ,然后将其复制到设备上的 /var/db/scripts/commit/ directory。
选择以下测试配置部分,然后按 Ctrl+c 将其复制到剪贴板。
如果您使用脚本的 SLAX 版本,请将层次结构级别的文件
[edit system scripts commit file]名更改为 add-accept.slax。system { scripts { commit { file add-accept.xsl; } } } firewall { policer sgt-friday { if-exceeding { bandwidth-percent 10; burst-size-limit 250k; } then discard; } family inet { filter test { term one { from { interface t1-0/0/0; } then { count ten-network; discard; } } term two { from { forwarding-class assured-forwarding; } then discard; } } } } interfaces { t1-0/0/0 { unit 0 { family inet { policer output sgt-friday; filter input test; } } } }在配置模式下
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 输出。脚本要求所有防火墙过滤器都以显式 then accept 语句结尾。样本配置部分包括带有两个术语的 test 过滤器,但不包括显式 then accept 语句。发出 commit 命令时,脚本会添加缺少 then accept 的语句并提交配置。发出 commit 命令时,将显示以下输出:
[edit] user@host# commit [edit firewall family inet filter test] warning: filter is missing final 'then accept' rule commit complete
在配置模式下 show firewall ,发出 命令来查看修改后的配置。显示以下输出:
[edit]
user@host# show firewall
policer sgt-friday {
if-exceeding {
bandwidth-percent 10;
burst-size-limit 250k;
}
then discard;
}
family inet {
filter test {
term one {
from {
interface t1-0/0/0;
}
then {
count ten-network;
discard;
}
}
term two {
from {
forwarding-class assured-forwarding;
}
then {
discard;
}
}
term very-last {
then accept; /* This term was added by a commit script */
}
}
}