Advertisement
Guest User

Untitled

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