Advertisement
Uno-Dan

Wait till online

Jun 18th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.10 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. HOST=$1 # Resolvable host name or IP address of remote host.
  4. TIMEOUT=$2
  5.  
  6. wait_for_host() {
  7.     cnt=0
  8.     host=$1
  9.     status=up
  10.     timeout=$2
  11.    
  12.     ping -c 1 -n -w 1 $host | grep "1 received" > /dev/null
  13.     result=$?
  14.     [ $result != 0 ] && status=down
  15.    
  16.     echo
  17.     echo -n "Waiting for host \"$host\" to come back online: "
  18.    
  19.     while true; do
  20.         ping -c 1 -n -w 1 $host | grep "0 received" > /dev/null
  21.         result=$?
  22.        
  23.         echo -n "."
  24.         [ $result == 1 ] && { sleep 1; cnt=$((cnt+1)); }
  25.    
  26.        
  27.         if [ $result == 1 ] && [ status == "up" ]; then
  28.             continue
  29.         elif [ $result == 0 ]; then
  30.             status=down
  31.         fi
  32.        
  33.         [ $result == 1 ] && [ $status == "down" ] && {
  34.             sleep 1
  35.             echo -e "\nHost \"$host\" is back online."
  36.             break
  37.         }
  38.        
  39.         if [ $cnt -ge $timeout ]; then
  40.             echo -e "\nRemote host \"$host\" did not reboot.\n"
  41.             break
  42.         fi
  43.     done
  44. }
  45.  
  46. echo
  47. ssh root@$HOST reboot
  48.  
  49. wait_for_host $HOST $TIMEOUT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement