Thursday, September 1, 2022

[SOLVED] Popen printing weird whitespace

Issue

I am trying to run ssh using subprocess.Popen. It works, but keeps printing some weird whitespace. As the time goes on, whitespace increases

Hi
Hi
Hi
Hi
Hi
Hi
Hi
  Hi
    Hi
      Hi
        Hi
          Hi
            Hi
              Hi
                Hi
                  Hi
                    Hi
                      Hi

And my code was

from subprocess import Popen, DEVNULL
from time import sleep
Popen(["ssh", "-R", "80:127.0.0.1:8080", "localhost.run"], stdout=DEVNULL, stderr=DEVNULL)
while True:
    print("Hi")
    sleep(1)

May be it is related to ssh but I'm not sure about that


Solution

Unix systems traditionally use a single linefeed character (ASCII 10, LF) as a line terminator. However, most terminals need a carriage return + linefeed sequence (CR+LF, 13+10) to move to the start of the next line. To handle this, the operating system's line discipline translates LF to CR+LF on output, and CR (from the terminal's RETURN key) to LF on input.

However, ssh opens a terminal connection to another system, and expects that this translation will happen at the remote end! It therefore turns off this feature in the local line discipline! This is why the end-of-lines that you are printing locally do not get translated.

You do not state what your goals are, so it is difficult to give a solution. A minimal "fix the symptoms" fix could be to give ssh the option -T to stop it from establising a terminal connection to the remote end (and just pass a "raw" data stream).



Answered By - Ture Pålsson
Answer Checked By - David Goodson (WPSolving Volunteer)