이 페이지의
예: Op 스크립트를 사용하여 show 인터페이스의 출력 사용자 정의 terse 명령
이 예에서는 op 스크립트 를 사용하여 명령의 출력을 사용자 지정합니다 show interfaces terse . XSLT 스크립트에 대한 라인별 설명이 제공됩니다.
요구 사항
이 예에서는 Junos OS를 실행하는 디바이스를 사용합니다.
개요 및 운영 스크립트
기본적으로 명령의 show interfaces terse 레이아웃은 다음과 같습니다.
user@host> show interfaces terse
Interface Admin Link Proto Local Remote
dsc up up
fxp0 up up
fxp0.0 up up inet 192.168.71.246/21
fxp1 up up
fxp1.0 up up inet 10.0.0.4/8
inet6 fe80::200:ff:fe00:4/64
fc00::10:0:0:4/64
tnp 4
gre up up
ipip up up
lo0 up up
lo0.0 up up inet 127.0.0.1 --> 0/0
lo0.16385 up up inet
inet6 fe80::2a0:a5ff:fe12:2f04
lsi up up
mtun up up
pimd up up
pime up up
tap up up
Junos XML에서 출력 필드는 다음과 같이 표시됩니다.
user@host> show interfaces terse | display xml <rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.0R1/junos"> <interface-information xmlns="http://xml.juniper.net/junos/10.0R1/junos-interface" junos:style="terse"> <physical-interface> <name>dsc</name> <admin-status>up</admin-status> <oper-status>up</oper-status> </physical-interface> <physical-interface> <name>fxp0</name> <admin-status>up</admin-status> <oper-status>up</oper-status> <logical-interface> <name>fxp0.0</name> <admin-status>up</admin-status> <oper-status>up</oper-status> ... Remainder of output omitted for brevity ...
XSLT 구문
다음 스크립트가 명령의 show interfaces terse 출력을 사용자 정의합니다. 스크립트에 대한 라인별 설명이 제공됩니다.
1 <?xml version="1.0" standalone="yes"?> 2 <xsl:stylesheet version="1.0" 3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 4 xmlns:junos="http://xml.juniper.net/junos/*/junos" 5 xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" 6 xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"> 7 <xsl:import href="../import/junos.xsl"/> 8 <xsl:variable name="arguments"> 9 <argument> 10 <name>interface</name> 11 <description>Name of interface to display</description> 12 </argument> 13 <argument> 14 <name>protocol</name> 15 <description>Protocol to display (inet, inet6)</description> 16 </argument> 17 </xsl:variable> 18 <xsl:param name="interface"/> 19 <xsl:param name="protocol"/> 20 <xsl:template match="/"> 21 <op-script-results> 22 <xsl:variable name="rpc"> 23 <get-interface-information> 24 <terse/> 25 <xsl:if test="$interface"> 26 <interface-name> 27 <xsl:value-of select="$interface"/> 28 </interface-name> 29 </xsl:if> 30 </get-interface-information> 31 </xsl:variable> 32 <xsl:variable name="out" select="jcs:invoke($rpc)"/> 33 <interface-information junos:style="terse"> 34 <xsl:choose> 35 <xsl:when test="$protocol='inet' or $protocol='inet6' or $protocol='mpls' or $protocol='tnp'"> 36 <xsl:for-each select="$out/physical-interface/ logical-interface[address-family/address-family-name = $protocol]"> 37 <xsl:call-template name="intf"/> 38 </xsl:for-each> 39 </xsl:when> 40 <xsl:when test="$protocol"> 41 <xnm:error> 42 <message> 43 <xsl:text>invalid protocol: </xsl:text> 44 <xsl:value-of select="$protocol"/> 45 </message> 46 </xnm:error> 47 </xsl:when> 48 <xsl:otherwise> 49 <xsl:for-each select="$out/physical-interface/logical-interface"> 50 <xsl:call-template name="intf"/> 51 </xsl:for-each> 52 </xsl:otherwise> 53 </xsl:choose> 54 </interface-information> 55 </op-script-results> 56 </xsl:template> 57 <xsl:template name="intf"> 58 <xsl:variable name="status"> 59 <xsl:choose> 60 <xsl:when test="admin-status='up' and oper-status='up'"> 61 <xsl:text> </xsl:text> 62 </xsl:when> 63 <xsl:when test="admin-status='down'"> 64 <xsl:text>offline</xsl:text> 65 </xsl:when> 66 <xsl:when test="oper-status='down' and ../admin-status='down'"> 67 <xsl:text>p-offline</xsl:text> 68 </xsl:when> 69 <xsl:when test="oper-status='down' and ../oper-status='down'"> 70 <xsl:text>p-down</xsl:text> 71 </xsl:when> 72 <xsl:when test="oper-status='down'"> 73 <xsl:text>down</xsl:text> 74 </xsl:when> 75 <xsl:otherwise> 76 <xsl:value-of select="concat(oper-status, '/', admin-status)"/> 77 </xsl:otherwise> 78 </xsl:choose> 79 </xsl:variable> 80 <xsl:variable name="desc"> 81 <xsl:choose> 82 <xsl:when test="description"> 83 <xsl:value-of select="description"/> 84 </xsl:when> 85 <xsl:when test="../description"> 86 <xsl:value-of select="../description"/> 87 </xsl:when> 88 </xsl:choose> 89 </xsl:variable> 90 <logical-interface> 91 <name><xsl:value-of select="name"/></name> 92 <xsl:if test="string-length($desc)"> 93 <admin-status><xsl:value-of select="$desc"/></admin-status> 94 </xsl:if> 95 <admin-status><xsl:value-of select="$status"/></admin-status> 96 <xsl:choose> 97 <xsl:when test="$protocol"> 98 <xsl:copy-of select="address-family[address-family-name = $protocol]"/> 99 </xsl:when> 100 <xsl:otherwise> 101 <xsl:copy-of select="address-family"/> 102 </xsl:otherwise> 103 </xsl:choose> 104 </logical-interface> 105 </xsl:template> 106 </xsl:stylesheet>
라인별 설명
1-7호선, 20호선, 105호선 및 106호선은 모든 운영 스크립트에 포함된 상용구입니다. 자세한 내용은 Op 스크립트를 위한 필수 상용구(Boilerplate)를 참조하십시오.
1 <?xml version="1.0" standalone="yes"?> 2 <xsl:stylesheet version="1.0" 3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 4 xmlns:junos="http://xml.juniper.net/junos/*/junos" 5 xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" 6 xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"> 7 <xsl:import href="../import/junos.xsl"/> ... 20 <xsl:template match="/"> ... 105 </xsl:template> 106 </xsl:stylesheet>
줄 8 ~ 17은 스크립트에 대한 두 개의 인수를 포함하는 , 라는 arguments변수를 선언합니다.protocolinterface 이 변수 선언은 스크립트에 사용 가능한 인수로 명령줄 인터페이스(CLI)에 표시 interface protocol 하고 표시합니다.
8 <xsl:variable name="arguments"> 9 <argument> 10 <name>interface</name> 11 <description>Name of interface to display</description> 12 </argument> 13 <argument> 14 <name>protocol</name> 15 <description>Protocol to display (inet, inet6)</description> 16 </argument> 17 </xsl:variable>
18호와 19호는 8호선에서 17번 라인으로 생성된 인수에 해당하는 2개의 매개 변수를 스크립트에 선언합니다. 매개 변수 이름은 인수 이름과 정확히 일치해야 합니다.
18 <xsl:param name="interface"/> 19 <xsl:param name="protocol"/>
20-31선을 지정한 변수 rpc를 선언합니다. show interfaces terse 이 명령은 변수에 rpc 할당됩니다. 스크립트를 실행할 때 인수를 interface 포함하면 인수(인터페이스 이름)의 값이 스크립트로 전달됩니다.
20 <xsl:template match="/"> 21 <op-script-results> 22 <xsl:variable name="rpc"> 23 <get-interface-information> 24 <terse/> 25 <xsl:if test="$interface"> 26 <interface-name> 27 <xsl:value-of select="$interface"/> 28 </interface-name> 29 </xsl:if> 30 </get-interface-information> 31 </xsl:variable>
Line 32는 명명된 out 변수를 선언하고 해당 변수(show interfaces terse명령)의 rpc 실행에 적용합니다.
32 <xsl:variable name="out" select="jcs:invoke($rpc)"/>
Line 33은 terse 수정되는 명령의 show interfaces 출력 수준이 (, detail등과 반대extensive) 되도록 지정합니다.
33 <interface-information junos:style="terse">
줄 34 ~ 39는 스크립트를 실행할 때 인수를 포함 protocol 하거나 지정inet한 프로토콜 값이 , inet6mpls또는 tnpintf 템플릿이 출력에서 해당 프로토콜 유형의 각 인스턴스에 적용되도록 지정합니다.
34 <xsl:choose> 35 <xsl:when test="$protocol='inet' or $protocol='inet6' or $protocol='mpls' or $protocol='tnp'"> 36 <xsl:for-each select="$out/physical-interface/ logical-interface[address-family/address-family-name = $protocol]"> 37 <xsl:call-template name="intf"/> 38 </xsl:for-each> 39 </xsl:when>
40-47 줄은 스크립트를 실행할 때 인수를 포함하고 protocol 사용자가 지정한 프로토콜 값이 에inet6mplstnp러 메시지가 생성되는 경우inet, 또는 다른 프로토콜 값이 아니라는 것을 지정합니다.
40 <xsl:when test="$protocol"> 41 <xnm:error> 42 <message> 43 <xsl:text>invalid protocol: </xsl:text> 44 <xsl:value-of select="$protocol"/> 45 </message> 46 </xnm:error> 47 </xsl:when>
줄 48 ~ 52는 스크립트를 실행할 때 인수를 protocol 포함하지 않으면 템플릿이 출력의 intf 각 논리적 인터페이스에 적용되도록 지정합니다.
48 <xsl:otherwise> 49 <xsl:for-each select="$out/physical-interface/logical-interface"> 50 <xsl:call-template name="intf"/> 51 </xsl:for-each> 52 </xsl:otherwise>
53-56호선은 폐쇄 태그입니다.
53 </xsl:choose> 54 </interface-information> 55 </op-script-results> 56 </xsl:template>
57행에서 템플릿이 intf 열립니다. 이 템플릿은 명령의 출력을 show interfaces terse 사용자 정의합니다.
57 <xsl:template name="intf">
Line 58은 인터페이스 상태를 보고하는 방법을 지정하는 용도인 변수status를 선언합니다. 59-78행은 가능한 모든 상태를 고려하여 변수를 status 채우는 명령이 포함되어 <xsl:choose> 있습니다. XSLT에서와 마찬가지로 TRUE로 평가되는 첫 번째 <xsl:when> 명령이 실행되고 나머지는 무시됩니다. 각 <xsl:when> 지침은 별도로 설명됩니다.
58 <xsl:variable name="status"> 59 <xsl:choose>
60-62줄은 '업'이면 oper-status 출력이 생성되지 않음을 지정 admin-status 합니다. 이 경우 변수는 status 비어 있습니다.
60 <xsl:when test="admin-status='up' and oper-status='up'"> 61 <xsl:text> </xsl:text> 62 </xsl:when>
63-65줄은 '다운'인 경우 admin-status 변수에 status 텍스트 offline가 포함되도록 지정합니다.
63 <xsl:when test="admin-status='down'"> 64 <xsl:text>offline</xsl:text> 65 </xsl:when>
66-68행은 '다운'된 경우 물리적 인터페이스 admin-status 가 '다운'된 경우 oper-status 변수에 status 텍스트p-offline가 포함되도록 지정합니다. (../물리적 인터페이스를 선택합니다.)
66 <xsl:when test="oper-status='down' and ../admin-status='down'"> 67 <xsl:text>p-offline</xsl:text> 68 </xsl:when>
69-71호는 '다운'하고 물리적 인터페이스 oper-status 가 '다운'된 경우 oper-status 변수에 status 텍스트p-down가 포함되도록 지정합니다. (../물리적 인터페이스를 선택합니다.)
69 <xsl:when test="oper-status='down' and ../oper-status='down'"> 70 <xsl:text>p-down</xsl:text> 71 </xsl:when>
줄 72 ~ 74는 'down'인 경우 oper-status 변수에 status 텍스트 down가 포함되도록 지정합니다.
72 <xsl:when test="oper-status='down'"> 73 <xsl:text>down</xsl:text> 74 </xsl:when>
75줄에서 77까지는 테스트 케이스 중 어느 것도 사실이 아니라면 이 변수가 구분 기호로 슬래시(slash)로 포함 oper-status 되고 admin-status 연결되도록 지정합니다status.
75 <xsl:otherwise> 76 <xsl:value-of select="concat(oper-status, '/', admin-status)"/> 77 </xsl:otherwise>
78호선과 79호선이 닫히고 있습니다.
78 </xsl:choose> 79 </xsl:variable>
80~89호선은 으로 불리는 desc변수를 정의합니다. 명령어 <xsl:choose> 에는 사용 가능한 가장 구체적인 인터페이스 설명을 선택하여 변수가 채워집니다. 논리적 인터페이스 설명이 구성에 포함된 경우 변수를 채우는 desc 데 사용됩니다. 그렇지 않은 경우 물리적 인터페이스 설명이 사용됩니다. 물리적 인터페이스 설명이 구성에 포함되지 않으면 변수는 비어 있습니다. XSLT에서와 마찬가지로 TRUE로 평가되는 첫 번째 <xsl:when> 명령이 실행되고 나머지는 무시됩니다.
80 <xsl:variable name="desc"> 81 <xsl:choose> 82 <xsl:when test="description"> 83 <xsl:value-of select="description"/> 84 </xsl:when> 85 <xsl:when test="../description"> 86 <xsl:value-of select="../description"/> 87 </xsl:when> 88 </xsl:choose> 89 </xsl:variable>
나머지 스크립트는 운영 모드 출력이 표시되는 방식을 지정합니다.
라인 90 및 91은 논리적 인터페이스 이름이 출력에서 먼저 표시되도록 지정합니다.
90 <logical-interface> 91 <name><xsl:value-of select="name"/></name>
92-94줄은 변수에 desc 비제로 개수가 있는지 여부를 테스트합니다. 문자 수가 0을 초과하면 필드의 표준 위치에 인터페이스 설명이 admin-status 표시됩니다. (표준 출력 admin-status 에서 필드는 두 번째 줄에 표시됩니다.)
92 <xsl:if test="string-length($desc)"> 93 <admin-status><xsl:value-of select="$desc"/></admin-status> 94 </xsl:if>
라인 95는 변수에 정의된 status 인터페이스 상태가 다음에 표시되도록 지정합니다.
95 <admin-status><xsl:value-of select="$status"/></admin-status>
96-103행은 스크립트를 실행할 때 인수를 protocol 포함하는 경우 구성된 프로토콜을 포함한 인터페이스만 표시됩니다. 인수를 protocol 포함하지 않으면 모든 인터페이스가 표시됩니다.
96 <xsl:choose> 97 <xsl:when test="$protocol"> 98 <xsl:copy-of select="address-family[address-family-name = $protocol]"/> 99 </xsl:when> 100 <xsl:otherwise> 101 <xsl:copy-of select="address-family"/> 102 </xsl:otherwise> 103 </xsl:choose>
104-106선이 태그를 닫습니다.
104 </logical-interface> 105 </xsl:template> 106 </xsl:stylesheet>
SLAX 구문
스크립트의 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";
var $arguments = {
<argument> {
<name> "interface";
<description> "Name of interface to display";
}
<argument> {
<name> "protocol";
<description> "Protocol to display (inet, inet6)";
}
}
param $interface;
param $protocol;
match / {
<op-script-results> {
var $rpc = {
<get-interface-information> {
<terse>;
if ($interface) {
<interface-name> $interface;
}
}
}
var $out = jcs:invoke($rpc);
<interface-information junos:style="terse"> {
if ($protocol='inet' or $protocol='inet6' or $protocol='mpls' or
$protocol='tnp') {
for-each ($out/physical-interface/
logical-interface[address-family/address-family-name = $protocol]) {
call intf();
}
} else if ($protocol) {
<xnm:error> {
<message> {
expr "invalid protocol: ";
expr $protocol;
}
}
} else {
for-each ($out/physical-interface/logical-interface) {
call intf();
}
}
}
}
}
intf () {
var $status = {
if (admin-status='up' and oper-status='up') {
} else if (admin-status='down') {
expr "offline";
} else if (oper-status='down' and ../admin-status='down') {
expr "p-offline";
} else if (oper-status='down' and ../oper-status='down') {
expr "p-down";
} else if (oper-status='down') {
expr "down";
} else {
expr oper-status _ '/' _ admin-status;
}
}
var $desc = {
if (description) {
expr description;
} else if (../description) {
expr ../description;
}
}
<logical-interface> {
<name> name;
if (string-length($desc)) {
<admin-status> $desc;
}
<admin-status> $status;
if ($protocol) {
copy-of address-family[address-family-name = $protocol];
} else {
copy-of address-family;
}
}
}
구성
절차
단계별 절차
스크립트를 다운로드, 활성화, 테스트하려면 다음을 수행합니다.
XSLT 또는 SLAX 스크립트를 텍스트 파일에 복사하여 파일 인터페이스.xsl 또는 interface.slax의 이름을 적절하게 지정하고 장비의 /var/db/scripts/op/ directory에 복사합니다.
구성 모드에서는 계층 수준 및 interface.xsl 또는 interface.slax의 명령문을
[edit system scripts op]적절하게 포함합니다file.[edit system scripts op] user@host# set file interface.(slax | xsl)
commit and-quit명령을 실행하여 구성을 커밋하고 운영 모드로 돌아갑니다.[edit] user@host# commit and-quit
운영 모드 명령을 실행하여 운영 스크립트를
op interface실행합니다.
확인
커밋 스크립트 출력 검증
목적
스크립트가 예상대로 작동하는지 확인합니다.
작업
show interfaces terse op interface 및 운영 명령을 발행하고 출력을 비교합니다. show interfaces terse 명령은 표준 출력을 표시합니다. op interface 명령은 사용자 정의된 출력을 표시합니다.
user@host> show interfaces terse
Interface Admin Link Proto Local Remote
dsc up up
fxp0 up up
fxp0.0 up up inet 192.168.71.246/21
fxp1 up up
fxp1.0 up up inet 10.0.0.4/8
inet6 fe80::200:ff:fe00:4/64
fc00::10:0:0:4/64
tnp 4
gre up up
ipip up up
lo0 up up
lo0.0 up up inet 127.0.0.1 --> 0/0
lo0.16385 up up inet
inet6 fe80::2a0:a5ff:fe12:2f04
lsi up up
mtun up up
pimd up up
pime up up
tap up up
user@host> op interface
Interface Admin Link Proto Local Remote
fxp0.0 This is the Ethernet Management interface.
inet 192.168.71.246/21
fxp1.0 inet 10.0.0.4/8
inet6 fe80::200:ff:fe00:4/64
fc00::10:0:0:4/64
tnp 4
lo0.0 inet 127.0.0.1 --> 0/0
lo0.16385 inet
inet6 fe80::2a0:a5ff:fe12:2f04-->
op interface 서로 다른 계층 수준에 대해 운영 명령을 발행하고 출력을 검토합니다. 예를 들어:
user@host> op interface interface fxp0
Interface Admin Link Proto Local Remote
fxp0.0 This is the Ethernet Management interface.
inet 192.168.71.246/21
user@host> op interface protocol inet
Interface Admin Link Proto Local Remote
fxp0.0 This is the Ethernet Management interface.
inet 192.168.71.246/21
fxp1.0 inet 10.0.0.4/8
lo0.0 inet 127.0.0.1 --> 0/0
lo0.16385 inet