Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active July 20, 2016 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/a9304c0f601b4e15afe7588c54deea2f to your computer and use it in GitHub Desktop.
Save kurozumi/a9304c0f601b4e15afe7588c54deea2f to your computer and use it in GitHub Desktop.
【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