def execute_command( self, command, bufsize=-1, timeout=None, stdin_str="", stdin_file=None, raise_exceptions=False): timeout = timeout or DEFAULT_TIMEOUT chan = self._transport.open_session() chan.settimeout(POLLING_RATE) chan.exec_command(command) stdin_str = stdin_str if stdin_file is None else stdin_file.read() stdin = chan.makefile("wb", bufsize) stdout = chan.makefile("rb", bufsize) stderr = chan.makefile_stderr("rb", bufsize) stdin.write(stdin_str) stdin.close() stdout_str = stderr_str = "" exit_status = None max_time = time.time() + timeout while not chan.exit_status_ready(): stderr_str += self._read_channel(stderr) stdout_str += self._read_channel(stdout) if max_time < time.time(): raise socket.timeout( "Command timed out\nSTDOUT:{0}\nSTDERR:{1}\n".format( stdout_str, stderr_str)) exit_status = chan.recv_exit_status() stdout_str += self._read_channel(stdout) stderr_str += self._read_channel(stderr) chan.close() return stdin_str, stdout_str, stderr_str, exit_status
def execute_command(self, command, bufsize=-1, timeout=None, stdin_str="", stdin_file=None, raise_exceptions=False): timeout = timeout or DEFAULT_TIMEOUT chan = self._transport.open_session() chan.settimeout(POLLING_RATE) chan.exec_command(command) stdin_str = stdin_str if stdin_file is None else stdin_file.read() stdin = chan.makefile("wb", bufsize) stdout = chan.makefile("rb", bufsize) stderr = chan.makefile_stderr("rb", bufsize) stdin.write(stdin_str) stdin.close() stdout_str = stderr_str = "" exit_status = None max_time = time.time() + timeout while not chan.exit_status_ready(): stderr_str += self._read_channel(stderr) stdout_str += self._read_channel(stdout) if max_time < time.time(): raise socket.timeout( "Command timed out\nSTDOUT:{0}\nSTDERR:{1}\n".format( stdout_str, stderr_str)) exit_status = chan.recv_exit_status() stdout_str += self._read_channel(stdout) stderr_str += self._read_channel(stderr) chan.close() return stdin_str, stdout_str, stderr_str, exit_status
def _read_shell_response(self, uuid, max_time): stdout = stderr = "" exit_status = None while max_time > time.time(): stdout += self._read_channel(self.channel.recv) stderr += self._read_channel(self.channel.recv_stderr) if stdout.count(uuid) == 2: list_ = stdout.split(uuid) stdout = list_[1] try: exit_status = int(list_[2]) except (ValueError, TypeError): exit_status = None break else: raise socket.timeout( "Command timed out\nSTDOUT:{0}\nSTDERR:{1}\n".format( stdout, stderr)) response = ExecResponse( stdin=None, stdout=stdout.strip(), stderr=stderr, exit_status=exit_status) return response
def _wait_for_active_shell(self, max_time): while not self.channel.send_ready(): time.sleep(POLLING_RATE) if max_time < time.time(): raise socket.timeout("Timed out waiting for active shell")
def _wait_for_active_shell(self, max_time): while not self.channel.send_ready(): time.sleep(common.POLLING_RATE) if max_time < time.time(): raise socket.timeout("Timed out waiting for active shell")