Created
January 30, 2014 07:23
-
-
Save simond/8704067 to your computer and use it in GitHub Desktop.
Toolkit that uses the Paramiko ssh library to do a bunch of stuff like download files, hash files etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment