Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
command="ls -lrt;uname -a;uname -o"
for i in
ssh auto21@$i "$command 2>&1" >> log
#ssh -n auto21@$i "$command" 2> >> log

i am trying multiple things but nothing seems to work i get the remote output of only the first ip

in the log file

share|improve this question
I tried your example and get the output for both systems in the log... – Johan Feb 26 '13 at 14:24
up vote 2 down vote accepted

You do the 2>&1 on the remote machine. So you're actually doing:

ssh auto21@$1 'ls -lrt;uname -a;uname -o 2>&1' >> log

Which means you're only redirecting the stderr of the last uname command.

Write it:

command="ls -lrt;uname -a;uname -o"
for i in
  ssh "auto21@$i" "$command"
done >> log 2>&1

ssh does use two channels for transferring stdout and stderr, so the remote commands stderr will end up on ssh's stderr. Above we merge the whole loop (and so ssh) stderr into stdout into log.

Or write it:

command="ls -lrt;uname -a;uname -o"
for i in
  ssh "auto21@$i" "{ $command; } 2>&1"
done >> log

That is have stderr merged into stdout at the remote end, but for all the commands, not just the last one. Or alternatively:

  ssh "auto21@$i" "exec 2>&1; $command"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.