Skip to main content

SSH & the while loop

Somehow this loop stops after the first element:

$ head -2 hosts
lt1001
lt1002

$ head -2 hosts | while read a; do ssh $a "uname -n"; done
lt1001
The best explanation I could find, comes from a forum post*):
> The "cat list.txt" is providing stdin for the entire "while" loop. It is not 
> constrained to only the read statement. ssh is eating all of the input
> and feeding it to "date" as stdin. And date just ignores the input. Use
> the -n option to ssh to stop this from happening.
And indeed, this is working now:
$ head -2 hosts | while read a; do ssh -n $a "uname -n"; done
lt1001
lt1002
Thanks, Perderabo!

*) copied w/o permission