Search code examples
sshexpectoutput

stderr and stdout of ssh?


I am not clear about the stdout and stderr of ssh because if I execute say :

ssh user@remotemachine_ip "some command"

I get the output instantly on the screen or may be some error

  1. logged in with ssh

  2. remote commands are executed on the remote machine

  3. the output is displayed on the remote machine (stderr or stdout) which I can see. Then it comes back to local machine.

or

the output come back to the local machine's stdout and which can be appended to a file on local machine.

So if I have a for loop in local machine

for i in ip1 ip2
do
    ssh user@remotemachine_ip "some command" 
done

what is the best way for error redirection and is it dependent on for, while, until loops

what if instead of ssh I am using some expect script

expectscript.exp user@remotemachine_ip "some command"

Solution

  • SSH is a protocol that lets you login into the remote machine just like you would login on a local machine.

    In a local machine if you run a login shell (say one of Ctrl + Alt + F1~6), it starts a new shell for the user and the output (stdout and stderr) goes right on your monitor.

    Now let's take a look at your SSH case, the steps are similar to last time with just one difference, instead of sending the output (again stdout and stderr) to the monitor, the server sends the data back to the machine from which you logged in - to the SSH client. The client runs in your local machine and hence just displays the same output on the local machine's monitor.

    But in case if you were to redirect the stdout and/or stderr to a file, when you're in a shell on the remote machine, that file would be on the remote machine, since the actual program you're running is executed on the remote machine.

    BTW SSH does more than just merely sending/receiving the data to/from the remote machine, it also encrypts the data and lot more.