Junos 디바이스용 맞춤형 YANG RPC에서 다양한 수준의 출력 정의
맞춤형 YANG RPC에서 다양한 수준의 출력 정의
YANG를 사용하여 Junos 디바이스에 대한 사용자 지정 RPC를 정의할 수 있습니다. RPC 출력은 RPC 입력에 따라 서로 다른 데이터와 CLI 형식을 내도록 사용자 정의할 수 있습니다. 이를 통해 동일한 RPC에 대해 서로 다른 스타일 또는 출력 수준을 생성할 수 있습니다.
RPC를 호출할 때 입력 인수에 적합한 값을 포함하여 원하는 스타일을 요청할 수 있습니다. 작업 스크립트는 이 인수를 처리하고 요청된 스타일에 대한 XML 출력을 내보내야 합니다. 그런 다음 Junos OS는 YANG 모듈에서 해당 스타일에 대해 정의된 해당 CLI 출력으로 XML을 변환합니다. 이 항목에 제시된 RPC 템플릿은 다음과 같은 두 가지 스타일을 brief
detail
만듭니다.
RPC 출력에 대해 서로 다른 스타일을 만들려면 다음을 수행합니다.
다음 코드는 RPC 및 모듈의 일반적인 구조를 개략적으로 설명합니다. CLI에서 RPC를 호출하고 입력 인수 level
를 포함하고 Junos OS 중 하나를 brief
detail
지정하면 해당 스타일에 정의된 출력이 렌더링됩니다.
module module-name { namespace "http://yang.juniper.net/yang/1.1/jrpc"; prefix jrpc; import junos-extension { prefix junos; } import junos-extension-odl { prefix junos-odl; } organization "Juniper Networks, Inc."; description "Junos OS YANG module for custom RPCs"; rpc rpc-name { description "RPC description"; junos:command "cli-command" { junos:action-execute { junos:script "action-script-filename"; } } input { leaf level { type enumeration { enum brief { description "Display brief output"; } enum detail { description "Display detailed output"; } } } } output { container output-container { // leaf definitions junos-odl:style brief { junos-odl:format output-container-format-brief { // formatting for brief output } } junos-odl:style detail { junos-odl:format output-container-format-detail { // formatting for detailed output } } } } } }
CLI에서 RPC를 실행하려면 명령문으로 junos:command
정의된 명령을 발행하고 이 예 level
에서와 같은 적절한 명령줄 인수를 포함시켜 스타일을 지정합니다. 해당 작업 스크립트는 입력 인수를 처리하고 요청된 스타일에 대한 출력을 내보낸다.
user@host> cli-command level brief
예: 다양한 수준의 출력 정의
이 예에서는 호스트 연결이 가능한지 확인하고 사용자 입력에 따라 다른 수준의 출력을 인쇄하는 간단한 사용자 지정 YANG RPC 및 작업 스크립트를 제공합니다.
요구 사항
이 예에서는 다음과 같은 하드웨어 및 소프트웨어 구성 요소를 사용합니다.
-
Junos OS 릴리스 17.3R1 이상에서 실행되는 디바이스로 사용자 지정 YANG 데이터 모델 로드를 지원합니다.
RPC 및 작업 스크립트 개요
이 예제의 YANG 모듈은 지정된 호스트에 대한 사용자 지정 RPC를 정의하고 사용자 입력에 따라 서로 다른 수준의 출력을 사용하여 결과를 반환합니다. YANG 모듈 rpc-style-test
은 rpc-style-test.yang 파일에 저장됩니다. 이 모듈은 장비에서 사용자 지정 RPC를 실행하고 CLI 출력을 사용자 지정하는 데 필요한 확장을 제공하는 Junos OS 확장 모듈을 가져옵니다.
모듈은 RPC를 get-host-status
정의합니다. <get-host-status>
요청 태그는 장비에서 RPC를 원격으로 실행하는 데 사용됩니다. RPC 정의에서 명령 junos:command
문은 CLI에서 RPC를 실행하는 데 사용되는 명령을 정의합니다. 이 경우 입니다 show host-status
.
rpc get-host-status { description "RPC example to retrieve host status"; junos:command "show host-status" { junos:action-execute { junos:script "rpc-style-test.py"; } } ...
및 junos:script
명령문은 junos:action-execute
RPC를 실행할 때 호출되는 작업 스크립트를 정의합니다. 이 예에서는 rpc-style-test.py 라는 Python 작업 스크립트를 사용하여 RPC에서 요구하는 정보를 검색합니다. 스크립트는 RPC output
의 명령문에 정의된 각 출력 수준에 대한 XML 출력 요소를 반환합니다.
Junos OS 릴리스 17.3 action-execute
부터 이 명령문은 에 대한 하위 상태입니다 command
. 이전 릴리스 action-execute
에서와 command
명령문은 동일한 수준으로 배치되며 command
명령문은 옵션입니다.
RPC에는 두 개의 입력 매개 변수 hostip
와 level
. 매개 변수는 hostip
도달 가능성을 확인하는 호스트입니다. 이 매개 변수는 level
RPC의 출력에 대한 스타일을 선택합니다. RPC를 실행할 때 대상 호스트의 IP 주소와 레벨 brief
또는 detail
를 포함합니다. 작업 스크립트는 기본값을 level
다음과 같이 'brief'
정의하므로 이 인수를 생략하면 RPC는 간단한 스타일에 해당하는 출력을 인쇄합니다.
input { leaf hostip { description "Host IP address"; type string; } leaf level { type enumeration { enum brief { description "Display brief output"; } enum detail { description "Display detailed output"; } } } }
또한 RPC는 해당 작업 스크립트에 의해 방출되어야 하는 출력 노드를 정의합니다. 루트 노드는 <host-status-information>
각 출력 수준에 대해 지정된 자식 출력 노드와 함께 사용자 입력에 따라 요소 또는 <detail>
요소를 포함하는 <brief>
요소입니다. 출력 수준은 물론 하위 요소도 포함 <hostip>
<status>
하지만 <detail>
요소에는 하위 요소도 포함됩니다<date>
. 명령문은 junos-odl:format
CLI에 표시되는 출력의 형식을 정의합니다. 이 노드는 출력 XML 트리에서 방출되지 않습니다.
output { container host-status-information { ... junos-odl:style brief { junos-odl:format host-status-information-format-brief { ... } } junos-odl:style detail { junos-odl:format host-status-information-format-detail { ... } } } }
작업 스크립트는 호스트를 핑하여 도달 가능한지 확인하고 결과를 기반으로 상태를 설정합니다. 그런 다음 스크립트는 지정된 인수를 기반으로 RPC 출력을 위해 XML을 구성하고 인쇄합니다 level
. XML 트리는 RPC에 정의된 계층과 정확히 일치해야 합니다.
RPC와 작업 스크립트 파일이 포함된 모듈이 새로운 YANG 패키지 rpc-style-test
의 일부로 장비에 추가됩니다.
YANG 모듈 및 작업 스크립트
YANG 모듈
YANG 모듈인 rpc-style-test.yang은 RPC, CLI에서 RPC를 실행하는 데 사용되는 명령 및 RPC가 실행될 때 호출할 작업 스크립트의 이름을 정의합니다. 파일의 기본 이름은 모듈 이름과 일치해야 합니다.
/* * Copyright (c) 2014 Juniper Networks, Inc. * All rights reserved. */ module rpc-style-test { namespace "http://yang.juniper.net/yang/1.1/jrpc"; prefix jrpc; import junos-extension-odl { prefix junos-odl; } import junos-extension { prefix junos; } organization "Juniper Networks, Inc."; description "Junos OS YANG module for RPC example"; rpc get-host-status { description "RPC example to retrieve host status"; junos:command "show host-status" { junos:action-execute { junos:script "rpc-style-test.py"; } } input { leaf hostip { description "Host IP address"; type string; } leaf level { type enumeration { enum brief { description "Display brief output"; } enum detail { description "Display detailed output"; } } } } output { container host-status-information { leaf hostip { type string; description "Host IP"; } leaf status { type string; description "Operational status"; } leaf date { type string; description "Date information"; } junos-odl:style brief { junos-odl:format host-status-information-format-brief { junos-odl:header "Brief output\n"; junos-odl:picture "@<<<<<<<<<<<< @"; junos-odl:space; junos-odl:line { junos-odl:field "hostip"; junos-odl:field "status"; } } } junos-odl:style detail { junos-odl:format host-status-information-format-detail { junos-odl:header "Detail output\n"; junos-odl:picture "@<<<<<<<<<<<< @<<<<<<<<<<<< @"; junos-odl:space; junos-odl:line { junos-odl:field "hostip"; junos-odl:field "status"; junos-odl:field "date"; } } } } } } }
작업 스크립트
해당 작업 스크립트는 rpc-style-test.py. 작업 스크립트는 사용자가 제공한 인수의 level
값을 기반으로 다양한 수준의 출력을 인쇄합니다. 스크립트는 인수에 'brief'
대한 level
기본값을 정의하므로 사용자가 인수를 생략하면 스크립트가 간략한 출력 스타일을 반환합니다. 이 예에서는 서로 다른 릴리스에 대한 스크립트의 명령줄 인수를 적절하게 처리하는 작업 스크립트의 두 버전을 제공합니다.
작업 스크립트(Junos OS 릴리스 21.2R1 이상)
#!/usr/bin/python3 # Junos OS Release 21.2R1 and later import os import argparse parser = argparse.ArgumentParser(description='This is a demo script.') parser.add_argument('--hostip', required=True) parser.add_argument('--level', required=False, default='brief') parser.add_argument('--rpc_name', required=True) args = parser.parse_args() f = os.popen('date') now = f.read() # Ping target host and set the status response = os.system('ping -c 1 ' + args.hostip + ' > /dev/null') if response == 0: pingstatus = "Host is Active" else: pingstatus = "Host is Inactive" # Print RPC XML for the given style print ("<host-status-information>") print ("<{}>".format(args.level)) print ("<hostip>{}</hostip>".format(args.hostip)) print ("<status>{}</status>".format(pingstatus)) if args.level == "detail": print ("<date>{}</date>".format(now)) print ("</{}>".format(args.level)) print ("</host-status-information>")
작업 스크립트(Junos OS 릴리스 21.1 이전 버전)
#!/usr/bin/python # Junos OS Release 21.1 and earlier import sys import os args = {'hostip': None, 'level': 'brief'} # Retrieve user input and store the values in the args dictionary for arg in args.keys(): if arg in sys.argv: index = sys.argv.index(arg) args[arg] = sys.argv[index+1] f = os.popen('date') now = f.read() # Ping target host and set the status if args['hostip'] is not None: response = os.system('ping -c 1 ' + args['hostip'] + ' > /dev/null') if response == 0: pingstatus = "Host is Active" else: pingstatus = "Host is Inactive" else: pingstatus = "Invalid host" # Print RPC XML for the given style print ("<host-status-information>") print ("<{}>".format(args['level'])) print ("<hostip>{}</hostip>".format(args['hostip'])) print ("<status>{}</status>".format(pingstatus)) if args['level'] == "detail": print ("<date>{}</date>".format(now)) print ("</{}>".format(args['level'])) print ("</host-status-information>")
구성
Python 스크립트 실행 활성화
서명되지 않은 Python 스크립트를 실행할 수 있도록 하려면 다음을 수행합니다.
-
Junos OS 릴리스에
language python
적합한 명령문 또는language python3
명령문을 구성합니다.[edit] user@host# set system scripts language (python | python3)
참고:Junos OS 릴리스 20.2R1 및 Junos OS Evolved Release 22.3R1부터 이 디바이스는 Python 3을 사용하여 YANG 작업 및 변환 스크립트를 실행합니다. 이전 릴리스에서 Junos OS는 Python 2.7만 사용하여 이러한 스크립트를 실행하며, Junos OS Evolved는 기본적으로 Python 2.7을 사용하여 스크립트를 실행합니다.
-
구성을 커밋합니다.
[edit] user@host# commit and-quit
디바이스에 RPC 로드
디바이스의 Junos 스키마에 RPC 및 작업 스크립트를 추가하려면 다음을 수행합니다.
-
YANG 모듈 및 작업 스크립트를 Junos 장비에 다운로드하십시오.
-
Python 작업 스크립트가 다음 요구 사항을 충족하는지 확인합니다.
-
파일 소유자는 Junos OS
super-user
로그인 클래스의 루트 또는 사용자입니다. -
파일 소유자만이 파일에 대한 쓰기 권한을 가지고 있습니다.
-
Script에는 Junos Devices의 YANG RPC용 만들기 작업 스크립트에 설명된 대로 적절한 해석기 지시어 라인이 포함되어 있습니다.
-
-
(선택사항) YANG 모듈 및 작업 스크립트의 구문을 검증합니다.
user@host> request system yang validate module /var/tmp/rpc-style-test.yang action-script /var/tmp/rpc-style-test.py YANG modules validation : START YANG modules validation : SUCCESS Scripts syntax validation : START Scripts syntax validation : SUCCESS
-
YANG 모듈 및 작업 스크립트를 새 YANG 패키지에 추가합니다.
user@host> request system yang add package rpc-style-test module /var/tmp/rpc-style-test.yang action-script /var/tmp/rpc-style-test.py YANG modules validation : START YANG modules validation : SUCCESS Scripts syntax validation : START Scripts syntax validation : SUCCESS TLV generation: START TLV generation: SUCCESS Building schema and reloading /config/juniper.conf.gz ... Restarting mgd ...
-
시스템에서 Junos OS CLI를 다시 시작하라는 메시지가 표시되면 , 또는 유형과 키를 누릅니
Enter
yes
다Enter
.yesWARNING: cli has been replaced by an updated version: ... Restart cli using the new version ? [yes,no] (yes) yes Restarting cli ...
RPC 검증
목적
RPC가 예상대로 작동하는지 확인합니다.
작업
운영 모드에서 RPC 정의의 명령어로 junos:command
정의된 명령을 실행하여 CLI에서 RPC를 실행합니다. 입력 인수를 hostip
포함하며 각 다른 출력 수준에 대한 인수를 포함합니다 level
.
user@host> show host-status hostip 198.51.100.1 level brief Brief output 198.51.100.1 Host is Active
명령에 부록을 추가하여 해당 XML을 | display xml
볼 수 있습니다.
user@host> show host-status hostip 198.51.100.1 level brief | display xml <rpc-reply xmlns:junos="http://xml.juniper.net/junos/18.3R1/junos"> <host-status-information> <brief> <hostip> 198.51.100.1 </hostip> <status> Host is Active </status> </brief> </host-status-information> <cli> <banner></banner> </cli> </rpc-reply>
마찬가지로, 상세 출력의 경우:
user@host> show host-status hostip 198.51.100.10 level detail Detail output 198.51.100.10 Host is Inactive Fri Feb 8 11:55:54 PST 2019
user@host> show host-status hostip 198.51.100.10 level detail | display xml <rpc-reply xmlns:junos="http://xml.juniper.net/junos/18.3R1/junos"> <host-status-information> <detail> <hostip> 198.51.100.10 </hostip> <status> Host is Inactive </status> <date> Fri Feb 8 16:03:35 PST 2019 </date> </detail> </host-status-information> <cli> <banner></banner> </cli> </rpc-reply>
의미
RPC를 실행하면 디바이스가 작업 스크립트를 호출합니다. 작업 스크립트는 RPC output
명령문에 정의된 지정된 출력 수준에 대한 XML 계층을 인쇄합니다. RPC가 CLI에서 실행되면 디바이스는 RPC에 정의된 CLI 형식을 사용하여 XML 출력을 표시된 CLI 출력으로 변환합니다.