Skip to content

Instantly share code, notes, and snippets.

@kurozumi kurozumi/scpwrapper.py
Last active Jul 20, 2016

Embed
What would you like to do?
【Python】scpでリモートサーバー(外部サーバー)のファイルを取得したり削除したりできるSCPClientのラッパークラス
class SCPClientWrapper(object):
def __init__(self, login):
self.login = login
def __enter__(self):
from paramiko import SSHClient, AutoAddPolicy
self.client = SSHClient()
self.client.set_missing_host_key_policy(AutoAddPolicy())
self.client.connect(
login["host"],
port = login["port"],
username = login["user"],
password = login["pass"])
return self
def __exit__(self, exception_type, exception_value, traceback):
self.client.close()
def find(self, remote_path):
stdin, stdout, stderr = self.client.exec_command("find %s -type f -maxdepth 1" % remote_path)
return stdout.read().strip("\n").split("\n")
def ls(self, remote_path):
stdin, stdout, stderr = self.client.exec_command("cd %s && ls" % remote_path)
return stdout.read().strip("\n").split("\n")
def cp(self, remote_path, local_path):
from scp import SCPClient
with SCPClient(self.client.get_transport()) as scp:
scp.get(remote_path, local_path)
return self
def rm(self, remote_path):
self.client.exec_command("rm %s" % remote_path)
return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.