Exemplo n.º 1
0
    def execute_shell_command(self,
                              cmd,
                              prompt=None,
                              timeout=100):
        """
        @summary: Executes a command in shell mode and receives all of
            the response.  Parses the response and returns the output
            of the command and the prompt.
        @return: Returns the output of the command and the prompt.

        """

        max_time = time.time() + timeout
        if not cmd.endswith("\n"):
            cmd = "{0}\n".format(cmd)
        self._log.debug('EXEC-SHELLing: {0}'.format(cmd))
        self.wait_for_active_shell(max_time)
        if not self._chan.send_ready():
            self._log.error("Channel timed out for send ready")
            return ExecResponse(
                stdin=None,
                stdout=None,
                stderr=None)
        self._chan.send(cmd)
        output = self.read_shell_response(prompt,
                                          max_time)
        if self._chan.recv_stderr_ready():
            stderr = self._chan.recv_stderr(1024)
        else:
            stderr = None
        return ExecResponse(
            stdin=self._chan.makefile('wb'),
            stdout=output,
            stderr=stderr)
Exemplo n.º 2
0
    def _create_dirs(self, test_dir, max_nodes, client=None):
        client = client or self.client

        setup_cmd = 'mkdir -p {directory}; cd {directory}'.format(
            directory=test_dir)

        overall_response = ExecResponse(
            stdin='', stdout='', stderr='', exit_status=-1)

        client.execute(setup_cmd)
        for iter_num in xrange(1, max_nodes):

            # Consider additional (more aggressive) approach for the cmd:
            # d=`printf "x/"%.0s {1..10}`; mkdir -p $d; cd $d

            cmd = 'mkdir {iter_num}; cd {iter_num}'.format(iter_num=iter_num)
            if self.DEBUG:
                self.fixture_log.debug('Issuing cmd: {cmd}'.format(cmd=cmd))

            cmd_response = self.client.execute(cmd)
            if cmd_response.stdout is not None:
                overall_response.stdout += cmd_response.stdout
            if cmd_response.stderr is not None:
                overall_response.stderr += cmd_response.stderr
            if (cmd_response.exit_status is not None and
                    int(cmd_response.exit_status) >
                    overall_response.exit_status):
                overall_response.exit_status = cmd_response.exit_status

        return overall_response
Exemplo n.º 3
0
    def _format_response(self, resp_dict):
        """
        Converts the exec_command response streams into an object.

        @param resp_dict: A dictionary containing the result
                          of an executed command
        @type resp_dict: dict
        @return: response
        @rtype: ExecResponse
        """

        stdout = (resp_dict.get('stdout').read()
                  if resp_dict.get('stdout') else None)
        stderr = (resp_dict.get('stderr').read()
                  if resp_dict.get('stderr') else None)
        response = ExecResponse(stdin=resp_dict.get('stdin'), stdout=stdout,
                                stderr=stderr,
                                exit_status=resp_dict.get('exit_status'))
        return response
Exemplo n.º 4
0
    def _ping_from_here(
            self, target_ip, count=PING_COUNT, connection=None, ip_version=4):
        """
        Ping target from the execution (local) host

        @param target_ip:  The IP address of the target
        @param count: Number of pings to attempt
        @param connection: Active pexpect session (from self.connect_to_proxy)
        @param ip_version: Version of the IP address to ping.

        :return: ExecResponse containing data (stdin, stdout, stderr).

        """
        # Setup ping command
        ping_version_cmd = 'ping' if ip_version == 4 else 'ping6'
        ping_cmd = '{ping_version_cmd} -c {count} -v {ip}'.format(
            ip=target_ip, count=count, ping_version_cmd=ping_version_cmd)

        # Build list of potential and expected output
        expectations = OrderedDict([
            (pexpect.TIMEOUT, None),
            (self.LINUX_PROMPT_PATTERN, None)])

        # Initialize output object (same used by remote SSH/ping cmd)
        output = ExecResponse()

        # Open process and start command
        if connection is None:
            connection = pexpect.spawn(ping_cmd)
        else:
            connection.sendline(ping_cmd)

        while True:

            # Watch process output and match on specific criteria
            try:
                response = connection.expect(expectations.keys())

            # TIMEOUT, break out of loop and indicate FAILURE
            except pexpect.TIMEOUT:
                err = 'Pinging target timed out. {0} --> {1}'
                self.logger.error(err.format(
                    connection.before, connection.after))
                output.stdout = connection.before
                break

            # CONNECTION CLOSED, save output and break out of loop.
            except pexpect.EOF:
                self.logger.debug('Reached END OF FILE')
                output.stdout = connection.before
                break

            # If TIMEOUT returned by response, break out of loop and indicate
            # FAILURE...
            if response == 0:
                err = 'Pinging target timed out. {0} --> {1}'
                self.logger.error(err.format(
                    connection.before, connection.after))
                output.stdout = connection.before
                break

            # Capture output from ping.
            output.stdout = connection.before + connection.match.group()
            break

        connection.close()
        return output
Exemplo n.º 5
0
    def _ping_from_here(self,
                        target_ip,
                        count=PING_COUNT,
                        connection=None,
                        ip_version=4):
        """
        Ping target from the execution (local) host

        @param target_ip:  The IP address of the target
        @param count: Number of pings to attempt
        @param connection: Active pexpect session (from self.connect_to_proxy)
        @param ip_version: Version of the IP address to ping.

        :return: ExecResponse containing data (stdin, stdout, stderr).

        """
        # Setup ping command
        ping_version_cmd = 'ping' if ip_version == 4 else 'ping6'
        ping_cmd = '{ping_version_cmd} -c {count} -v {ip}'.format(
            ip=target_ip, count=count, ping_version_cmd=ping_version_cmd)

        # Build list of potential and expected output
        expectations = OrderedDict([(pexpect.TIMEOUT, None),
                                    (self.LINUX_PROMPT_PATTERN, None)])

        # Initialize output object (same used by remote SSH/ping cmd)
        output = ExecResponse()

        # Open process and start command
        if connection is None:
            connection = pexpect.spawn(ping_cmd)
        else:
            connection.sendline(ping_cmd)

        while True:

            # Watch process output and match on specific criteria
            try:
                response = connection.expect(expectations.keys())

            # TIMEOUT, break out of loop and indicate FAILURE
            except pexpect.TIMEOUT:
                err = 'Pinging target timed out. {0} --> {1}'
                self.logger.error(
                    err.format(connection.before, connection.after))
                output.stdout = connection.before
                break

            # CONNECTION CLOSED, save output and break out of loop.
            except pexpect.EOF:
                self.logger.debug('Reached END OF FILE')
                output.stdout = connection.before
                break

            # If TIMEOUT returned by response, break out of loop and indicate
            # FAILURE...
            if response == 0:
                err = 'Pinging target timed out. {0} --> {1}'
                self.logger.error(
                    err.format(connection.before, connection.after))
                output.stdout = connection.before
                break

            # Capture output from ping.
            output.stdout = connection.before + connection.match.group()
            break

        connection.close()
        return output