Paramiko
Paramiko는 Python에서 ssh 접속을 제어 가능하도록 하는 라이브러리로, ssh 원격접속과 파일전송을 가능하게하는 Fabric 라이브러리도 paramiko를 기반으로한다. Fabric의 사용 용도보다 심화된 또는 low level의 코딩을 해야하는 때와, Python 내에서 sshd (ssh daemon) 를 돌릴 때에 paramiko를 직접적으로 사용하게된다. 여기서 daemon은 백그라운드 상 요청을 기다리는 프로세스로, sshd는 ssh 연결을 받아주기 위해 대기하는 프로세스정도로 이해하면 된다. 오늘은 파일을 주고 받는정도의 간단한 코드를 소개한다.
Code
SSH client를 만들고, 연결한다.
# Client class
ssh = paramiko.SSHClient()
# Host key policy에 따라 ssh .HostKeys에 host key 저장
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
ssh.connect(hostname=server, username=server_user, password=pwd, port=port_ID)
파일을 주고받는다.
# Open an SFTP client
sftp = ssh.open_sftp()
# Download and upload files
sftp.get('myFile1.csv', 'myFile2.csv')
sftp.put('myFile3.csv', 'myFile4.csv')
연결 종료.
# Close the SFTP and SSH clients
sftp.close()
ssh.close()
참고
https://docs.paramiko.org/en/latest/index.html
Welcome to Paramiko’s documentation! — Paramiko documentation
Welcome to Paramiko’s documentation! This site covers Paramiko’s usage & API documentation. For basic info on what Paramiko is, including its public changelog & how the project is maintained, please see the main project website. API documentation The h
docs.paramiko.org
https://hackersandslackers.com/automate-ssh-scp-python-paramiko/(그림)
SSH & SCP in Python with Paramiko
Automate remote server tasks by using the Paramiko & SCP Python libraries. Use Python to SSH into hosts, execute tasks, & transfer files.
hackersandslackers.com
'정리 조금 > Codes' 카테고리의 다른 글
[Python] Package Version Check (0) | 2023.12.11 |
---|---|
[Python] Image Data, Shape Transformation (0) | 2023.12.04 |
[html] Tistory hELLO Skin, 사이드바에 방명록 추가 (0) | 2023.10.24 |
[Python] Cytominer Import Error (0) | 2023.10.18 |
[Linux] Virtual Environment (0) | 2023.10.05 |