Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paramiko
- import shutil
- import hashlib
- import os
- class Server:
- def __init__(self, hostname='', port=0, username='', password=''):
- self.hostname = hostname
- self.port = port
- self.username = username
- self.password = password
- self.connection = self.connect()
- def __enter__(self, hostname='', port=0, username='', password=''):
- return self
- def __exit__(self, hostname='', port=0, username='', password=''):
- self.disconnect()
- def connect(self):
- s = paramiko.SSHClient()
- s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- s.connect(self.hostname, self.port, self.username, self.password)
- print "connection open"
- return s
- def disconnect(self):
- self.connection.close()
- print "connection closed"
- def ExecuteCommand(self, bashCommand):
- stdin, stdout, stderr = self.connection.exec_command(bashCommand)
- #print stdout
- return stdout.read()
- """returns nothing (but should) copy a file"""
- def DownloadFile(self, serverFileLocation, localFileLocation, filePrefix, filePostfix=''):
- transport = paramiko.Transport((self.hostname, self.port))
- transport.connect(username = self.username, password = self.password)
- newFileName = self.CheckFileName('%s\\%s%s%s' % (localFileLocation, filePrefix, os.path.basename(serverFileLocation), filePostfix))
- sftp = paramiko.SFTPClient.from_transport(transport)
- sftp.get(serverFileLocation, newFileName)
- print "Server Hash: %s" % self.HashRemoteFile(serverFileLocation)
- print "Local Hash: %s" % self.HashLocalFile(newFileName)
- print ('HOST: %s \nREMOTE FILE: %s \nLOCAL FILE: %s' % (self.hostname, serverFileLocation, newFileName))
- sftp.close()
- transport.close()
- """returns nothing (but should) copy a file"""
- def UploadFile(self, serverFileLocation, localFileLocation, filePrefix, filePostfix):
- transport = paramiko.Transport((self.hostname, self.port))
- transport.connect(username = self.username, password = self.password)
- newFileName = self.CheckFileName('%s\\%s%s%s' % (localFileLocation, filePrefix, os.path.basename(serverFileLocation), filePostfix))
- sftp = paramiko.SFTPClient.from_transport(transport)
- sftp.get(serverFileLocation, newFileName)
- print "Server Hash: %s" % self.HashRemoteFile(serverFileLocation)
- print "Local Hash: %s" % self.HashLocalFile(newFileName)
- print ('HOST: %s \nREMOTE FILE: %s \nLOCAL FILE: %s' % (self.hostname, serverFileLocation, newFileName))
- sftp.close()
- transport.close()
- def CheckFileName(self, fileName):
- fileCopyNumber = ''
- i = 0
- while os.path.isfile('%s\\%s' % (os.path.dirname(fileName), fileCopyNumber + os.path.basename(fileName))):
- print 'File already found, choosing another name...'
- i=i+1
- fileCopyNumber = str(i) + '_'
- return '%s\\%s' % (os.path.dirname(fileName), fileCopyNumber + os.path.basename(fileName))
- def HashRemoteFile(self, remoteFileLocation):
- #this is slack, add more error checking etc to this
- return self.ExecuteCommand("sha1sum %s" % (remoteFileLocation)).split(' ')[0]
- def HashLocalFile(self, file):
- f = open(file, 'rb')
- h = hashlib.sha1()
- h.update(f.read())
- hash = h.hexdigest()
- f.close()
- return hash
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement