I have a file servers.txt
, with list of servers:
server1.mydomain.com
server2.mydomain.com
server3.mydomain.com
when I read the file line by line with while
and echo each line, all works as expected. All lines are printed.
$ while read HOST ; do echo $HOST ; done < servers.txt
server1.mydomain.com
server2.mydomain.com
server3.mydomain.com
However, when I want to ssh to all servers and execute a command, suddenly my while
loop stops working:
$ while read HOST ; do ssh $HOST "uname -a" ; done < servers.txt
Linux server1 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux
This only connects to the first server in the list, not to all of them. I don't understand what is happening here. Can somebody please explain?
This is even stranger, since using for
loop works fine:
$ for HOST in $(cat servers.txt ) ; do ssh $HOST "uname -a" ; done
Linux server1 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux
Linux server2 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux
Linux server3 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux
It must be something specific to ssh
, because other commands work fine, such as ping
:
$ while read HOST ; do ping -c 1 $HOST ; done < servers.txt
ansible