LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ssh command in a loop ? (http://www.linuxquestions.org/questions/linux-newbie-8/ssh-command-in-a-loop-854225/)

gadikota 01-04-2011 04:19 PM

ssh command in a loop ?
 
Hello,

I am trying to run the same command(s) on the many destination servers from my source server.

source server user "report" ssh keys are added to all destination hosts.

hosts.cfg:
----------
gadikota_dev01
gadikota_dev02
gadikota_prod01
gadikota_prod02

loop command:
cat hosts.cfg | while read line
do
ssh root@$line uname -a
done

Output:
Linux gadikota_dev01 2.6.9-67.EL.bz450900.HFsmp #1 SMP Thu Jun 19 16:25:45 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux

as you see above even when i am running a loop its just splitting the output of first server and not others.

Anything special i need to do ? or is there a different way ?

colucix 01-04-2011 04:36 PM

Yes. Add option -n to the ssh command. The standard input coming from the pipe becomes the standard input of the remote command (this is the way ssh works) and when the ssh command terminates it sends a SIGTTIN signal to the local shell: it closes the standard input and causes the loop to exit. The -n option redirects the standard input from /dev/null, leaving the stdin of the local shell untouched. Hope this helps.

Tinkster 01-04-2011 05:23 PM

Another alternative would be to use a for loop:
Code:

for i in $(cat host.cfg)
do
  ssh user@$i uname -a
done


Cheers,
Tink


P.S.: I *really* hope you don't have root-logins via ssh enabled,
possibly even w/o passwords?

gadikota 01-04-2011 10:06 PM

Both solutions work great. Thanks Guys for your help.


All times are GMT -5. The time now is 01:50 AM.