How to exit shell script if remote login unsuccessful?

#!/bin/bash
for servers in `cat ~/servers`
do
rosh -l root -n $servers 'if [ -d /opt/ca ]
then
echo $HOSTNAME
else
exit 1
fi'
done

I have few servers in the for loop that is powered off, so whenever I execute my script, it works fine if all the servers are on, but when it tries to execute the script in the servers that are powered off, my script just hangs there. Please provide me with some hints. I am new to shell scripting. Thank you very much

Not sure about the command rosh it may have a time-out parameter check you on-line manual for it with man rosh

You could try ping of server to ensure it is up first, like this:

#!/bin/bash
while read servers
do
    if ping -c 1 $servers &> /dev/null
    then
	rosh -l root -n $servers 'if [ -d /opt/ca ]
	then
	echo $HOSTNAME
	else
	exit 1
	fi'
    fi
done < ~/servers