帮助我们改善您的体验。

让我们了解您的想法。

您是否能抽出两分钟的时间完成一份问卷调查?

close
keyboard_arrow_left
自动化脚本用户指南
Table of Contents Expand all
list Table of Contents
keyboard_arrow_right

机器翻译对您有帮助吗?

starstarstarstarstar
Go to English page
免责声明:

我们将使用第三方机器翻译软件翻译本页面。瞻博网络虽已做出相当大的努力提供高质量译文,但无法保证其准确性。如果对译文信息的准确性有任何疑问,请参阅英文版本. 可下载的 PDF 仅提供英文版.

示例:基于简单接口配置创建复杂配置

date_range 31-Jan-22

此提交脚本示例使用巨集自动扩展简单接口配置。

要求

此示例使用运行 Junos OS 的设备。

概述和提交脚本

此示例使用提交脚本和巨集,通过生成 瞬态更改 来自动扩展简单接口配置,该更改分配默认封装类型,在接口上配置多个路由协议,并应用多个配置组。Junos OS 管理 (mgd) 进程检查配置,查找 apply-macro params 包含在层次结构级别的语句 [edit interfaces interface-name]

当脚本找到语句时 apply-macro params ,它将执行以下操作:

  • interface-details 配置组应用于接口。

  • 包括层次结构级别的参数[edit interfaces interface-name description]description

  • 包括层次结构级别的参数[edit interfaces interface-name encapsulation]encapsulation。如果语句中未包含apply-macro paramsencapsulation参数,脚本会将封装设置为cisco-hdlc默认值。

  • 将逻辑单元号设置为0并测试该参数是否inet-address包含在语句中apply-macro params。如果是,则脚本在层次结构级别包含[edit interfaces interface-name unit 0 family inet address]参数的值inet-address

  • 包括层次结构级别的接口名称 [edit protocols rsvp interface]

  • 在层次结构级别包含 [edit protocols isis interface interface-name]level 1 enable metric 语句。

  • 在层次结构级别包含 [edit protocols isis interface interface-name]level 2 enable metric 语句。

  • 测试语句中是否isis-level-1包含 apply-macro params or isis-level-1-metric 参数。如果包括其中一个或两个参数,则脚本将包含level 1层次结构级别的语句[edit protocols isis interface interface-name]isis-level-1如果包含参数,则脚本还会包含层次结构级别的参数enable(或disable[edit protocols isis interface interface-name level 1]的值isis-level-1isis-level-1-metric如果包含参数,则脚本还会在层次结构级别包含[edit protocols isis interface interface-name level 1 metric]参数的值isis-level-1-metric

  • 测试语句中是否isis-level-2包含 apply-macro params or isis-level-2-metric 参数。如果包括其中一个或两个参数,则脚本将包含level 2层次结构级别的语句[edit protocols isis interface interface-name]isis-level-2如果包含参数,则脚本还会包含层次结构级别的参数enable(或disable[edit protocols isis interface interface-name level 2]的值isis-level-2isis-level-2-metric如果包含参数,则脚本还会在层次结构级别包含[edit protocols isis interface interface-name level 2 metric]参数的值isis-level-2-metric

  • 包括层次结构级别的接口名称 [edit protocols ldp interface]

示例脚本以 XSLT 和 SLAX 语法显示:

XSLT 语法

content_copy zoom_out_map
<?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="top" select="."/>
        <xsl:for-each select="interfaces/interface/apply-macro[name = 'params']">
            <xsl:variable name="description" 
                    select="data[name = 'description']/value"/>
            <xsl:variable name="inet-address"
                    select="data[name = 'inet-address']/value"/>
            <xsl:variable name="encapsulation"
                    select="data[name = 'encapsulation']/value"/>
            <xsl:variable name="isis-level-1"
                    select="data[name = 'isis-level-1']/value"/>
            <xsl:variable name="isis-level-1-metric"
                    select="data[name = 'isis-level-1-metric']/value"/>
            <xsl:variable name="isis-level-2"
                    select="data[name = 'isis-level-2']/value"/>
            <xsl:variable name="isis-level-2-metric"
                    select="data[name = 'isis-level-2-metric']/value"/>
            <xsl:variable name="ifname" select="concat(../name, '.0')"/>
            <transient-change>
                <interfaces>
                    <interface>
                        <name><xsl:value-of select="../name"/></name>
                        <apply-groups>
                            <name>interface-details</name>
                        </apply-groups>
                        <xsl:if test="$description">
                            <description>
                                <xsl:value-of select="$description"/>
                            </description>
                        </xsl:if>
                        <encapsulation>
                            <xsl:choose>
                                <xsl:when test="string-length($encapsulation) &gt; 0">
                                    <xsl:value-of select="$encapsulation"/>
                                </xsl:when>
                                <xsl:otherwise>cisco-hdlc</xsl:otherwise>
                            </xsl:choose>
                        </encapsulation>
                        <unit>
                            <name>0</name>
                            <xsl:if test="string-length($inet-address) &gt; 0">
                                <family>
                                    <inet>
                                        <address>
                                            <xsl:value-of select="$inet-address"/>
                                        </address>
                                    </inet>
                                </family>
                            </xsl:if>
                        </unit>
                    </interface>
                </interfaces>
                <protocols>
                    <rsvp>
                        <interface>
                            <name><xsl:value-of select="$ifname"/></name>
                        </interface>
                    </rsvp>
                    <isis>
                        <interface>
                            <name><xsl:value-of select="$ifname"/></name>
                            <xsl:if test="$isis-level-1 or $isis-level-1-metric">
                                <level>
                                    <name>1</name>
                                    <xsl:if test="$isis-level-1">
                                        <xsl:element name="{$isis-level-1}"/>
                                    </xsl:if>
                                    <xsl:if test="$isis-level-1-metric">
                                        <metric>
                                            <xsl:value-of select="$isis-level-1-metric"/>
                                        </metric>
                                    </xsl:if>
                                </level>
                            </xsl:if>
                            <xsl:if test="$isis-level-2 or $isis-level-2-metric">
                                <level>
                                    <name>2</name>
                                    <xsl:if test="$isis-level-2">
                                        <xsl:element name="{$isis-level-2}"/>
                                    </xsl:if>
                                    <xsl:if test="$isis-level-2-metric">
                                        <metric>
                                            <xsl:value-of select="$isis-level-2-metric"/>
                                        </metric>
                                    </xsl:if>
                                </level>
                            </xsl:if>
                        </interface>
                    </isis>
                    <ldp>
                        <interface>
                            <name><xsl:value-of select="$ifname"/></name>
                        </interface>
                    </ldp>
                </protocols>
            </transient-change>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

SLAX 语法

content_copy zoom_out_map
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 $top = .;
    for-each (interfaces/interface/apply-macro[name = 'params']) {
        var $description = data[name = 'description']/value;
        var $inet-address = data[name = 'inet-address']/value;
        var $encapsulation = data[name = 'encapsulation']/value;
        var $isis-level-1 = data[name = 'isis-level-1']/value;
        var $isis-level-1-metric = data[name = 'isis-level-1-metric']/value;
        var $isis-level-2 = data[name = 'isis-level-2']/value;
        var $isis-level-2-metric = data[name = 'isis-level-2-metric']/value;
        var $ifname = ../name _  '.0';
        <transient-change> {
            <interfaces> {
                <interface> {
                    <name> ../name;
                    <apply-groups> {
                        <name> "interface-details";
                    }
                    if ($description) {
                        <description> $description;
                    }
                    <encapsulation> {
                        if (string-length($encapsulation) > 0) {
                            expr $encapsulation;
                        } else {
                            expr "cisco-hdlc";
                        }
                    }
                    <unit> {
                        <name> "0";
                        if (string-length($inet-address) > 0) {
                            <family> {
                                <inet> {
                                    <address> $inet-address;
                                }
                            }
                        }
                    }
                }
            }
            <protocols> {
                <rsvp> {
                    <interface> {
                        <name> $ifname;
                    }
                }
                <isis> {
                    <interface> {
                        <name> $ifname;
                        if ($isis-level-1 or $isis-level-1-metric) {
                            <level> {
                                <name> "1";
                                if ($isis-level-1) {
                                    <xsl:element name="{$isis-level-1}">;
                                }
                                if ($isis-level-1-metric) {
                                    <metric> $isis-level-1-metric;
                                }
                            }
                        }
                        if ($isis-level-2 or $isis-level-2-metric) {
                            <level> {
                                <name> "2";
                                if ($isis-level-2) {
                                    <xsl:element name="{$isis-level-2}">;
                                }
                                if ($isis-level-2-metric) {
                                    <metric> $isis-level-2-metric;
                                }
                            }
                        }
                    }
                }
                <ldp> {
                    <interface> {
                        <name> $ifname;
                    }
                }
            }
        }
    }
}

拓扑学

配置

程序

分步过程

要下载、启用和测试脚本,请执行以下操作:

  1. 将脚本复制到文本文件中,根据需要将文件命名为 if-params.xslif-params.slax ,然后将其复制到设备上的 /var/db/scripts/commit/ 目录。

  2. 选择以下测试配置节,然后按 Ctrl+c 将其复制到剪贴板。

    如果您使用的是脚本的 SLAX 版本,请将层次结构级别的文件名 [edit system scripts commit file] 更改为 if-params.slax

    content_copy zoom_out_map
    system {
        scripts {
            commit {
                allow-transients;
                file if-params.xsl;
            }
        }
    }
    groups {
        interface-details {
            interfaces {
                <so-*/*/*> {
                    clocking internal;
                }
            }
        }
    }
    interfaces {
        so-1/2/3 {
            apply-macro params {
                description "Link to Hoverville";
                encapsulation ppp;
                inet-address 10.1.2.3/28;
                isis-level-1 enable;
                isis-level-1-metric 50;
                isis-level-2-metric 85;
            }
        }
    }
    
  3. 在配置模式下,发出 load merge terminal 命令以将节合并到设备配置中。

    content_copy zoom_out_map
    [edit]
    user@host# load merge terminal
    [Type ^D at a new line to end input]
    ... Paste the contents of the clipboard here ...
    
    1. 出现提示时,使用鼠标和粘贴图标粘贴剪贴板的内容。

    2. 按回车键。

    3. 按 Ctrl+d。

  4. 提交配置。

    content_copy zoom_out_map
    user@host# commit
    

验证

验证配置

目的

验证脚本的行为是否符合预期。

行动

show interfaces | display commit-scripts | display inheritance发出配置模式命令。该| display commit-scripts选项显示配置中的所有语句,包括由瞬时更改生成的语句。该| display inheritance选项显示继承的配置数据以及有关从中继承配置的源组的信息。此选项还以展开格式显示接口范围配置数据,以及有关已从中扩展配置的源接口范围的信息。应会看到以下输出:

content_copy zoom_out_map
[edit]
user@host# show interfaces | display commit-scripts | display inheritance
so-1/2/3 {
    apply-macro params {
        clocking internal;
        description "Link to Hoverville";
        encapsulation ppp;
        inet-address 10.1.2.3/28;
        isis-level-1 enable;
        isis-level-1-metric 50;
        isis-level-2-metric 85;
    }
    description "Link to Hoverville";
    ##
    ## 'internal' was inherited from group 'interface-details'
    ##
    clocking internal;
    encapsulation ppp;
    unit 0 {
        family inet {
            address 10.1.2.3/28;
        }
    }
}

show protocols | display commit-scripts发出配置模式命令。应会看到以下输出:

content_copy zoom_out_map
[edit]
user@host# show protocols | display commit-scripts
rsvp {
    interface so-1/2/3.0;
}
isis {
    interface so-1/2/3.0 {
        level 1 {
            enable;
            metric 50;
        }
        level 2 metric 85;
    }
}
ldp {
    interface so-1/2/3.0;
}
external-footer-nav