Guest User

Untitled

a guest
Jul 31st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. import sys
  2. import paramiko
  3. import getpass
  4. import os
  5.  
  6. def upload_files_from_local_directory(ssh, local_path, remote_path):
  7. sftp_client= ssh.open_sftp()
  8.  
  9. for file in os.listdir(local_path):
  10. file_remote = remote_path + file
  11. file_local = local_path + file
  12. print(file_local + ">>>" + file_remote)
  13. sftp_client.put(file_local,file_remote)
  14.  
  15. sftp_client.close()
  16.  
  17. def download_files_from_remote_directory(ssh, local_path, remote_path):
  18. sftp_client= ssh.open_sftp()
  19.  
  20. for file in os.listdir(local_path):
  21. file_remote = remote_path + file
  22. file_local = local_path + file
  23. print(file_remote + ">>>" + file_local)
  24. sftp_client.get(file_remote,file_local)
  25.  
  26. sftp_client.close()
  27.  
  28. def ssh_command(ssh,command):
  29. stdin, stdout, stderr = ssh.exec_command(command)
  30. print(stdout.read())
  31.  
  32. def ssh_connect(host, user, key):
  33. try:
  34. ssh = paramiko.SSHClient()
  35. print('Calling paramiko')
  36. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  37. ssh.connect(hostname=host, username=user, password=key)
  38. return ssh
  39. except Exception as e:
  40. print('Connection Failed')
  41. print(e)
  42.  
  43. if __name__=='__main__':
  44. #CHANGE THESE VARIABLES
  45. host = "SERVER_IP"
  46. user = "USER_LOGIN"
  47. key = "KEY_PWD"
  48. ssh = ssh_connect(host, user, key)
  49. ssh_command(ssh,'echo "Connected to remote server"')
  50. # upload_files_from_local_directory(ssh, "./test.txt", "/tmp")
  51. # download_files_from_remote_directory(ssh, "./", "file.txt")
  52. ssh.close()
Add Comment
Please, Sign In to add comment