Sunday, 1 December 2019

Output garbled when using subprocess.Popen to launch multiple ssh sessions with pseudo-tty

I have a python script that opens multiple concurrent pseudo-tty ssh sessions to a server. My problem is that the output is garbled:

for i in range(0, 3):
    subprocess.Popen(
        "ssh -tt -q myserver 'echo 11; echo 22; echo 33; echo 44;'",
        shell=True
    )

Output:

    11
      22
        33
          44
            11
    22
    33
    44
    11
    22
    33
    44

The output varies. Sometimes it works, but most of the time I get those weird indentations. In reality I want to launch remote python processes (a locust load gen slave), but I've simplified it to just use echo.

Things I've tried:

  • universal_newlines=True, bufsize=1 (doesnt help)
  • remove -tt (fixes the output but has the undesired side effect of remote processes not dying right away if python/ssh is terminated)
  • piping to cat -e to get hidden characters (for debugging):
11^M$
     22^M$
          33^M$
               44^M$
                    11$
22$
33$
44$
11$
   22$
      33$
         44$

I'm not sure if is even a python issue or just an SSH issue. My guess is that I need to use some sort of line buffering, but I dont know how :-/

I'm on MacOS Mojave, and I've tried both in iTerm2 and Term if that matters.



from Output garbled when using subprocess.Popen to launch multiple ssh sessions with pseudo-tty

No comments:

Post a Comment