SSHing to other hosts in ‘while read’ loop

The while read construct relies on receiving a stream of data through stdin to work. Under normal circumstances ssh also requires access to stdin. This means that without special care a while read loop that contains a call to ssh will only execute once because on the first loop ssh will attach to and drain stdin. This can be demonstrated by:

# cat host_list.txt
server1
server2
server3

# while read RMTHOST; do ssh ${RMTHOST} 'hostname'; done < host_list.txt
server1

#

According to man 1 ssh, the -n flag will cause ssh to not try to read from stdin. This fixes the problem and allows ssh to be used within a while read loop.

# cat host_list.txt
server1
server2
server3

# while read RMTHOST; do ssh -n ${RMTHOST} 'hostname'; done < host_list.txt
server1
server2
server3

#