LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

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

Rep: Reputation: 0
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 ?
 
Old 01-04-2011, 04:36 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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, 05:23 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,966
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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?
 
Old 01-04-2011, 10:06 PM   #4
gadikota
LQ Newbie
 
Registered: Dec 2010
Posts: 25

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


Reply


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 10:41 AM
ssh program for a loop g4gopal@yahoo.com Programming 1 01-26-2009 06:13 PM
bash: ssh from within while loop doesn't work properly peter.lovell Programming 2 10-08-2008 07:44 AM
SSH Bash Loop m1ck Programming 7 01-31-2007 10:03 AM
while loop failure due to ssh hurny^ Programming 1 05-04-2004 03:00 PM


All times are GMT -5. The time now is 10:43 PM.

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