Junos PyEZ를 사용하여 파일 전송
요약 Junos PyEZ를 사용하여 로컬 호스트와 Junos 디바이스 간의 복사본(SCP) 파일을 보호합니다.
Junos PyEZ는 Junos 디바이스에서 파일 관리 작업을 수행할 수 있는 유틸리티를 제공합니다. Junos PyEZ jnpr.junos.utils.scp.SCP
클래스를 사용하여 로컬 호스트와 Junos 디바이스 간의 복사본(SCP) 파일을 보호할 수 있습니다.
SCP
open()
및 close()
메서드는 장치와의 연결을 설정하고 종료합니다. 따라서 클라이언트 응용 프로그램이 파일 복사 작업만 수행하는 경우 및 close()
메서드에 Device
open()
대한 호출을 생략할 수 있습니다. 클래스의 인스턴스는 SCP
및 close()
메서드를 자동으로 호출하는 open()
컨텍스트 관리자로 사용할 수 있습니다. 예를 들어:
from jnpr.junos import Device from jnpr.junos.utils.scp import SCP dev = Device('router1.example.com') with SCP(dev) as scp: scp.put('local-file', remote_path='path') scp.get('remote-file', local_path='path')
SCP
매개 변수를 사용하여 전송 진행 상황을 추적할 수 있습니다 progress
. 기본적으로 진행 SCP
률 메시지를 인쇄하지 않습니다. 전송 완료 간격이 10% 이상인 기본 진행률 메시지를 인쇄하도록 설정합니다 progress=True
.
with SCP(dev, progress=True) as scp:
또는 진행률 메시지를 인쇄하는 사용자 지정 함수를 정의한 다음 매개 변수를 함수 이름과 동일하게 설정할 progress
수 있습니다. 함수 정의에는 장치 인스턴스와 진행률 메시지에 해당하는 두 개의 매개 변수가 포함되어야 합니다. 예를 들어:
def log(dev, report): print (dev.hostname + ': ' + report) def main(): ... with SCP(dev, progress=log) as scp:
다음 샘플 프로그램은 로컬 호스트의 scp-test1.txt 및 scp-test2.txt 파일을 대상 디바이스의 /var/tmp 디렉토리로 전송한 다음 대상 디바이스의 메시지 로그 파일을 로컬 호스트의 logs 디렉토리로 전송합니다. 파일 이름에 디바이스 호스트 이름이 추가되도록 메시지 로그의 이름이 바뀝니다. 이 예에서는 인증을 위해 로컬 호스트와 디바이스에 이미 구성된 SSH 키를 사용합니다.
비교를 위해 프로그램은 기본 진행 메시지와 사용자 지정 메시지를 모두 사용하며, 이는 라는 함수 log
에 정의되어 있습니다.
from jnpr.junos import Device from jnpr.junos.utils.scp import SCP def log(dev, report): print (dev.hostname + ': ' + report) def main(): dev = Device('router1.example.com') msgfile = 'logs/'+dev.hostname+'-messages' try: #Default progress messages with SCP(dev, progress=True) as scp1: scp1.put('scp-test1.txt', remote_path='/var/tmp/') scp1.get('/var/log/messages', local_path=msgfile) #Custom progress messages with SCP(dev, progress=log) as scp2: scp2.put('scp-test2.txt', remote_path='/var/tmp/') scp2.get('/var/log/messages', local_path=msgfile) except Exception as err: print (err) return if __name__ == "__main__": main()
전송 진행률은 표준 출력으로 전송됩니다. 기본 출력(progress=True
)에는 장치 이름, 전송 중인 파일, 전송 진행률(바이트 및 백분율)이 포함됩니다.
router1.example.com: scp-test1.txt: 8 / 8 (100%) router1.example.com: logs/router1.example.com-messages: 0 / 229513 (0%) router1.example.com: logs/router1.example.com-messages: 24576 / 229513 (10%) router1.example.com: logs/router1.example.com-messages: 139264 / 229513 (60%) router1.example.com: logs/router1.example.com-messages: 229513 / 229513 (100%)
이 경우 사용자 지정 함수는 유사한 출력을 생성합니다.
router1.example.com : scp-test2.txt: 1 / 1 (100%) router1.example.com : logs/router1.example.com-messages: 0 / 526493 (0%) router1.example.com : logs/router1.example.com-messages: 57344 / 526493 (10%) router1.example.com : logs/router1.example.com-messages: 106496 / 526493 (20%) router1.example.com : logs/router1.example.com-messages: 212992 / 526493 (40%) router1.example.com : logs/router1.example.com-messages: 319488 / 526493 (60%) router1.example.com : logs/router1.example.com-messages: 368640 / 526493 (70%) router1.example.com : logs/router1.example.com-messages: 475136 / 526493 (90%) router1.example.com : logs/router1.example.com-messages: 526493 / 526493 (100%)
프로그램을 실행한 후 대상 장치에서 명령을 실행하여 file list
scp-test1.txt 및 scp-test2.txt 파일이 올바른 디렉터리에 복사되었는지 확인합니다.
user1@router1> file list /var/tmp/scp-test* /var/tmp/scp-test1.txt /var/tmp/scp-test2.txt
로컬 호스트에서 디바이스 호스트 이름을 포함하도록 이름이 변경된 메시지 로그 파일이 logs 디렉토리에 있어야 합니다.
user1@server:~$ ls logs router1.example.com-messages
기본적으로 Junos PyEZ는 ~/.ssh/config에 있는 경우 기본 SSH 구성 파일을 쿼리합니다. 그러나 인수 목록에 매개 변수를 Device
포함하여 ssh_config
디바이스 인스턴스를 생성할 때 다른 SSH 구성 파일을 지정할 수 있습니다. 예를 들어:
ssh_config_file = '~/.ssh/config_dc' dev = Device('198.51.100.1', ssh_config=ssh_config_file)
또한 인수 목록에 파라미터 Device
를 ssh_private_key_file
포함하여 인증을 SCP
위한 특정 SSH 프라이빗 키 파일을 정의하면 인스턴스는 파일을 전송할 때 동일한 키 파일을 인증에 사용합니다.
key_file='/home/user1/.ssh/id_rsa_dc' dev = Device('198.51.100.1', ssh_private_key_file=key_file) with SCP(dev) as scp: scp.put('scp-test.txt', remote_path='/var/tmp/')
또한 이 SCP
클래스는 netcat을 지원하는 중간 호스트를 통해 로컬 호스트에서 대상 장치로 파일을 전송할 수 있는 ProxyCommand를 지원합니다. 이는 중간 호스트를 통해서만 대상 디바이스에 로그인할 수 있는 경우에 유용합니다. ProxyCommand를 구성하려면 SSH 구성 파일에 적절한 정보를 추가합니다. 예를 들어:
user1@server:~$ cat ~/.ssh/config Host 198.51.100.1 User user1 ProxyCommand ssh -l user1 198.51.100.2 nc %h 22 2>/dev/null