Check SSH Connection .. help ..

I've script where i check connection to many hosts ... it take input from HOSTLIST.txt
Ex. cat HOSTLIST.txt
host1
host2

while read HOSTNAME
do
echo $HOSTNAME
ssh -o "BatchMode=yes" ${HOSTNAME} "echo 2>&1" $$ echo flag=0 || flag=1;
if [ $? -ne 0 ]; then
echo "No SSH Connection" >> ${LOG}
else
echo " SSH Connection Good " >> ${LOG}
fi
done < HOSTLIST.txt

Problem is it processes host1 successfully and breaks before even it checks for host2

+ read HOSTNAME
+ ssh -o BatchMode=yes host1 echo 2>&1 2701 echo flag=0
2701 echo flag=0
+ [ 0 -ne 0 ]
+ echo SSH Connection Good
+ 1>> logfile.log
+ read HOSTLIST.txt

this is all i get from set -x mode ....

any ideas why its breaking ?

You should try a for loop instead.

WOW ... for loop worked

thank you so much chatwizrd

don't know why while loop did not work .. but it will do the job ... thanks again

Probably because stdin was coming from your HOSTLIST.txt file and was swallowed by ssh.

You might have tried the "ssh -n" option.