5

I used ssh to untar a remote file using:

ssh host "cat file.tgz" | tar xf -

This works perfectly fine. However I noticed while playing with forced commands, that pty-allocation changes the output of the command:

ssh host -T "cat file.tgz" >first_file
ssh host -t "cat file.tgz" >second_file

Here the first file is fine, but the second file is broken.

Why exactly does the allocation of a pty change the output?

Thomas Dickey
  • 76,765
michas
  • 21,510

2 Answers2

3

It helps to read the ssh manual page:

 -T      Disable pseudo-tty allocation.                                     

 -t      Force pseudo-tty allocation.  This can be used to execute arbi‐    
         trary screen-based programs on a remote machine, which can be      
         very useful, e.g. when implementing menu services.  Multiple -t    
         options force tty allocation, even if ssh has no local tty.

When you tell it to allocate a pseudo-tty, any process on the remote side can see that the connection is a "real" terminal, and will send additional messages since the connection is interactive. In your shell initialization, it also is able to setup the terminal modes, which you can inspect using stty -a. Terminal modes are used to translate between your keyboard entry and the host, and between text sent from the host to your terminal:

  • Without initializing, the connection is not a terminal and no translation is done.
  • With initializing, the terminal will translate newline (\n) to carriage-return and line-feed (0x0d, 0x0a). It also will (for most users) translate tabs to spaces.

The effect described is for translation. Without that, your interactive session would "staircase" across the screen and be unusable.

Your shell may also print additional information, but for a single command, the suggestion by @kba is misleading because the shell normally will not send prompts, and the ssh controls such as ~C mentioned apply to input rather than output.

When running to a terminal, ssh will also print a message when closing the connection. But that is written to the standard error.

Thomas Dickey
  • 76,765
0

Why exactly does the allocation of a pty change the output?

Because the remote side (with allocated terminal) will "inject" control characters for your local terminal (basically C0 and C1 control codes). And since your local side is not terminal, but file, it just dumps them into that file.

SSH is trying to do the best to guess what you want (if stdin is not TTY, then it will not allocate remote TTY unless you add -tt switches). The option is there for a reason and if you want binary transfer, you don't want terminals to mess with your file.

You can see this behaviour simply by transfering small files and then hexdump them:

$ ssh -t host "cat test" > /tmp/test.t
$ ssh host "cat test" > /tmp/test
$ hexdump -C /tmp/test
00000000  0a 2a 20 46 72 69 20 46  65 62 20 31 32 20 32 30  |.* Fri Feb 12 20|
00000030  6d 3e 20 33 2e 34 2e 31  2d 31 0a 2d 20 4e 65 77  |m> 3.4.1-1.- New|

$ hexdump -C /tmp/test.t
00000000  0d 0a 2a 20 46 72 69 20  46 65 62 20 31 32 20 32  |..* Fri Feb 12 2|
00000030  6f 6d 3e 20 33 2e 34 2e  31 2d 31 0d 0a 2d 20 4e  |om> 3.4.1-1..- N|

For me the difference is only in the two bytes 0d 0a before every new line, but there might be more (linux line Newline is only \n, but terminal gets both \r\n).

Jakuje
  • 21,357
  • thanks for -1. Are you brave enough to tell me what you don't like on my answer? – Jakuje Feb 18 '16 at 10:06
  • It shows a basic lack of understanding of the process. I would only cite as that. – Thomas Dickey Feb 18 '16 at 10:27
  • @ThomasDickey Yes, your question shows better understanding of the internals. I admit that I don't have 20+ years experience with Unix/Linux, but answering this question needs just common sense. There is no reason for downvote wars if you have better answer and there is nothing obviously wrong here. – Jakuje Feb 18 '16 at 10:41
  • I downvoted your answer because, reading it, it was giving incorrect information, e.g., the term "injecting" gives the reader a misleading sense of what is happening. Then on reflection I decided to provide an answer to give a usable explanation. If there had been a good-enough explanation, I would not have bothered. – Thomas Dickey Feb 18 '16 at 23:38