Advertisement
Uno-Dan

Wait for host

Jun 18th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.12 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.    
  11.     [ "$2" == "" ] && timeout=90 || timeout=$2
  12.    
  13.     ping -c 1 -n -w 1 $host | grep "1 received" > /dev/null
  14.     result=$?
  15.     [ $result != 0 ] && status=down
  16.    
  17.     echo
  18.     echo -n "Waiting for host \"$host\" to come online: "
  19.    
  20.     while true; do
  21.         ping -c 1 -n -w 1 $host | grep "0 received" > /dev/null
  22.         result=$?
  23.        
  24.         echo -n "."
  25.         [ $result == 1 ] && { sleep 1; cnt=$((cnt+1)); }
  26.    
  27.        
  28.         if [ $result == 1 ] && [ status == "up" ]; then
  29.             continue
  30.         elif [ $result == 0 ]; then
  31.             status=down
  32.         fi
  33.        
  34.         [ $result == 1 ] && [ $status == "down" ] && {
  35.             sleep 1
  36.             echo -e "\nHost \"$host\" is back online."
  37.             break
  38.         } || [ $cnt -ge $timeout ] && {
  39.             echo -e "\nRemote host \"$host\" did not reboot.\n"
  40.             break
  41.         }
  42.        
  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