Exemplo n.º 1
0
    def run(self, command, arguments=(), console_mode_stdin=True, skip_cmd_shell=False):
        """This function does something.
        :param command: The command to be executed
        :type name: str.
        :param arguments: A list of arguments to be passed to the command
        :type state: str.
        :returns:  int -- the return code.
        :raises: AttributeError, KeyError

        iclegg: blocking i/o operations are slow, doesnt Python have a moden 'async' mechanism
        rather than replying on 80's style callbacks?
        """
        logging.info('running command: ' + command)
        resource = ResourceLocator(CommandShell.ShellResource)
        resource.add_selector('ShellId', self.__shell_id)
        resource.add_option('WINRS_SKIP_CMD_SHELL', ['FALSE', 'TRUE'][bool(skip_cmd_shell)], True)
        resource.add_option('WINRS_CONSOLEMODE_STDIN', ['FALSE', 'TRUE'][bool(console_mode_stdin)], True)

        command = OrderedDict([('rsp:Command', command)])
        command['rsp:Arguments'] = list(arguments)

        response = self.session.command(resource, {'rsp:CommandLine': command})
        command_id = response['rsp:CommandResponse']['rsp:CommandId']
        logging.info('receive command: ' + command_id)
        return command_id
Exemplo n.º 2
0
 def close(self):
     """
     Closes pipe
     :return:
     """
     resource = ResourceLocator(CommandShell.ShellResource)
     resource.add_selector('ShellId', self.__shell_id)
     self.session.delete(resource)
Exemplo n.º 3
0
    def _receive_poll(self, command_id, response_streams):
        """
        Recieves data
        :param command_id:
        :param streams:
        :return:
        """
        logging.info('receive command: ' + command_id)
        resource = ResourceLocator(CommandShell.ShellResource)
        resource.add_selector('ShellId', self.__shell_id)

        stream_attributes = {'#text': " ".join(response_streams.keys()), '@CommandId': command_id}
        receive = {'rsp:Receive': {'rsp:DesiredStream': stream_attributes}}

        try:
            response = self.session.recieve(resource, receive)['rsp:ReceiveResponse']
        except Exception as e:
            return False, None

        # some responses will not include any output
        session_streams = response.get('rsp:Stream', ())
        if not isinstance(session_streams, list):
            session_streams = [session_streams]

        for stream in session_streams:
            if stream['@CommandId'] == command_id and '#text' in stream:
                response_streams[stream['@Name']] += base64.b64decode(stream['#text'])
                # XPRESS Compression Testing
                # print "\\x".join("{:02x}".format(ord(c)) for c in base64.b64decode(stream['#text']))
                # data = base64.b64decode(stream['#text'])
                # f = open('c:\\users\\developer\\temp\\data.bin', 'wb')
                # f.write(data)
                # f.close()
                # decode = api.compression.xpress_decode(data[4:])
        done = response['rsp:CommandState']['@State'] == CommandShell.StateDone
        if done:
            exit_code = int(response['rsp:CommandState']['rsp:ExitCode'])
        else: exit_code = None
        return done, exit_code