Advertisement
Guest User

Untitled

a guest
May 16th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. import paramiko
  2. import shutil
  3. import hashlib
  4. import os
  5.  
  6. class Server:
  7. def __init__(self, hostname='', port=0, username='', password=''):
  8. self.hostname = hostname
  9. self.port = port
  10. self.username = username
  11. self.password = password
  12. self.connection = self.connect()
  13.  
  14. def __enter__(self, hostname='', port=0, username='', password=''):
  15. return self
  16.  
  17. def __exit__(self, hostname='', port=0, username='', password=''):
  18. self.disconnect()
  19.  
  20. def connect(self):
  21. s = paramiko.SSHClient()
  22. s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  23. s.connect(self.hostname, self.port, self.username, self.password)
  24. print "connection open"
  25. return s
  26.  
  27. def disconnect(self):
  28. self.connection.close()
  29. print "connection closed"
  30.  
  31. def ExecuteCommand(self, bashCommand):
  32. stdin, stdout, stderr = self.connection.exec_command(bashCommand)
  33. #print stdout
  34. return stdout.read()
  35.  
  36. """returns nothing (but should) copy a file"""
  37. def DownloadFile(self, serverFileLocation, localFileLocation, filePrefix, filePostfix=''):
  38. transport = paramiko.Transport((self.hostname, self.port))
  39. transport.connect(username = self.username, password = self.password)
  40. newFileName = self.CheckFileName('%s\\%s%s%s' % (localFileLocation, filePrefix, os.path.basename(serverFileLocation), filePostfix))
  41.  
  42. sftp = paramiko.SFTPClient.from_transport(transport)
  43. sftp.get(serverFileLocation, newFileName)
  44.  
  45. print "Server Hash: %s" % self.HashRemoteFile(serverFileLocation)
  46. print "Local Hash: %s" % self.HashLocalFile(newFileName)
  47. print ('HOST: %s \nREMOTE FILE: %s \nLOCAL FILE: %s' % (self.hostname, serverFileLocation, newFileName))
  48.  
  49. sftp.close()
  50. transport.close()
  51.  
  52. """returns nothing (but should) copy a file"""
  53. def UploadFile(self, serverFileLocation, localFileLocation, filePrefix, filePostfix):
  54. transport = paramiko.Transport((self.hostname, self.port))
  55. transport.connect(username = self.username, password = self.password)
  56. newFileName = self.CheckFileName('%s\\%s%s%s' % (localFileLocation, filePrefix, os.path.basename(serverFileLocation), filePostfix))
  57.  
  58. sftp = paramiko.SFTPClient.from_transport(transport)
  59. sftp.get(serverFileLocation, newFileName)
  60.  
  61. print "Server Hash: %s" % self.HashRemoteFile(serverFileLocation)
  62. print "Local Hash: %s" % self.HashLocalFile(newFileName)
  63. print ('HOST: %s \nREMOTE FILE: %s \nLOCAL FILE: %s' % (self.hostname, serverFileLocation, newFileName))
  64.  
  65. sftp.close()
  66. transport.close()
  67.  
  68. def CheckFileName(self, fileName):
  69. fileCopyNumber = ''
  70. i = 0
  71. while os.path.isfile('%s\\%s' % (os.path.dirname(fileName), fileCopyNumber + os.path.basename(fileName))):
  72. print 'File already found, choosing another name...'
  73. i=i+1
  74. fileCopyNumber = str(i) + '_'
  75. return '%s\\%s' % (os.path.dirname(fileName), fileCopyNumber + os.path.basename(fileName))
  76.  
  77. def HashRemoteFile(self, remoteFileLocation):
  78. #this is slack, add more error checking etc to this
  79. return self.ExecuteCommand("sha1sum %s" % (remoteFileLocation)).split(' ')[0]
  80.  
  81. def HashLocalFile(self, file):
  82. f = open(file, 'rb')
  83. h = hashlib.sha1()
  84. h.update(f.read())
  85. hash = h.hexdigest()
  86. f.close()
  87. return hash
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement