Advertisement
killermist

shutdown.sh

Nov 24th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.34 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. IPGROUP="192.168.1.35 192.168.1.36 192.168.1.2"  # any number, space delimited
  4. IPCOUNT="3"   # number of machines being tested
  5. MAXFAILS="5"    # Total number of fails before shutdown
  6. INTERVAL="10"   # time in seconds between tests
  7. #If MAXFAILS is 5 and INTERVAL is 10, then you're looking at about a minute
  8. # of all machines being down before the NAS powers off
  9. COUNT="0"       # reset COUNTer
  10. UPMACHINES="0"  # pass/fail indicator
  11. DEBUG="1"       # 1 for DEBUGging, 0 for not
  12.  
  13. while [ "$MAXFAILS" != "$COUNT" ];
  14. do
  15.   if [ "1" == $DEBUG ]; then echo "MAXFAILS=$MAXFAILS  COUNT=$COUNT"; fi
  16.   UPMACHINES=$IPCOUNT     # Reset indicator
  17.   if [ "1" == $DEBUG ]; then echo "UPMACHINES=$UPMACHINES"; fi
  18.   for IP in $IPGROUP; do # This is where the space delimiting expands to multiple parameters
  19.   #  The variable $IPGROUP is not quoted for a reeason, do not "correct" it
  20.     /sbin/ping -c 1 "$IP"
  21.     if [ $? != "0" ]; then
  22.       UPMACHINES=$((UPMACHINES-1)) #decrease number of running machines detected
  23.       if [ "1" == $DEBUG ]; then echo "IP=$IP  UPMACHINES=$UPMACHINES"; fi
  24.     fi
  25.   done
  26.   if [ 0 == $UPMACHINES ]; then COUNT=$((COUNT+1)); else COUNT="0";  fi
  27.   # If all IPs failed, increment COUNTer, otherwise, zero it
  28.   sleep $INTERVAL
  29. done
  30. echo "Shutting down. no response(s) from any of $IPGROUP"
  31. /sbin/shutdown -p now
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement