Junos PyEZ를 사용하여 구성 검색
요약 Junos 디바이스의 지정된 구성 데이터베이스에서 구성 데이터를 검색하는 Junos PyEZ 애플리케이션을 생성할 수 있습니다.
Junos PyEZ 애플리케이션은 Junos 디바이스에서 온디맨드 방식으로 원격 프로시저 호출(RPC)을 실행할 수 있습니다. 클래스의 인스턴스를 Device
만든 후 응용 프로그램은 RPC를 인스턴스의 속성 Device
으로 실행할 수 있습니다. Junos PyEZ 애플리케이션은 RPC를 get_config()
사용하여 네이티브 Junos OS 구성뿐만 아니라 디바이스에 추가된 표준(IETF, OpenConfig) 또는 사용자 지정 YANG 데이터 모델에 해당하는 구성 데이터 모두에 대한 전체 구성 또는 구성의 선택된 부분을 요청할 수 있습니다.
Junos PyEZ get_config
RPC는 Junos XML 프로토콜 <get-configuration>
작업을 호출합니다. 작업 및 해당 옵션에 대한 <get-configuration>
자세한 내용은 <get-configuration>을 참조하세요.
이 주제는 Junos PyEZ get_config()
RPC를 사용하여 구성을 검색하는 방법에 대해 설명합니다. 테이블 및 뷰를 사용하여 구성 데이터를 검색하는 방법에 대한 자세한 내용은 Junos PyEZ 구성 테이블 정의 및 Junos PyEZ 구성 테이블을 사용하여 구성 데이터 검색을 참조하세요.
전체 후보 구성 검색
Junos 디바이스에서 전체 후보 구성을 검색하려면 RPC를 get_config()
실행합니다. 기본 출력 형식은 XML입니다. 예를 들어:
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: data = dev.rpc.get_config() print (etree.tostring(data, encoding='unicode', pretty_print=True))
구성 데이터에 대한 원본 데이터베이스 지정
Junos PyEZ 애플리케이션이 RPC를 get_config()
사용하여 Junos 디바이스에서 구성 정보를 검색하는 경우, 기본적으로 서버는 후보 구성 데이터베이스에서 데이터를 반환합니다. Junos PyEZ 애플리케이션은 커밋된 구성 데이터베이스 또는 임시 구성 데이터베이스에서 구성 데이터를 검색할 수도 있습니다.
후보 구성 데이터베이스
후보 구성 데이터베이스에서 데이터를 검색하려면 RPC를 get_config()
실행하고 필요에 따라 추가 인수를 포함합니다.
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: data = dev.rpc.get_config() print (etree.tostring(data, encoding='unicode', pretty_print=True))
커밋된 구성 데이터베이스
커밋된 구성 데이터베이스에서 데이터를 검색하려면, RPC 호출에 get_config()
인수 를 'database':'committed'
포함합니다options
.
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: data = dev.rpc.get_config(options={'database' : 'committed'}) print (etree.tostring(data, encoding='unicode', pretty_print=True))
사용 후 삭제 구성 데이터베이스
Junos PyEZ는 이 데이터베이스를 지원하는 디바이스의 임시 구성 데이터베이스에 대한 작업을 지원합니다. 공유 구성 데이터베이스에서 구성 데이터를 검색할 때 기본적으로 결과에는 임시 구성 데이터베이스의 데이터가 포함되지 않습니다.
임시 데이터베이스는 Junos 디바이스에서 구성 업데이트를 수행하기 위한 빠른 프로그래밍 인터페이스를 제공하는 대체 구성 데이터베이스입니다. 임시 구성 데이터베이스는 고급 기능으로, 잘못 사용하면 디바이스 작동에 심각한 부정적인 영향을 미칠 수 있습니다. 자세한 내용은 사용 후 삭제 구성 데이터베이스 이해를 참조하십시오.
임시 구성 데이터베이스의 기본 인스턴스에서 데이터를 검색하려면 먼저 기본 임시 인스턴스를 열고 데이터를 요청합니다. 기본 인스턴스를 열려면 컨텍스트 관리자를 사용하여 인스턴스를 만들고 Config
인수를 mode='ephemeral'
포함합니다. 예를 들어:
from jnpr.junos import Device from jnpr.junos.utils.config import Config from jnpr.junos.exception import ConnectError from lxml import etree dev = Device(host='router1.example.net') try: dev.open() with Config(dev, mode='ephemeral') as cu: data = dev.rpc.get_config(options={'format':'text'}) print(etree.tostring(data, encoding='unicode')) dev.close() except ConnectError as err: print ("Cannot connect to device: {0}".format(err)) except Exception as err: print (err)
임시 구성 데이터베이스의 특정 인스턴스에서 데이터를 검색하려면 먼저 임시 인스턴스를 연 다음 데이터를 요청합니다. 임시 구성 데이터베이스의 사용자 정의 인스턴스를 열려면 컨텍스트 관리자를 사용하여 인스턴스를 작성 Config
하고, 인수를 mode='ephemeral'
포함하고, 인수를 ephemeral_instance
임시 인스턴스의 이름으로 설정합니다.
from jnpr.junos import Device from jnpr.junos.utils.config import Config from jnpr.junos.exception import ConnectError from lxml import etree dev = Device(host='router1.example.net') try: dev.open() with Config(dev, mode='ephemeral', ephemeral_instance='eph1') as cu: data = dev.rpc.get_config(options={'format':'text'}) print(etree.tostring(data, encoding='unicode')) dev.close() except ConnectError as err: print ("Cannot connect to device: {0}".format(err)) except Exception as err: print (err)
반환할 구성 데이터의 범위 지정
Junos PyEZ 애플리케이션은 전체 Junos OS 구성을 검색할 뿐만 아니라 인수와 함께 filter_xml
RPC를 get_config()
호출하여 구성의 특정 부분을 검색할 수 있습니다. 이 filter_xml
매개 변수는 반환할 구성 문을 선택하는 하위 트리 필터가 포함된 문자열을 사용합니다. 하위 트리 필터는 선택 기준과 일치하는 구성 데이터를 반환합니다.
여러 계층을 요청하려면 문자열에 filter_xml
루트 요소가 포함되어야 <configuration>
합니다. 그렇지 않으면, 의 filter_xml
값은 루트 <configuration>
요소 바로 아래에서 시작하여 표시할 계층까지 구성 계층의 모든 수준을 나타내야 합니다. 하위 트리를 선택하려면 해당 계층 수준에 대해 빈 태그를 포함합니다. 특정 객체를 반환하려면 일치시킬 요소와 값을 정의하는 컨텐츠 일치 노드를 포함합니다.
다음 Junos PyEZ 애플리케이션은 후보 구성의 및 [edit protocols]
계층 수준에서 구성을 검색하고 인쇄합니다.[edit interfaces]
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: filter = '<configuration><interfaces/><protocols/></configuration>' data = dev.rpc.get_config(filter_xml=filter) print (etree.tostring(data, encoding='unicode', pretty_print=True))
다음 예제에서는 인수에 대해 다르지만 동일한 값을 사용하여 계층 수준에서 구성을 [edit system services]
검색하고 인쇄합니다 filter_xml
.
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: data = dev.rpc.get_config(filter_xml='<system><services/></system>') print (etree.tostring(data, encoding='unicode', pretty_print=True)) data = dev.rpc.get_config(filter_xml='system/services') print (etree.tostring(data, encoding='unicode', pretty_print=True)) filter = etree.XML('<system><services/></system>') data = dev.rpc.get_config(filter_xml=filter) print (etree.tostring(data, encoding='unicode', pretty_print=True))
다음 예제에서는 상속 후 후보 구성의 계층 아래에 있는 <interfaces>
각 <interface>
요소에 대한 요소를 검색합니다<name>
.
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: filter = '<interfaces><interface><name/></interface></interfaces>' data = dev.rpc.get_config(filter_xml=filter, options={'inherit':'inherit'}) print (etree.tostring(data, encoding='unicode', pretty_print=True))
user@server:~$ python3 junos-pyez-get-interface-names.py <configuration changed-seconds="1544032801" changed-localtime="2018-12-05 10:00:01 PST"> <interfaces> <interface> <name>ge-1/0/0</name> </interface> <interface> <name>ge-1/0/1</name> </interface> <interface> <name>lo0</name> </interface> <interface> <name>fxp0</name> </interface> </interfaces> </configuration>
다음 예제에서는 ge-1/0/1 인터페이스의 하위 트리를 검색합니다.
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: filter = '<interfaces><interface><name>ge-1/0/1</name></interface></interfaces>' data = dev.rpc.get_config(filter_xml=filter, options={'inherit':'inherit'}) print (etree.tostring(data, encoding='unicode', pretty_print=True))
user@server:~$ python3 junos-pyez-get-single-interface.py <configuration changed-seconds="1544032801" changed-localtime="2018-12-05 10:00:01 PST"> <interfaces> <interface> <name>ge-1/0/1</name> <description>customerA</description> <disable/> <unit> <name>0</name> <family> <inet> <address> <name>198.51.100.1/24</name> </address> </inet> </family> </unit> </interface> </interfaces> </configuration>
반환할 구성 데이터의 형식 지정
Junos PyEZ get_config()
RPC는 Junos XML 프로토콜 <get-configuration>
작업을 호출하며, Junos OS 구성 데이터를 Junos XML 요소, CLI 구성 명령문, Junos OS set
명령 또는 JSON(JavaScript Object Notation)으로 반환할 수 있습니다. 기본적으로 RPC는 get_config()
구성 데이터를 XML로 반환합니다.
구성 데이터를 반환할 형식을 지정하기 위해 Junos PyEZ 애플리케이션은 인수 목록에 와 'format':'format'
사전을 get_config()
포함합니다options
. CLI 구성 문, Junos OS set
명령 또는 JSON 형식을 요청하려면 값을 text
각각 , set
또는 json
로 설정합니다format
.
NETCONF 및 Junos XML 프로토콜 세션에서와 같이 Junos PyEZ는 해당 형식에 적합한 XML 요소 내에 포함된 예상 형식으로 구성 데이터를 반환합니다. RPC 응답은 구성 데이터를 XML, 텍스트 또는 set
명령 형식 <configuration>
으로, <configuration-text>
, 및 <configuration-set>
요소에 각각 포함합니다.
from jnpr.junos import Device from lxml import etree from pprint import pprint with Device(host='router1.example.net') as dev: # XML format (default) data = dev.rpc.get_config() print (etree.tostring(data, encoding='unicode', pretty_print=True)) # Text format data = dev.rpc.get_config(options={'format':'text'}) print (etree.tostring(data, encoding='unicode', pretty_print=True)) # Junos OS set format data = dev.rpc.get_config(options={'format':'set'}) print (etree.tostring(data, encoding='unicode', pretty_print=True)) # JSON format data = dev.rpc.get_config(options={'format':'json'}) pprint (data)
Python 버전 및 출력 형식에 따라 사람이 읽을 수 있는 출력을 더 많이 표시하도록 print 문을 수정해야 할 수 있습니다.
표준 또는 사용자 지정 YANG 데이터 모델에 대한 구성 데이터 검색
표준화된 YANG 모듈 또는 사용자 지정 YANG 모듈을 Junos 디바이스에 로드하여 Junos OS에서 기본적으로 지원되지 않지만 변환을 통해 지원할 수 있는 데이터 모델을 추가할 수 있습니다. 후보 구성에서 해당 모델에 대해 정의된 구문을 사용하여 네이티브가 아닌 데이터 모델을 구성합니다. 구성을 커밋하면 데이터 모델의 변환 스크립트가 해당 데이터를 변환하고 해당 Junos OS 구성을 체크아웃 구성의 일시적인 변경으로 커밋합니다.
후보 및 활성 구성에는 해당 모델에서 정의한 구문의 비네이티브 YANG 데이터 모델에 대한 구성 데이터가 포함됩니다. Junos PyEZ 애플리케이션은 RPC에 적절한 인수를 포함하여 기본 Junos OS 구성을 검색하는 것 외에도 표준 및 사용자 지정 YANG 데이터 모델에 대한 구성 데이터를 검색할 수 있습니다 get_config()
. 기본적으로, 네이티브가 아닌 구성 데이터는 RPC 응답에 get_config()
포함되지 않습니다.
Junos OS 구성을 검색하는 것 외에도 네이티브가 아닌 YANG 데이터 모델로 정의된 구성 데이터를 검색하려면 인수와 함께 model
RPC를 get_config()
실행하고 적절한 경우 인수를 namespace
포함합니다. 인수는 model
다음 값 중 하나를 사용합니다.
custom
- 사용자 지정 YANG 데이터 모델에 의해 정의된 구성 데이터를 검색합니다. 사용자 지정 YANG 데이터 모델에 대한 데이터를 검색할 때 인수를namespace
포함해야 합니다.ietf
- IETF YANG 데이터 모델에 의해 정의된 구성 데이터를 검색합니다.openconfig
- OpenConfig YANG 데이터 모델에 의해 정의된 구성 데이터를 검색합니다.True
- 전체 Junos OS 구성 및 모든 YANG 데이터 모델의 데이터를 포함한 모든 구성 데이터를 검색합니다.
인수에 ietf
model
또는 openconfig
값을 지정하면 Junos PyEZ는 자동으로 적절한 네임스페이스를 사용합니다. 를 사용하여 model='custom'
사용자 지정 YANG 데이터 모델에 대한 데이터를 검색하는 경우 해당 네임스페이스와 namespace
함께 인수도 포함해야 합니다.
, 또는 openconfig
값이 ietf
custom
있는 인수를 포함하고 model
특정 XML 서브트리를 filter_xml
반환하는 인수도 포함하는 경우, Junos OS는 네이티브가 아닌 데이터 모델에서 일치하는 계층만 반환합니다. Junos OS 구성에 동일한 이름의 계층(예: "interfaces")이 포함된 경우에는 회신에 포함되지 않습니다. 이 filter_xml
옵션은 을 사용할 model=True
때 지원되지 않습니다.
다음 예제에서 RPC는 get_config()
디바이스의 후보 구성에서 OpenConfig bgp
구성 계층을 검색합니다. 인수를 filter_xml
생략하면 RPC는 전체 Junos OS 및 OpenConfig 후보 구성을 반환합니다.
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: data = dev.rpc.get_config(filter_xml='bgp', model='openconfig') print (etree.tostring(data, encoding='unicode', pretty_print=True))
다음 RPC는 interfaces
IETF YANG 데이터 모델의 후보 구성에서 구성 계층을 검색합니다.
data = dev.rpc.get_config(filter_xml='interfaces', model='ietf') print (etree.tostring(data, encoding='unicode', pretty_print=True))
다음 RPC는 지정된 네임스페이스를 l2vpn
가진 사용자 지정 YANG 데이터 모델의 후보 구성에서 구성 계층을 검색합니다.
data = dev.rpc.get_config(filter_xml='l2vpn', model='custom', namespace='http://yang.juniper.net/customyang/demo/l2vpn') print (etree.tostring(data, encoding='unicode', pretty_print=True))
다음 RPC는 전체 Junos OS 후보 구성과 디바이스에 추가된 다른 YANG 데이터 모델의 구성 데이터를 검색합니다.
data = dev.rpc.get_config(model=True) print (etree.tostring(data, encoding='unicode', pretty_print=True))
추가 RPC 옵션 지정
Junos PyEZ get_config()
RPC를 사용하여 구성을 검색하면 Junos XML 프로토콜 <get-configuration>
작업이 호출됩니다. RPC는 인수를 options
지원하므로 작업에서 지원하는 모든 특성의 키/값 쌍 사전을 <get-configuration>
포함할 수 있습니다. Junos XML 프로토콜 <get-configuration>
작업에서 지원하는 전체 속성 목록은 <get-configuration>을 참조하십시오.
예를 들어 RPC는 get_config()
, , <apply-groups-except>
및 <interface-range>
태그가 구성 출력에서 별도의 요소인 사전 상속 구성<groups>
<apply-groups>
에서 데이터를 검색합니다. 사용자 정의 그룹 및 범위에서 상속된 명령문을 상속 명령문의 하위 항목으로 표시하는 사후 상속 구성에서 데이터를 검색하려면, 와 함께 'inherit':'inherit'
인수를 options
포함할 수 있습니다.
예를 들어 다음 코드는 상속 후 후보 구성에서 계층 수준의 구성을 [edit system services]
검색합니다. 이 경우, 구성에 계층 수준에서 구성된 [edit groups global system services]
문도 포함되어 있는 경우, 해당 문은 상속 후 구성의 계층 아래에 [edit system services]
상속되고 검색된 구성 데이터에 반환됩니다.
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: data = dev.rpc.get_config(filter_xml='system/services', options={'inherit':'inherit'}) print (etree.tostring(data, encoding='unicode', pretty_print=True))
구성 데이터의 네임스페이스를 처리하는 방법
Junos PyEZ get_config()
RPC는 기본적으로 반환된 구성 데이터에서 모든 네임스페이스를 제거합니다. Junos PyEZ 애플리케이션은 반환된 구성 데이터에서 네임스페이스를 유지할 수 있으므로 기존 구성을 빠르게 수정하려는 경우와 같이 데이터를 디바이스에 다시 로드할 수 있습니다.
구성 데이터에서 네임스페이스를 유지하려면 인수 목록에 인수를 remove_ns=False
get_config()
포함합니다. 예를 들어:
from jnpr.junos import Device from lxml import etree with Device(host='router1.example.net') as dev: data = dev.rpc.get_config(filter_xml='bgp', model='openconfig', remove_ns=False) print (etree.tostring(data, encoding='unicode', pretty_print=True))
다음과 같은 잘린 출력 <bgp>
에서 요소는 네임스페이스를 xmlns
정의하는 특성을 유지합니다.
<bgp xmlns="http://openconfig.net/yang/bgp"> <neighbors> <neighbor> <neighbor-address>198.51.100.1</neighbor-address> <config> <peer-group>OC</peer-group> <neighbor-address>198.51.100.1</neighbor-address> <enabled>true</enabled> <peer-as>64496</peer-as> </config> </neighbor> </neighbors> ...
인수를 get_config()
remove_ns=False
생략하면 네임스페이스가 출력에 포함되지 않습니다.
<bgp> <neighbors> <neighbor> <neighbor-address>198.51.100.1</neighbor-address> <config> <peer-group>OC</peer-group> <neighbor-address>198.51.100.1</neighbor-address> <enabled>true</enabled> <peer-as>64496</peer-as> </config> </neighbor> </neighbors> ...