def execute(self, command, logger=None, writein=None, max_lines=None): self.logger.debug("Execute command: '%s'" % command) if not self.client: self._connect() stdin, stdout, stderr = self._paramiko_exec_command(command) # read stdout asynchronously - in 'realtime' output_reader = OutputReader(stdout, logger=logger or self.output_logger, max_lines=max_lines) if writein: # write writein to stdin stdin.write(writein) stdin.flush() stdin.channel.shutdown_write() # wait for end of output output = output_reader.output() # wait for exit code exit_code = stdout.channel.recv_exit_status() if exit_code: err = stderr.read().decode('utf-8').strip() self.logger.debug('command error: %s' % err) raise CommandError(command, exit_code, err) return output
def execute(self, command, logger=None, writein=None, max_lines=None): self.logger.debug("Execute command: '%s'" % command) process = Popen(command, stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True) if writein: # write writein to stdin process.stdin.write(writein.encode()) process.stdin.flush() process.stdin.close() # read stdout asynchronously - in 'realtime' output_reader = OutputReader(process.stdout, logger=logger or self.output_logger, max_lines=max_lines) # wait for end of output output = output_reader.output() # wait for exit code exit_code = process.wait() if exit_code: err = process.stderr.read().decode('utf-8').strip() raise CommandError(command, exit_code, err) return output