Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #!/usr/bin/env python2.7
  2. import paramiko
  3. import os
  4.  
  5. class MySFTPClient(paramiko.SFTPClient):
  6. def exists(self, path):
  7. """os.path.exists for paramiko's SCP object
  8. """
  9. try:
  10. self.stat(path)
  11. except IOError, e:
  12. if e[0] == 2:
  13. return False
  14. raise
  15. else:
  16. return True
  17.  
  18. def put_dir(self, source, target):
  19. self.mkdir(target, ignore_existing=True)
  20. for root, dirs, files in os.walk(source):
  21. for f in files:
  22. local_file = os.path.join(root, f)
  23. remote_file = os.path.join(target, local_file.replace(source, '').replace('/', '', 1))
  24. try:
  25. self.put(local_file, remote_file)
  26. except Exception, e:
  27. self.mkdir(os.path.join(target, os.path.split(remote_file)[0]))
  28. self.put(local_file, remote_file)
  29. for d in dirs:
  30. local_path = os.path.join(root, d)
  31. remote_path = os.path.join(target, local_path.replace(source, '').replace('/', '', 1))
  32. try:
  33. self.mkdir(remote_path)
  34. except Exception, e:
  35. print str(e)
  36. def mkdir(self, path, mode=0775, ignore_existing=False):
  37. try:
  38. super(MySFTPClient, self).mkdir(path, mode)
  39. except IOError as e:
  40. if ignore_existing:
  41. pass
  42. else:
  43. print "[ERROR] mkdir failed! %s" % (str(e),)
  44. raise
  45.  
  46.  
  47. if __name__ == "__main__":
  48. client = paramiko.SSHClient()
  49. client.load_system_host_keys()
  50. client.set_missing_host_key_policy(paramiko.WarningPolicy())
  51. client.connect('10.10.192.68', username='ubuntu', password='ubuntu')
  52. sftp = MySFTPClient.from_transport(client.get_transport())
  53.  
  54. sftp.put_dir('build/nginx/any-static', '/home/ubuntu/any-static')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement