Advertisement
Guest User

autowake_NAS

a guest
Jan 17th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.97 KB | None | 0 0
  1. #!/bin/sh
  2. #############################################################################
  3. # Script aimed at waking a (NAS) computer according to various conditions.
  4. # - If at least one of a configurable set of devices is online
  5. # - Unless a curfew timeslot is defined and the current time is
  6. #   within this slot
  7. # - Every day at configurable time
  8. #
  9. # This script was developped to be executed on a DD-WRT router.
  10. # The script shall be launched at router startup
  11. #
  12. # Configuration of the script:
  13. # - This script does not accept any argument when calling it
  14. # - The behaviour of the script can be configured by changing the value
  15. #   of the variables in capital letters below
  16. #
  17. # Author: fritz from NAS4Free forum
  18. #
  19. #############################################################################
  20.  
  21. # script configuration
  22. POLL_INTERVAL=5                         # number of seconds to wait between to cycles
  23.  
  24. BROADCAST_IP="100.200.1.255"            # Broadcast IP address of the network
  25. NAS_IP="100.200.1.108"                  # IP address of the NAS to wake
  26. NAS_MAC="F4:6D:04:AE:79:24"             # MAC address of the NAS to wake
  27. WOL_PORT="9"                            # Usually 7 or 9
  28.  
  29. # IP addresses of the devices to be polled, separated by a space character)
  30. # (Attention: The devices to be polled shall have static IP addresses!)
  31. # The script will ensure that the NAS is awake, whenever one of the
  32. # devices having the latter IP addresses is online
  33. #
  34. # The devices will be polled only if CHECK_ONLINE equals 1
  35. CHECK_ONLINE="1"
  36. IP_ADDRS="100.200.1.112"
  37.  
  38. # Curfew:
  39. # Time of the day at which the NAS shall not be woken up
  40. # (even if some of the above mentionned devices are online)
  41. #
  42. # The curfew time exists if CHECK_CURFEW_ACTIVE equals 1
  43. CHECK_CURFEW_ACTIVE="1"
  44. BEG_POLL_CURFEW="23:00"
  45. END_POLL_CURFEW="6:00"
  46.  
  47. # Time of the day at which the NAS shall always by awake
  48. # (even if none of the above mentionned devices are online)
  49. # This may be required in order to ensure that the NAS performs
  50. # some administrative tasks (e.g. backup)
  51. # format: "hh:mm"
  52. #
  53. # Wake time is taken into account if CHECK_WAKE_TIME equals 1
  54. CHECK_WAKE_TIME="1"
  55. WAKE_TIME="6:00"
  56.  
  57. # Used utilities
  58. DATE="/bin/date"
  59. PING="/bin/ping"
  60. WOL="/usr/bin/wol"
  61.  
  62. # Constants
  63. NB_SEC_IN_DAY="86400"
  64.  
  65. # Initialization of variables
  66. next_wake_timestamp=`$DATE -d "2030-01-01 00:00" +%s`
  67.  
  68.  
  69.  
  70. ##################################
  71. # Returns true if the current time is within the timeslot
  72. # provided as parameter.
  73. #
  74. # Param 1: Start of the timeslot (format: "hh:mm")
  75. # Param 2: End of the timeslot (format: "hh:mm"). If End=Beg,
  76. #           the timeslot is considered to last the whole day.
  77. # Return: 0 if the current time is within the timeslot
  78. ##################################
  79. isInTimeSlot() {
  80.  
  81.         local startTime
  82.         local endTime
  83.         startTime="$1"
  84.         endTime="$2"
  85.  
  86.         local nbSecInDay
  87.         nbSecInDay="86400"
  88.  
  89.         local currentTimestamp
  90.         local startTimestamp
  91.         local endTimestamp
  92.  
  93.         currentTimestamp=`$DATE +"%s"`
  94.         startTimestamp=`$DATE -d "$startTime:00" +"%s"`
  95.         endTimestamp=`$DATE -d "$endTime:00" +"%s"`
  96.  
  97.         if [ "$endTimestamp" -le "$startTimestamp" ]; then
  98.                 if [ "$currentTimestamp" -gt "$startTimestamp" ]; then
  99.                         endTimestamp=`expr $endTimestamp + $nbSecInDay`
  100.                 else
  101.                         startTimestamp=`expr $startTimestamp - $nbSecInDay`
  102.                 fi
  103.         fi
  104.  
  105.         if [ "$currentTimestamp" -gt "$startTimestamp" -a "$currentTimestamp" -le "$endTimestamp" ]; then
  106.                 return 0
  107.         else
  108.                 return 1
  109.         fi
  110. }
  111.  
  112.  
  113.  
  114. while true
  115. do
  116.  
  117.         # Wait until next poll
  118.         sleep $POLL_INTERVAL
  119.  
  120.         echo "---------------------"
  121.  
  122.         # Check if the NAS is already awake
  123.         if ping -c 1 -t 1 $NAS_IP > /dev/null; then
  124.                 echo "NAS is already ONLINE"
  125.                 continue
  126.         else
  127.                 echo "NAS is OFFLINE"
  128.         fi
  129.  
  130.         # Initialize variable stating if NAS shall be waken
  131.         wake_nas="0"
  132.  
  133.         # Check if any device requiring the NAS to be awake is online
  134.  
  135.         # Only wake NAS if we are not within the curfew timeslot        
  136.         isInTimeSlot "$BEG_POLL_CURFEW" "$END_POLL_CURFEW"
  137.         inTimeSlot=$?
  138.         if [ $CHECK_CURFEW_ACTIVE -eq "0" -o $inTimeSlot -eq "1" ]; then
  139.                 if [ $CHECK_ONLINE -eq "1" ]; then
  140.                         for ip_addr in $IP_ADDRS; do
  141.                                 if $PING -c 1 -t 1 $ip_addr > /dev/null ; then
  142.                                         wake_nas="1"
  143.                                         echo "Online device detected: $ip_addr"
  144.                                         break # Checking the other devices skipped to save time
  145.                                 fi
  146.                         done
  147.                 fi
  148.         else
  149.                 echo "Within curfew timeslot"
  150.         fi
  151.  
  152.         # Check if it is time to wake NAS
  153.         if [ $CHECK_WAKE_TIME -eq "1" ]; then
  154.                 # Record current time
  155.                 current_timestamp=`$DATE +%s`
  156.  
  157.                 # Check if wake time is reached
  158.                 if [ $next_wake_timestamp -le $current_timestamp ]; then
  159.                         wake_nas="1"
  160.                         echo "Time to wake up !!!"
  161.                 fi
  162.  
  163.                 # Compute next wake timestamp
  164.                 next_wake_timestamp=`$DATE -d "$WAKE_TIME" +%s`
  165.                 # If the next wake time is tomorrow
  166.                 if [ $next_wake_timestamp -le $current_timestamp ]; then
  167.                         next_wake_timestamp=`expr $next_wake_timestamp + $NB_SEC_IN_DAY`
  168.                 fi
  169.         fi
  170.  
  171.         # Wake NAS if required
  172.         if [ "$wake_nas" -eq "1" ]; then
  173.                 echo "Waking NAS"
  174.                 $WOL -i $BROADCAST_IP -p $WOL_PORT $NAS_MAC
  175.         else
  176.                 echo "No need to wake NAS"
  177.         fi
  178. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement