이 페이지 내용
예: SLAX 및 XSLT Op 스크립트를 사용하여 구성 변경
이 예제에서는 SLAX op 스크립트를 사용하여 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 op 스크립트는 가져오기 파일에 있는 junos.xsl 템플릿을 사용하여 jcs:load-configuration
Junos OS 구성을 구조화하여 변경할 수 있습니다. 이 예는 Junos OS를 실행하는 디바이스에서 인터페이스를 비활성화하기 위해 템플릿을 사용하는 jcs:load-configuration
SLAX op 스크립트를 생성합니다. 템플릿에 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
반환된 노드 집합을 구문 분석하는 코드를 스크립트에 포함해야 합니다. 이를 통해 커밋 성공 여부를 보다 쉽게 확인할 수 있습니다. 경고 또는 오류 메시지가 없으면 여러 가지 방법으로 커밋의 성공 여부를 확인할 수 있습니다.
커밋 로그를 확인하여 커밋이 성공했는지 확인합니다. 매개 변수에
log
commit-options
옵션을 포함시킨 경우 커밋 정보와 함께 커밋 로그에 메시지가 표시되어야 합니다.user@host> show system commit 0 2010-09-22 17:08:17 PDT by user via junoscript disabling interface so-0/0/0
syslog 메시지 파일을 확인하여 커밋 작업이 기록되었는지 확인합니다. 이 경우 비활성화된 인터페이스 so-0/0/0에 대한 메시지도 표시됩니다
SNMP_TRAP_LINK_DOWN
. 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