Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-04-2011, 05:19 PM   #1
LQ Newbie
Registered: Dec 2010
Posts: 26

Rep: Reputation: 0
ssh command in a loop ?


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.


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

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 ?
Old 01-04-2011, 05:36 PM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
1 members found this post helpful.
Old 01-04-2011, 06:23 PM   #3
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
Another alternative would be to use a for loop:
for i in $(cat host.cfg)
  ssh user@$i uname -a


P.S.: I *really* hope you don't have root-logins via ssh enabled,
possibly even w/o passwords?
Old 01-04-2011, 11:06 PM   #4
LQ Newbie
Registered: Dec 2010
Posts: 26

Original Poster
Rep: Reputation: 0
Both solutions work great. Thanks Guys for your help.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH onto nodes in a loop and then get off MOFH Linux - Newbie 2 11-20-2009 11:41 AM
ssh program for a loop Programming 1 01-26-2009 07:13 PM
bash: ssh from within while loop doesn't work properly peter.lovell Programming 2 10-08-2008 08:44 AM
SSH Bash Loop m1ck Programming 7 01-31-2007 11:03 AM
while loop failure due to ssh hurny^ Programming 1 05-04-2004 04:00 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration