I want to write a bash script that performs some commands on a remote server via ssh. Then I need to reboot the remote server, connect again via ssh and perform more commands.
I'm using this code:
ssh $theip 'sudo reboot' echo "starting sleep---1" sleep 30 echo " finished sleeping1" while ! ping -c 1 $theip &>/dev/null; do echo "waiting..."; done echo "starting sleep---2" sleep 45 echo " finished sleeping2" echo "finished rebooting" ssh $theip 'commands....'
The problem is that sometimes the server is returning pings before I can ssh to it and then the script fails. Some of my servers need more than the 45s sleep. Is there any other method to do this? For example, try to ssh in the while loop instead of ping?