Ssh to multiple hosts and then run multiple for loops under remote session

Hello,

I am trying to login to multiple servers and i have to run multiple loops to gather some details..Could you please help me out.

I am specifically facing issues while running for loops.

I have to run multiple for loops in else condition. but the below code is giving errors in for loop.

while read LINE
do
srv="$(echo $LINE | awk '{print $1}')"
ssh  $srv '/bin/bash -s' << EOF
if [ -d /home/mvardha ]
then
        echo " directory already exist , exiting"
else
        echo " directory not exist "
'for i in $( ls -ltr /var/log/messages* | awk "{ print \$9 }" ) ; do echo $i ; done'
fi

EOF
done < /home/mvardhan/serverlist

Thanks in advance!

Try using a different file descriptor for the while read loop:

while read LINE <&3
do
   ...
done 3< /home/mvardhan/serverlist

stdin is needed for the ssh command.

Thank you , but the while condition is working fine.

I am having issues with the for loop which is running inside else statement under remote ssh.