Wake On Wan
I’ve got a MythTV backend running for recording my favorite shows. This PC isn’t online for most of the time, as it automatically powers on when it has a show to record. Sometimes however I’m not at home and I want to schedule a recording. This can be fixed easily by installing mythweb on the backend, forwarding port 80 (TCP) to the box and let it stay on forever. Personally I don’t find this a good solution, because the backend would be mostly powered on for doing nothing.
Another approach would be to use WOL (Wake On Lan), however this only works when the PC you’re sending it from is on the same subnet as the host you’re trying to wake. So this doesn’t solve my problem either.
Guess it’s time to do some creative scripting to be able to wake the backend from the Internet. You can accomplish this using a dozen of ways, but I’ve did it the following way: run a lightweight webserver on my thintune (or DD-WRT enabled router) with PHP support and the wakeonlan binary.
Excited ? Lets start …
Install and configure Dynamic DNS
As I don’t have a static IP-address, I’ve to get the current one assigned from my ISP in order to be able to access the webpage on my thintune. Luckely there’s this free service named DynDNS (Dynamic DNS) which resolves a hostname to an IP. Therefore you need to register on their site, and install a tool called ddclient to update the information on the DynDNS servers whenever your IP changes.
apt-get install ddclient
Enter all the necessary information requested, and afterwards open up your favorite editor to edit the file /etc/ddclient.conf and make sure it looks like this:
pid=/var/run/ddclient.pid
protocol=dyndns2
use=web, if=eth0
server=members.dyndns.org
login=your_username
password=’your_password’
your_hostname.your_dyndns_domain.com
The only thing I had to change was the ‘use=’ part. If you set it to web, the program will parse this page to determine your IP-address and send it to the DynDNS servers.
Install and configure the webserver
I’ve chosen to use the lighttpd server because it’s really lightweight and doesn’t use much diskspace. It supports CGI scripting and htpasswd authentication, which is basically all we need.
apt-get install lighttpd php5-cgi apache2-utils
The configuration file for lighttpd is located at /etc/lighttpd/lighttpd.conf and looks like this:
server.modules = (
“mod_access”,
“mod_alias”,
“mod_accesslog”,
“mod_fastcgi”,
“mod_auth”,
)
server.document-root = “/var/www/”
server.errorlog = “/var/log/lighttpd/error.log”
index-file.names = ( “index.php”, “index.html”,
“index.htm”, “default.htm”)
accesslog.filename = “/var/log/lighttpd/access.log”
url.access-deny = ( “~”, “.inc” )
server.pid-file = “/var/run/lighttpd.pid”
dir-listing.encoding = “utf-8″
server.dir-listing = “enable”
server.username = “www-data”
server.groupname = “www-data”
$HTTP["remoteip"] =~ “127.0.0.1″ {
alias.url += (
“/doc/” => “/usr/share/doc/”,
“/images/” => “/usr/share/images/”
)
$HTTP["url"] =~ “^/doc/|^/images/” {
dir-listing.activate = “enable”
}
}
include_shell “/usr/share/lighttpd/create-mime.assign.pl”
include_shell “/usr/share/lighttpd/include-conf-enabled.pl”
auth.backend = “htpasswd”
auth.backend.htpasswd.userfile = “/var/www/htpasswd”
auth.require = ( “/” =>
(
“method” => “basic”,
“realm” => “thintune”,
“require” => “valid-user”
)
)
Because we’ve edited the config file, we need to restart the daemon:
/etc/init.d/lighttpd restart
By using mod_auth you’re able to password protect your webpages so unwanted visitors won’t be able to turn on your machines. The htpasswd file located at /var/www/htpasswd contains a list of users who are allowed to view the pages and their encrypted passwords. You create the file like this:
htpasswd -c /var/www/htpasswd _username_
Now the webserver is completely configured. The only thing we have to do is enable the CGI module by issueing:
lighty-enable-mod cgi
Note:
Because I’m running this webserver on a read-only compact flash card (see here), I had to add these lines to /etc/init.d/bootclean (before the line stateing [ -f /tmp/.clean ] && [ -f /var/run/.clean ] && [ -f /var/lock/.clean ] && exit 0):
kdir /var/log/lighttpd 2>/dev/null
touch /var/log/lighttpd/access.log
touch /var/log/lighttpd/error.log
chown -R www-data:www-data /var/log/lighttpd/
chmod 755 /var/log/lighttpd
The actual codeing
For the scripting part we need to create 4 files and install 1 binary. The scripting is quite simple and needs DHCP reservations in order to work properly (meaning that every PC always gets the same IP-address). For the actual wakeing of other PC’s we need the wakeonlan binary.
apt-get install wakeonlan
Store the following files in your /var/www folder: checkonline.sh, online.php and wake.php. These are the scripting files.There is one config file necessary to map the MAC-addresses with hostnames and IP-addresses. I’ve called this file maclist (also located in /var/www) and mine looks like this:
192.168.0.5 Devastator 00:19:64:51:15:75 192.168.0.50 Mythtv 00:12:b2:4B:CA:03
You can see your MAC- and IP-address with the ifconfig tool:
/sbin/ifconfig
Output on Devastator:
eth0 Link encap:Ethernet HWaddr 00:19:64:51:15:75 inet addr:192.168.0.5 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe19:ef3d/64 Scope:Link …
Only drawback of this script is that it checks if the hosts are online when the page is requested, so the user needs to wait for the script to finish before the page is loaded. This way you always have the most up-to-date info, but the page loads slow. Alternatively you could execute the script from a cronjob and send it’s output to a file, which you read from on the website.
Preparing linux hosts for Wake On Lan (WOL)
For the target host to support WOL, it needs to be enabled in the bios AND in the operating system. There’s a nice litte program which does this for us: ethtool. Just add the following line to /etc/rc.local (before the exit 0 line):
ethtool -s eth0 wol g
That’s it ! Now you can fire up your webbrowser and point it to: http://your_webserver_hostname/online.php.
Happy wakeing !

Nice post Kristof. i’ve been fooling around with a sh script from a qnap nas device but that requires me to ssh in. be much better to do it from a webpage.
when i try and view your php files however, the browser tries to execute them.
would you mind linking them to a .txt file with the code intact?
cheers and once again, nice post.
Hey Kristian, glad you like my post …
Funny I’ve looked over the php pages, which obviously get interpreted by the server …
I’ll fix it right away !