- while True:
- buf = chan.recv(self.__MAX_RECV_BUF)
- stdout += buf
- if not buf:
- break
- except socket.timeout:
- logger.error('Caught timeout exception, current contents '
- 'of buffer: {0}'.format(stdout))
- raise
-
- stderr = ""
- while True:
- buf = chan.recv_stderr(self.__MAX_RECV_BUF)
- stderr += buf
- if not buf:
- break
-
+ chan = self._ssh.get_transport().open_session(timeout=5)
+ peer = self._ssh.get_transport().getpeername()
+ except (AttributeError, SSHException):
+ self._reconnect()
+ chan = self._ssh.get_transport().open_session(timeout=5)
+ peer = self._ssh.get_transport().getpeername()
+ chan.settimeout(timeout)
+
+ logger.trace(f"exec_command on {peer} with timeout {timeout}: {cmd}")
+
+ if export:
+ export_ssh_command(self._node[u"host"], self._node[u"port"], cmd)
+ start = monotonic()
+ chan.exec_command(cmd)
+ while not chan.exit_status_ready() and timeout is not None:
+ if chan.recv_ready():
+ s_out = chan.recv(self.__MAX_RECV_BUF)
+ stdout += s_out.decode(encoding=u'utf-8', errors=u'ignore') \
+ if isinstance(s_out, bytes) else s_out
+
+ if chan.recv_stderr_ready():
+ s_err = chan.recv_stderr(self.__MAX_RECV_BUF)
+ stderr += s_err.decode(encoding=u'utf-8', errors=u'ignore') \
+ if isinstance(s_err, bytes) else s_err
+
+ duration = monotonic() - start
+ if duration > timeout:
+ if export:
+ export_ssh_timeout(
+ host=self._node[u"host"],
+ port=self._node[u"port"],
+ stdout=stdout,
+ stderr=stderr,
+ duration=duration,
+ )
+ raise SSHTimeout(
+ f"Timeout exception during execution of command: {cmd}\n"
+ f"Current contents of stdout buffer: "
+ f"{stdout}\n"
+ f"Current contents of stderr buffer: "
+ f"{stderr}\n"
+ )
+
+ sleep(0.1)