Automatically Wake NAS on Access

Rabid picture Rabid · Jun 6, 2012 · Viewed 8.2k times · Source

I'm trying to get my NAS server to wake from an S3 sleep state when the NAS is accessed by a user. I want to do this to increase the longevity of the server, and also to limit its power usage. I've seen people asking for similar advice but none that I found provide a robust solution, most threads just ended unanswered.

So to just detail my problem quickly: At my home I have a custom built, from an old PC, NAS server, running Ubuntu server, which stores media and documents mainly. This server is currently set to sleep after a predefined inactive period. Currently the NAS can be brought out of the S3 state with a WOL magic packet. What I would like to achieve is that this magic packet is automatically sent to the server when a user accesses one of the shares from their PC. The users are primarily running windows 7. I'm not sure if this is entirely prevalent but I have a Linksys WRT54G running DD-WRT as my home router/DHCP/DNS.

During my research I came across many articles which just automatically woke the server on a timed loop, no real intelligence. The article given below seems to do what I want:

http://wdtvhd.com/index.php?showtopic=7908

A script is given which attempts to address this problem by using the DD-WRT router to send the wake-on-lan packets when a query is made. This seems like a good way to go about this, however I could not get the script given in the link to operate correctly.

I think that covers most aspects of my problem. Any assistance will be greatly appreciated.

Answer

Rabid picture Rabid · Aug 16, 2012

Just thought I would post my eventual solution to the above mentioned problem. To solve the problem I wrote a custom script which runs on my dd-wrt router on a timed cron job. When this script runs it access the file

\proc\net\arp

Within in that file is stored a record of all currently leased IP address and the corresponding mac addresses. Thus my script compared the mac addresses stored within that file to a predefined list of mac address of PCs on my home network. The list is comprised only of the PCs I would like to be able to access the NAS server. If one of the PCs is found to have an active lease the router then sends the wake-on-lan magic packet. Which then wakes the server. At that point I thought I had achieved my goal, as the server switched on with any of the PCs on the network, and the wait was not too long. However, after completing this I found that my timed sleep for the server would initiate every 30 min or so and sleep the server only to be woken again a couples of seconds later.

So to solve that issue I just added another condition to my conditional statement that would sleep the server if none of the required PC had an active lease. To do this I used SSH and the built in dropbear ssh functionality of DD-WRT to sleep the server. Below is the script

#!/bin/ash

NAS="MA:CA:DD:RE:SS:00"

PC="MA:CA:DD:RE:SS:00"

varP='grep -o $PC /proc/net/arp'

while true
do
    echo 'Entered Loop'
    if ping -c 1 IPADDRESSOFNAS > /dev/null; then
            echo 'NAS is Already ON'

    if [[ "$varP" != "MA:CA:DD:RE:SS:00" ]]; then
                    echo 'All Hosts Offline'
                    echo IPADDRESSOFNAS  ssh-rsa NASPUPLICKEY
                    #HOME=/temp/root/
                    DROPBEAR_PASSWORD='NASPASSWORD' ssh root@IPADDRESSOFNAS pm-suspend &
            fi
            exit
    fi

    if [[ "$varP" == "MA:CA:DD:RE:SS:00" ]]; then
            echo 'waking from lan'
            /usr/sbin/wol -i BROADCASTADDRESSOFNETORK -p 9 MA:CA:DD:RE:SS:00
            /usr/sbin/wol -i BROADCASTADDRESSOFNETORK  -p 9 MA:CA:DD:RE:SS:00
            exit
    fi
    exit
done

DISCLAMER: The code is supplied as is. I am aware it is not pretty nor the best solution possible. But it works for me and thats all I really need.

Hope someone finds this useful!