Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stderr and stdout of ssh?

Tags:

output

ssh

expect

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"
like image 548
munish Avatar asked Oct 13 '25 01:10

munish


1 Answers

for ip in ip1 ip2
do
  ssh user@$ip "some command" > output.$ip 2>&1
done

will put the output in a separate file for each IP.

for ip in ip1 ip2
do
  ssh user@$ip "some command"
done > output 2>&1

will put all the output in a single file.

like image 81
Barmar Avatar answered Oct 15 '25 08:10

Barmar