Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. from paramiko import SSHClient, AutoAddPolicy
  2. from scp import SCPClient
  3.  
  4. def scp_file():
  5. host = "host"
  6. port = 22
  7. user = "remote"
  8. pswd = "pswd"
  9.  
  10. client = SSHClient()
  11. client.set_missing_host_key_policy(AutoAddPolicy())
  12. client.connect(host, port=port, username=user, password=pswd)
  13.  
  14. # リモートのファイル一覧を標準出力する
  15. stdin, stdout, stderr = client.exec_command("cd /home/remote/dir && ls")
  16.  
  17.   # 標準出力したリストを改行で分割してファイルリストを作成
  18. files = stdout.read().strip("\n").split("\n")
  19.  
  20.  ## spcでリモートのファイルの一つをローカルにコピー
  21. with SCPClient(client.get_transport()) as scp:
  22. scp.get("/home/remote/%s" % files[0], "/home/local/")
  23.  
  24.   # リモートのファイルを削除
  25. client.exec_command("rm /home/remote/%s" %s files[0])
  26.  
  27. # 終了
  28. client.close()
  29.  
  30. if __name__ == "__main__":
  31. scp_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement