Linux: Using Remote Wakeup (Wake on LAN)

Here is the scenario, you are an independent IT consultant and/or an administrator of some business IT infrastructure. The systems you manage are a mix of Linux and Microsoft desktop and server systems. You do much of your system updates and other management tasks after hours using remote access over VNC or a VPN so you can be home with your family. The upper management, also know as “suits”, at your location has decided that shutting down PC desktop systems after hours is a great cost savings measure and tells you to implement a plan to do this. The suits also want the desktop PC systems to be up and running when employees arrive in the mornings so time is not wasted while people wait for their PC to start up. How do you do all this and still give yourself that time at home at night when you need to do those after hours management tasks? Enter Remote Wakeup, otherwise known as Wake on LAN.

I was presented with a challenge like this for one of our charity organization clients that is trying to cut costs as much as possible. The idea originally was that each PC will shut down each night after running its nightly backup. Then each morning the systems would be restarted by the users when they came in. Since most of the support provided by my company happens after hours this meant we needed to implement Wake on LAN so we could still provide that after hours support while giving the client the cost savings from shutting down the PC systems overnight. To do this we use the ‘wakeonlan’ Perl application with a mix of cron job and hand created scripts to wake up PC systems as needed. Each PC that needs to be awakened has its BIOS set with Wake on LAN enabled. One Linux PC system is set up as the “master” system for managing Wake on LAN and that PC is never shut down. That one PCs BIOS is configured to auto-restart that PC after a power outage so it will always be available.

Another hitch in this request is that some of the Windows desktop users do need access from home either after hours or over weekends. These people are completely unfamiliar with Linux and so need to be given an easy way to access the ‘wakeonlan’ capability of the Linux PC that handles this. This is accomplished by giving them PuTTY on Windows at home with a saved session that logs them into an account on the Linux PC over SSH. From there they just type ‘wol’ and are given a menu from which they can choose the PC they need to “wake up”. Here is a copy of the ‘wol’ script as it exists today:

#!/bin/bash
# wol script by ERA Computers & Consulting www.eracc.com
clear
wolmenu="wol.menu"
woldata="wol.data"
wolloc="`dirname \"$0\"`/"
if [ ! -f $wolloc$wolmenu ]
then
     echo Cannot find my menu file. My files should be in $wolloc.
     elif [ ! -f $wolloc$woldata ]
     then
          echo Cannot find my data file. My files should be in $wolloc.
     else
          cat $wolloc$wolmenu
          echo;echo Type the number of the PC to awaken or c to cancel and press Enter:
          read n
          case $n in
               c) exit;
               ;;
               C) exit;
               ;;
               *) echo Waking up `grep ^$n $wolloc$wolmenu`.;
               ipsubnet=`grep ^$n $wolloc$woldata|cut -d ' ' -f 3`;
               hwaddress=`grep ^$n $wolloc$woldata|cut -d ' ' -f 2`;
               echo The command running is - wakeonlan -i $ipsubnet $hwaddress;
               wakeonlan -i $ipsubnet $hwaddress;
               ;;
     esac
fi

There is more that could be done to check for invalid input and to check to see when a PC starts responding to pings, but this serves our needs just fine as it is written. The wol.menu and wol.data files contain the information needed to present the users with a selection and then to take the selection and send a wakeup signal to the selected hardware address on the LAN. Here is the menu structure:

Number PC Name             HW Address         IP Address
====== =================== ================== ================
1      ACCOUNTING          00:11:22:33:44:50  192.168.1.10
2      FINANCE             00:11:22:33:44:51  192.168.1.11
3      MANAGER             00:11:22:33:44:52  192.168.1.12

Here is the data file that corresponds with the menu:

1 00:11:22:33:44:50 192.168.1.255
2 00:11:22:33:44:51 192.168.1.255
3 00:11:22:33:44:52 192.168.1.255

Yes, I know we could grab the data direct from the menu using some tool other than ‘cut’. However, what is done here works even though it is not as elegant as some would like. If some of you with elite bash scripting skills would like to share how to do this with just the menu, please do so in a comment.

The one other item we need to address is waking up all the PC systems before the employees arrive in the morning on Monday through Friday each week. This is done in a cron job on the same Linux PC. Here is how the job might be set up:

30 6 * * 1-5 wakeonlan -i 192.168.1.255 -f /home/user/scripts/autowol.data

What this does is tell the cron scheduler to run the command “wakeonlan” at 6:30 AM (“30 6”) every Monday through Friday. (“1-5”) This command reads the hardware addresses from a file (“-f /home/user/scripts/autowol.data”) and then sends the wakeup signal to each address on the chosen subnet (“-i 192.168.1.255”). The hardware address data file looks like this:

00:11:22:33:44:50
00:11:22:33:44:51
00:11:22:33:44:52
00:11:22:33:44:53
00:11:22:33:44:54

It contains all the hardware addresses of the PC systems that need to be awakened at that time of day. One hardware address per line.

So, if you are an IT consultant, systems administrator or Joe User who wants to use Linux at home, perhaps this article gave you some idea of how to manage your own Remote Wakeup scenario.

free hit counter
free hit counter

Notice: All comments here are approved by a moderator before they will show up. Depending on the time of day this can take several hours. Please be patient and only post comments once. Thank you.

Share

Published by

Gene A.

Gene is a "Unix Guy", network technologist, system trouble-shooter and IT generalist with over 20 years experience in the SOHO and SMB markets. He is familiar with and conversant in eComStation (a.k.a. OS/2), DOS (PC, MS and Free), Unix, Linux and those GUI based systems from Microsoft. Gene is also a follower of Jesus (forgiven, not perfect), and this does inform his world view.

7 thoughts on “Linux: Using Remote Wakeup (Wake on LAN)”

  1. By the way, if you provide an example of how to better scripts shown on our site please provide a detailed explanation of any regular expressions used. This is so newbies can be assisted with getting a grip on regex. Why? I think regex is one of the more difficult tools for newbies to grasp. Only explanations from those who “grok” regex can help some people understand it.

  2. I like it!

    Our shop has a medium sized call center. Our fall project is going to be a linux migration. I’m going to propose WOL/Readonly NFS root/Single OS Instance using a NetApp filer cluster.

    We did this very thing ages ago at Johnson Controls for Sun Ultra5 workstations that ran CATIA and IDEAS. We didn’t do WOL though.

    Funny how the same concepts and implementations keep coming back around.

    Thanks,
    /Chris C

  3. I think this is a really awesome post. Not only is this a very, very common scenario, but it’s become increasingly necessary to save on the vast amount of useless energy expended unnecessarily by networks. One enigma I’ve been thinking about is – how to do this with a Linux PC using port forwarding and any ordinary TCP | UDP packet, eg :
    remote -> no-ip.com -> MyPc (in suspend) 192.168.0.1:80 => WOL ?

    Go figure – any comments welcome 🙂

    RM

    1. Rob M, thank you for reading.

      You pose an interesting question. The “magic packet” for WoL is typically sent on port 9, the discard port. Frankly, I am not sure one can or should use it across the internet.

      Since it appears your PC in question is directly connected to the internet then it should be running as a firewall and router. In my experience a firewall / router PC should be the one PC that does not “go to sleep”. You could use that PC to awaken other PC systems on your LAN. But you do not want your router going into hibernate / suspend if you need remote access.

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow the directions below to post a comment if you are human. After 3 failed tries reload the page to start with new images.