Example #1
0
    def connect(self):
        """Attempt a connection using psexec.py.

        This will create a subprocess.Popen() instance and communicate with it
        via _file_read/_file_write and _process.stdin
        """
        try:
            if self._connected and self._process:
                if self._process.poll() is None:
                    return
                else:
                    self._process.wait()
                    if self.gateway:
                        self.shutdown_tunnel()
            if self.gateway:
                self.create_tunnel()
            self._substituted_command = self._command % (os.path.dirname(
                __file__), self.port, self.username, self.password, self.host)
            self._process = popen.popen(shlex.split(self._substituted_command),
                                        stdout=self._file_write,
                                        stderr=popen.STDOUT,
                                        stdin=popen.PIPE,
                                        close_fds=True,
                                        universal_newlines=True,
                                        bufsize=-1)
            output = ''
            while not self._prompt_pattern.findall(output):
                output += self._get_output()
            self._connected = True
        except Exception:
            LOG.error("Failed to connect to host %s over smb",
                      self.host,
                      exc_info=True)
            self.close()
            raise
Example #2
0
    def execute(self, command, **kwargs):
        """Execute a command (containing no shell operators) locally.

        :param command:         Shell command to be executed.
        :param with_exit_code:  Include the exit_code in the return body.
                                Default is False.
        :param cwd:              The child's current directory will be changed
                                to `cwd` before it is executed. Note that this
                                directory is not considered when searching the
                                executable, so you can't specify the program's
                                path relative to this argument
        :returns:               A dict with stdin, stdout, and
                                (optionally) the exit code.
        """
        cwd = kwargs.get("cwd")
        with_exit_code = kwargs.get("with_exit_code")
        spipe = popen.PIPE

        cmd = shlex.split(command)
        LOG.debug("Executing `%s` on local machine", command)
        result = popen.popen(cmd, stdout=spipe, stderr=spipe, cwd=cwd)
        out, err = result.communicate()
        resultdict = {"stdout": out.strip(), "stderr": err.strip()}
        if with_exit_code:
            resultdict.update({"exit_code": result.returncode})
        return resultdict
Example #3
0
    def execute(self, command, **kwargs):
        """Execute a command (containing no shell operators) locally.

        :param command:         Shell command to be executed.
        :param with_exit_code:  Include the exit_code in the return body.
                                Default is False.
        :param cwd:              The child's current directory will be changed
                                to `cwd` before it is executed. Note that this
                                directory is not considered when searching the
                                executable, so you can't specify the program's
                                path relative to this argument
        :returns:               A dict with stdin, stdout, and
                                (optionally) the exit code.
        """
        cwd = kwargs.get('cwd')
        with_exit_code = kwargs.get('with_exit_code')
        spipe = popen.PIPE

        cmd = shlex.split(command)
        LOG.debug("Executing `%s` on local machine", command)
        result = popen.popen(
            cmd, stdout=spipe, stderr=spipe, cwd=cwd)
        out, err = result.communicate()
        resultdict = {
            'stdout': out.strip(),
            'stderr': err.strip(),
        }
        if with_exit_code:
            resultdict.update({'exit_code': result.returncode})
        return resultdict
Example #4
0
    def connect(self):
        """Attempt a connection using psexec.py.

        This will create a subprocess.Popen() instance and communicate with it
        via _file_read/_file_write and _process.stdin
        """
        try:
            if self._connected and self._process:
                if self._process.poll() is None:
                    return
                else:
                    self._process.wait()
                    if self.gateway:
                        self.shutdown_tunnel()
            if self.gateway:
                self.create_tunnel()
            self._substituted_command = self._command % (
                os.path.dirname(__file__),
                self.port,
                self.username,
                self.password,
                self.host)
            self._process = popen.popen(
                shlex.split(self._substituted_command),
                stdout=self._file_write,
                stderr=popen.STDOUT,
                stdin=popen.PIPE,
                close_fds=True,
                universal_newlines=True,
                bufsize=-1)
            output = ''
            while not self._prompt_pattern.findall(output):
                output += self._get_output()
            self._connected = True
        except Exception:
            LOG.error("Failed to connect to host %s over smb",
                      self.host, exc_info=True)
            self.close()
            raise