__JMY__
MY Devblog
__JMY__
전체 방문자
오늘
어제
  • 분류 전체보기 (52)
    • Dev (52)
      • Algorithm (6)
      • Linux (12)
      • Network (7)
      • Container (2)
      • Python (14)
      • Frontend (2)
      • Etc (9)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • SCP
  • network
  • algorithm
  • frontend
  • hikaricp
  • wget
  • hash
  • Docker
  • certificate
  • tcpdump
  • Tuple
  • springboot
  • react
  • Kubernetes
  • Python
  • Sorting
  • flexbox
  • flask
  • Linux
  • Ingress

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
__JMY__

MY Devblog

Dev/Python

[python] scp를 이용한 파일전송

2019. 11. 27. 00:30

scp를 이용한 파일전송

python의 paramiko와 scp라이브러리를 사용하여 파일전송이 가능합니다.

라이브러리 설치

scp 전송을 위해 사용될 paramiko, scp 라이브러리를 설치합니다.

pip install paramiko
pip install scp

SSHManger 클래스

SSH 연결 및 종료, scp 전송 및 다운로드 등의 기능을 포함한 SSHManager 클래스를 생성합니다.

  • create_ssh_client: SSH 세션 생성
  • close_ssh_client: SSH 세션 종료
  • send_file: scp를 이용한 파일 전송
  • get_file: scp를 이용한 파일 다운로드
  • send_command: remote 서버에서 명령어 실행
import paramiko
from scp import SCPClient, SCPException

class SSHManager:
    """
    usage:
        >>> import SSHManager
        >>> ssh_manager = SSHManager()
        >>> ssh_manager.create_ssh_client(hostname, username, password)
        >>> ssh_manager.send_command("ls -al")
        >>> ssh_manager.send_file("/path/to/local_path", "/path/to/remote_path")
        >>> ssh_manager.get_file("/path/to/remote_path", "/path/to/local_path")
        ...
        >>> ssh_manager.close_ssh_client()
    """
    def __init__(self):
        self.ssh_client = None

    def create_ssh_client(self, hostname, username, password):
        """Create SSH client session to remote server"""
        if self.ssh_client is None:
            self.ssh_client = paramiko.SSHClient()
            self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.ssh_client.connect(hostname, username=username, password=password)
        else:
            print("SSH client session exist.")

    def close_ssh_client(self):
        """Close SSH client session"""
        self.ssh_client.close()

    def send_file(self, local_path, remote_path):
        """Send a single file to remote path"""
        try:
            with SCPClient(self.ssh_client.get_transport()) as scp:
                scp.put(local_path, remote_path, preserve_times=True)
        except SCPException:
            raise SCPException.message

    def get_file(self, remote_path, local_path):
        """Get a single file from remote path"""
        try:
            with SCPClient(self.ssh_client.get_transport()) as scp:
                scp.get(remote_path, local_path)
        except SCPException:
            raise SCPException.message

    def send_command(self, command):
        """Send a single command"""
        stdin, stdout, stderr = self.ssh_client.exec_command(command)
        return stdout.readlines()

실행

선언한 함수들을 실행하여 전송 & 다운로드 작업을 수행합니다.

ssh_manager = SSHManager()
ssh_manager.create_ssh_client("hostname", "username", "password") # 세션생성
ssh_manager.send_file("local_path", "remote_path") # 파일전송
ssh_manager.get_file('remote_path', 'local_path')  # 파일다운로드
ssh_manager.close_ssh_client() # 세션종료
반응형

'Dev > Python' 카테고리의 다른 글

[python] Shell 명령어 실행법  (0) 2020.03.30
[python] flask 파일 업로드  (0) 2020.03.03
[python] file의 md5 checksum 구하는 방법  (0) 2019.08.13
[python] wget 사용하는 방법  (0) 2019.08.12
Python 자료형 - 6. 집합 (Set)  (0) 2019.06.30
    'Dev/Python' 카테고리의 다른 글
    • [python] Shell 명령어 실행법
    • [python] flask 파일 업로드
    • [python] file의 md5 checksum 구하는 방법
    • [python] wget 사용하는 방법
    __JMY__
    __JMY__
    공부내용 정리 블로그

    티스토리툴바