Example #1
0
    def exec_script(self, script):
        logger.debug("SSHExex.exec_script...")
        logger.debug('script content = {0}'.format(script))
        exec_scheme, exec_parts = uriparse(self.uri)
        ssh = sshclient(exec_parts.hostname, exec_parts.port, self.credential)
        sftp = None
        try:
            sftp = ssh.open_sftp()

            script_name = self.upload_script(sftp, script)
            stdin, stdout, stderr = ssh.exec_command(script_name, bufsize=-1, timeout=None, get_pty=False)
            stdin.close()
            exit_code = stdout.channel.recv_exit_status()
            logger.debug("sshclient exec'd script OK")

            self.remove_script(sftp, script_name)

            return exit_code, stdout.readlines(), stderr.readlines()
        except paramiko.SSHException as sshe:
            raise RetryException(sshe, traceback.format_exc())
        finally:
            try:
                if sftp is not None:
                    sftp.close()
                if ssh is not None:
                    ssh.close()
            except:
                pass
Example #2
0
    def exec_script(self, script):
        logger.debug("SSHExex.exec_script...")
        logger.debug("script content = {0}".format(script))
        exec_scheme, exec_parts = uriparse(self.uri)
        ssh = sshclient(exec_parts.hostname, exec_parts.port, self.credential)
        sftp = None
        try:
            sftp = ssh.open_sftp()

            script_name = self.upload_script(sftp, script)
            stdin, stdout, stderr = ssh.exec_command(script_name, bufsize=-1, timeout=None, get_pty=False)
            stdin.close()
            exit_code = stdout.channel.recv_exit_status()
            logger.debug("sshclient exec'd script OK")

            self.remove_script(sftp, script_name)

            return exit_code, stdout.readlines(), stderr.readlines()
        except paramiko.SSHException as sshe:
            raise RetryException(sshe, traceback.format_exc())
        finally:
            try:
                if sftp is not None:
                    sftp.close()
                if ssh is not None:
                    ssh.close()
            except:
                pass
Example #3
0
 def sshclient(self):
     exec_scheme, exec_parts = uriparse(self.uri)
     ssh = sshclient(exec_parts.hostname, exec_parts.port, self.credential)
     try:
         yield ssh
     finally:
         try:
             ssh.close()
         except:
             pass
Example #4
0
 def sshclient(self):
     exec_scheme, exec_parts = uriparse(self.uri)
     ssh = sshclient(exec_parts.hostname, exec_parts.port, self.credential)
     try:
         yield ssh
     finally:
         try:
             ssh.close()
         except:
             pass
Example #5
0
 def rm(self, uri):
     """recursively delete a uri"""
     scheme, parts = uriparse(uri)
     logger.debug('{0}'.format(parts.path))
     ssh = sshclient(parts.hostname, parts.port, self.cred.credential)
     try:
         sftp = ssh.open_sftp()
         self._rm(sftp, parts.path)
     except Exception as exc:
         raise RetryException(exc, traceback.format_exc())
     finally:
         try:
             if ssh is not None:
                 ssh.close()
         except:
             pass
Example #6
0
 def rm(self, uri):
     """recursively delete a uri"""
     scheme, parts = uriparse(uri)
     logger.debug("{0}".format(parts.path))
     ssh = sshclient(parts.hostname, parts.port, self.cred.credential)
     try:
         sftp = ssh.open_sftp()
         self._rm(sftp, parts.path)
     except Exception as exc:
         raise RetryException(exc, traceback.format_exc())
     finally:
         try:
             if ssh is not None:
                 ssh.close()
         except:
             pass
Example #7
0
 def ls(self, uri):
     """ls at uri"""
     self.set_cred(uri)
     scheme, parts = uriparse(uri)
     ssh = sshclient(parts.hostname, parts.port, self.cred.credential)
     try:
         sftp = ssh.open_sftp()
         results = self._do_ls(sftp, parts.path)
         output = {}
         output[parts.path] = results
         return output
     except FileNotFoundError:
         return {}
     except Exception as exc:
         logger.exception("ls: %s" % uri)
         raise RetryException(exc, traceback.format_exc())
     finally:
         try:
             if ssh is not None:
                 ssh.close()
         except:
             pass
Example #8
0
 def ls(self, uri):
     """ls at uri"""
     self.set_cred(uri)
     scheme, parts = uriparse(uri)
     ssh = sshclient(parts.hostname, parts.port, self.cred.credential)
     try:
         sftp = ssh.open_sftp()
         results = self._do_ls(sftp, parts.path)
         output = {}
         output[parts.path] = results
         return output
     except FileNotFoundError:
         return {}
     except Exception as exc:
         logger.exception("ls: %s" % uri)
         raise RetryException(exc, traceback.format_exc())
     finally:
         try:
             if ssh is not None:
                 ssh.close()
         except:
             pass
Example #9
0
    def mkdir(self, uri):
        """mkdir at uri"""
        self.set_cred(uri)
        scheme, parts = uriparse(uri)
        path = parts.path
        ssh = sshclient(parts.hostname, parts.port, self.cred.credential)
        try:
            sftp = ssh.open_sftp()
            try:
                self._rm(sftp, path)
                logger.debug("deleted existing directory %s OK" % path)
            except Exception as ex:
                logger.debug("could not remove directory %s: %s" % (path, ex))

            def full_path(result, d):
                previous = result[-1] if result else ""
                result.append("%s/%s" % (previous, d))
                return result

            dirs = [p for p in path.split("/") if p.strip() != '']
            dir_full_paths = reduce(full_path, dirs, [])
            non_existant_dirs = dropwhile(lambda d: self.path_exists(sftp, d),
                                          dir_full_paths)

            for d in non_existant_dirs:
                sftp.mkdir(d)

            logger.debug("created dir %s OK" % path)

        except Exception as exc:
            logger.error(exc)
            raise RetryException(exc, traceback.format_exc())
        finally:
            try:
                if ssh is not None:
                    ssh.close()
            except:
                pass
Example #10
0
    def mkdir(self, uri):
        """mkdir at uri"""
        self.set_cred(uri)
        scheme, parts = uriparse(uri)
        path = parts.path
        ssh = sshclient(parts.hostname, parts.port, self.cred.credential)
        try:
            sftp = ssh.open_sftp()
            try:
                self._rm(sftp, path)
                logger.debug("deleted existing directory %s OK" % path)
            except Exception as ex:
                logger.debug("could not remove directory %s: %s" % (path, ex))

            def full_path(result, d):
                previous = result[-1] if result else ""
                result.append("%s/%s" % (previous, d))
                return result

            dirs = [p for p in path.split("/") if p.strip() != ""]
            dir_full_paths = reduce(full_path, dirs, [])
            non_existant_dirs = dropwhile(lambda d: self.path_exists(sftp, d), dir_full_paths)

            for d in non_existant_dirs:
                sftp.mkdir(d)

            logger.debug("created dir %s OK" % path)

        except Exception as exc:
            logger.error(exc)
            raise RetryException(exc, traceback.format_exc())
        finally:
            try:
                if ssh is not None:
                    ssh.close()
            except:
                pass
Example #11
0
 def connect(self, host, port, credential):
     return sshclient(host, port, credential)
Example #12
0
 def connect(self, host, port, credential):
     return sshclient(host, port, credential)