SSH in batch mode and File-Handles in a loop

Hi all
I try to execute SSH commands on several hosts in a while-loop.

There seems to be a problem with file-handle, first cycle works correct but no other one will follow due to the while condition is false even that there are many more host entries (lines) in all_hosts.dat.

#!/usr/bin/ksh
exec 0<all_hosts.dat
while read line
do
echo "...executing on host :" $line
ssh $line 'ls -la /tmp' # execution aborts after the first cycle
###pwd | ssh $line 'ls -la /tmp' # continues for each entry of all_hosts.dat
done # <all_hosts.dat
#

Any clue?
Any pointer to explaining tutorials about file handles?

MTIA

Dave

I too get the same error,ie the SSH session is entered once, in case of while loop.

But it doesnot seems to happen if i use for inplace of while

for host in $(</tmp/hosts); do
echo "Executing on the $host"
ssh $host 'ls -lrt /tmp'
echo $host
done

cat /tmp/hosts
user1@localhost
remote@localhost
others@localhost

Gurus,
Help us why did the code as in OP using the while doesnot work,but using the for loop is works?

Thanks
Nagarajan Ganesan

The problem is that ssh is reading from stdin.

Either have the loop reading from another file descriptor or redirect the stdin of ssh from somewhere else, e.g. /dev/null

... and

ssh -n 'command ...'

as written in the man page was the solution.

tks Chris