Skip to content
Next Next commit
capture stderr/stdout over SSH
I need to find out why this solution works while others don't. And
make it more readable..
  • Loading branch information
andreashappe committed Apr 23, 2024
commit 88324d45f1148a1898a438f619964a19ca429883
10 changes: 7 additions & 3 deletions capabilities/ssh_run_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from invoke import Responder

from io import StringIO
from utils import SSHConnection
from .capability import Capability

Expand All @@ -28,15 +29,18 @@ def __call__(self, command: str) -> Tuple[str, bool]:
response=self.conn.password + '\n',
)

out = StringIO()

try:
stdout, stderr, rc = self.conn.run(command, pty=True, warn=True, watchers=[sudo_pass], timeout=10)
resp = self.conn.run(command, pty=True, warn=True, out_stream=out, watchers=[sudo_pass], timeout=10)
except Exception as e:
print("TIMEOUT! Could we have become root?")
stdout, stderr, rc = "", "", -1
out.seek(0)
tmp = ""
last_line = ""
for line in stdout.splitlines():
for line in out.readlines():
if not line.startswith('[sudo] password for ' + self.conn.username + ':'):
line.replace("\r", "")
last_line = line
tmp = tmp + line

Expand Down