EDIT2: since OP mentioned that there is a file named IP.list
from which server names should be read then following may help you here.
while read server
do
if [[ -n $(ssh user_name@"$server" "ls -ld /tmp") ]]
then
echo "$server is up."
echo "Exiting from script now.."
exit;
else
echo "$server is NOT up, look for another server."
fi
done < "IP.txt"
EDIT: Since OP is saying ping
is not an option to check server's availability then using ssh
only for servers. Considering that your source server(from where you are running the script) and target server(whose status you are checking are having passwordless connection between them so that you will NOT be prompted for any password)
if [[ -n $(ssh user_name@server_name "ls -ld /tmp") ]]
then
echo "Server 1 is up."
else
echo "Server is NOT up, look for another server."
fi
You could simple do ping
to SERVER1 and see if that is happening successfully in case it not it means server is not up and you could move your things to another stand up server.
if [[ $(ping -c5 SERVER1 | grep -q "5 packets transmitted, 5 received") -eq 0 ]]
then
echo "Server1 is up."
else
echo "Server is NOT up, look for another server."
fi