Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
command="ls -lrt;uname -a;uname -o"
for i in 10.38.227.229 10.38.164.103
do
ssh auto21@$i "$command 2>&1" >> log
#ssh -n auto21@$i "$command" 2> >> log
done

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
1  
I tried your example and get the output for both systems in the log... –  Johan Feb 26 '13 at 14:24

1 Answer 1

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 10.38.227.229 10.38.164.103
do
  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 10.38.227.229 10.38.164.103
do
  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

 
discard

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.