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 06-07-2012, 09:22 PM   #1
LQ Newbie
Registered: Jun 2012
Posts: 1

Rep: Reputation: Disabled
shell script ssh to multiple servers

Hey all, I'm new to shell scripting and kinda struck here with some of the commands


spawn ssh dmin@host
expect "password"
send "mypassword\n"
send "command1\n"
send "command2\n"
I write this simple code for 1 remote host and its working fine


for host in $(cat SERVERS);
spawn ssh dmin@host
expect "password"
send "mypassword\n"
send "command1\n"
send "command2\n"
However I want to execute the same command to over 20 hosts. Im having some trouble with the it. Is there any recommendation? If I want to use cat function and for loop is this possible and if yes how should I write it? I receive error: "cant' read "(cat hosts)"; no such variable

**btw Im using cygwin terminal to execute the script
Old 06-08-2012, 12:39 AM   #2
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,146

Rep: Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496Reputation: 1496

there are already a number of tools for sshing to and running the same command on multiple hosts. Have a look, for example at, clusterssh, mussh, pssh, taktuk etc.

Also, you should probably be using rsa public/private key authentication instead of entering a password for each host (searching for ssh-keygen and ssh-copy-id should provide a huge number of howtos on this topic).




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
[SOLVED] Run shell script on multiple linux servers vanish78 Linux - Newbie 5 05-16-2012 10:42 AM
[SOLVED] ssh into multiple servers and tail -f greenpool Linux - Newbie 7 09-28-2011 11:05 PM
LXer: ssh on multiple servers Using cluster ssh LXer Syndicated Linux News 0 01-11-2008 03:40 PM
SSH to multiple servers DigiCrime Linux - General 6 06-22-2006 08:19 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:04 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
Open Source Consulting | Domain Registration