Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SSHSession(object):
- # Usage:
- # Detects DSA or RSA from key_file, either as a string filename or a
- # file object. Password auth is possible, but I will judge you for
- # using it. So:
- # ssh=SSHSession('targetserver.com','root',key_file=open('mykey.pem','r'))
- # ssh=SSHSession('targetserver.com','root',key_file='/home/me/mykey.pem')
- # ssh=SSHSession('targetserver.com','root','mypassword')
- # ssh.put('filename','/remote/file/destination/path')
- # ssh.put_all('/path/to/local/source/dir','/path/to/remote/destination')
- # ssh.get_all('/path/to/remote/source/dir','/path/to/local/destination')
- # ssh.command('echo "Command to execute"')
- def __init__(self,hostname,username='root',key_file=None,password=None):
- #
- # Accepts a file-like object (anything with a readlines() function)
- # in either dss_key or rsa_key with a private key. Since I don't
- # ever intend to leave a server open to a password auth.
- #
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.sock.connect((hostname,22))
- self.t = paramiko.Transport(self.sock)
- self.t.start_client()
- keys = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
- key = self.t.get_remote_server_key()
- # supposed to check for key in keys, but I don't much care right now to find the right notation
- if key_file is not None:
- if isinstance(key,str):
- key_file=open(key,'r')
- key_head=key_file.readline()
- key_file.seek(0)
- if 'DSA' in key_head:
- keytype=paramiko.DSSKey
- elif 'RSA' in key_head:
- keytype=paramiko.RSAKey
- else:
- raise Exception("Can't identify key type")
- pkey=keytype.from_private_key(key_file)
- self.t.auth_publickey(username, pkey)
- else:
- if password is not None:
- self.t.auth_password(username,password,fallback=False)
- else: raise Exception('Must supply either key_file or password')
- self.sftp=paramiko.SFTPClient.from_transport(self.t)
- def remote_walk(self, remotepath):
- dirs = []
- #files = []
- for f in self.sftp.listdir_attr(remotepath):
- if S_ISDIR(f.st_mode):
- if f.filename != '.' and f.filename != '..':
- dirs.append(f.filename)
- #else:
- # files.append(f.filename)
- #return dirs, files
- return dirs
- def find_folder(self, remotepath, foldername):
- print("find_folder:", remotepath, foldername)
- dirs = self.remote_walk(remotepath)
- if foldername in dirs:
- return os.path.join(remotepath, foldername)
- else:
- print("I am trying to find in child directories")
- for subfolder in dirs:
- subfolder_fullpath = os.path.join(remotepath, subfolder)
- print("subfolder:", subfolder)
- result = self.find_folder(subfolder_fullpath, foldername)
- # if found path then return it (in recursion)
- if results:
- return result
- # else go back to `for` and check next subfolder
- #return None # it automatically returns None if can't find folder
- ###################################################
- sshobject = SSHSession(hostname=host_name, username=test_user, key_file=None, password=test_password)
- sshobject.find_folder(remotepath=str(base_dir), foldername="folder_name_to_search")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement