Thursday, May 10, 2007

while loop with ssh issue

somehow while loop with ssh only read the first line from the input file & exit without reading the next lines.
eg:
$ cat server.txt
server1
server2
server3
$ while read server
> do
> echo $server
> ssh $server uptime
> done < server.txt
server1
13:15:08 up 49 days, 1:07, 5 users, load average: 0.00, 0.00, 0.00
the uptime command will only be run on the server1 but not on the server2 or server3. to resolve this, use the -n option with ssh, which prevents ssh from reading from standard input:
> ssh -n $server uptime

alternatively, you can use for loop. should be no issue ;)
$ for server in `cat server.txt`
> do
> echo $server
> ssh $server uptime
> done
for more info - http://sial.org/howto/shell/while-ssh/

No comments: