Advertisement
Uno-Dan

Wait for host update

Jun 18th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.96 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.    
  10.     [ "$2" == "" ] && timeout=90 || timeout=$2
  11.    
  12.     ping -c 1 -n -w 1 $host | grep "1 received" > /dev/null
  13.     result=$?
  14.     [ $result != 0 ] && status=down || status=up
  15.    
  16.     echo
  17.     echo -n "Waiting for host \"$host\" to come 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.         cnt=$((cnt+1))
  25.         [ $result == 1 ] && sleep 1 || status=down
  26.        
  27.         [ $result == 1 ] && [ $status == "down" ] && {
  28.             sleep 1
  29.             echo -e "\nHost \"$host\" is back online."
  30.             break
  31.         } || [ $cnt -ge $timeout ] && {
  32.             echo -e "\nRemote host \"$host\" did not reboot.\n"
  33.             break
  34.         }
  35.     done
  36. }
  37.  
  38. echo
  39. ssh root@$HOST reboot
  40.  
  41. wait_for_host $HOST $TIMEOUT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement