Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from paramiko import SSHClient, AutoAddPolicy
- from scp import SCPClient
- def scp_file():
- host = "host"
- port = 22
- user = "remote"
- pswd = "pswd"
- client = SSHClient()
- client.set_missing_host_key_policy(AutoAddPolicy())
- client.connect(host, port=port, username=user, password=pswd)
- # リモートのファイル一覧を標準出力する
- stdin, stdout, stderr = client.exec_command("cd /home/remote/dir && ls")
- # 標準出力したリストを改行で分割してファイルリストを作成
- files = stdout.read().strip("\n").split("\n")
- ## spcでリモートのファイルの一つをローカルにコピー
- with SCPClient(client.get_transport()) as scp:
- scp.get("/home/remote/%s" % files[0], "/home/local/")
- # リモートのファイルを削除
- client.exec_command("rm /home/remote/%s" %s files[0])
- # 終了
- client.close()
- if __name__ == "__main__":
- scp_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement