Wake on LAN Script

Posted by

Any Linux user knows that the command line, however intimidating it looks to some people, is the most powerful and efficient way to accomplish a task.

For Wake on LAN in Linux despite all my search to find a modern GUI application that integrates well with GNOME (my Desktop Environment of choice) I came up empty handed.

So, what I ended up using was a Linux bash script that does everything I want through the command line.

It uses the WOL command to provide Wake on LAN functionality in Linux.

Installation

You need to have the WOL package installed in your Linux system.

If you are using Ubuntu or one of its derivatives you can use the following command:

sudo apt-get install wol

If you are on an Arch based distribution you can get the package from the link below.

Then you just need to build it from source. Extract the tar.gz archive, cd into the directory and run:

make
sudo make install

Now that WOL is installed in your system we need to find where the command is placed. Just run:

whereis wol

Depending on your Linux distro this will either be installed in /usr/bin or in /usr/local/bin

Now it’s time to edit the script to your needs. Copy the script below and save it on your computer. If you are doing this in a text editor make sure that no extension (i.e *.txt) is saved.

#!/bin/bash

# Definition of MAC Addresses (Change Device Name and MAC Address)
DEVICE1=MAC ADDRESS 1
DEVICE2=MAC ADDRESS 2

# Edit echo lines to reflect what you want to do. Edit command path and port number.
while [ "$input1" != quit ]; do
echo "Which PC do you want to wake?"
echo ""
echo "a) Device 1"
echo "b) Device 2"
echo "d) Wake Device 2, wait 15sec, then wake Device 1"
echo "e) Quit & Exit"
echo ""
echo "Enter Selection: "
read input1
  if [ $input1 == a ]; then
  /usr/bin/wol -p 7 $DEVICE1
  exit 1
fi

if [ $input1 == b ]; then
  /usr/bin/wol -p 9 $DEVICE2
  exit 1
fi

if [ $input1 == d ]; then
  /usr/bin/wol -p 9 $DEVICE2
  echo "Device 2 sent, now waiting for 15sec then waking Device 1"
  sleep 15
  /usr/bin/wol -p 7 $DEVICE1
  exit 1
fi

if [ $input1 == Q ] || [ $input1 == q ]; then
echo "Exiting!"
exit 1
fi

done
echo  "Quit!!!"

First thing you need to do is edit the command path to reflect where WOL was installed in your system as above.

Then you need to input the MAC addresses and the correct ports of the devices you are waking up using the script. You can also name your devices for easy reference.

You can add or remove sections for devices accordingly.

Last step is make sure that the script is executable. Just run:

chmod +x script_name

Now you can use this script to wake up devices (you need of course to have Wake on LAN functionality enabled on these devices. See this post here for more details) in your network.

If you have a Raspberry Pi running in your home network (as a VPN or for any other task) you can transfer this script onto your Raspberry Pi and run it through SSH to wake up devices in your network while you are outside your network. 

You can find SSH clients for your Android/iOS device which you can install to enable you to access your Raspberry Pi remotely and run terminal commands such as this script.

To enable SSH access to gain remote access on a network machine you need to have a read through these posts if you are not sure how any of this works:

Remote Access
Port Forwarding
DDNS
Static IP

You can do lots of cool things with this script. If you have a PC in your home that you run PLEX Media Server on but don’t want to leave it running 24/7 you can configure a Raspberry Pi with remote SSH access and have this script on it. You can then connect using an SSH client on your phone, run the script to wake up the PC running the PLEX Media Server and enjoy your media when you are outside your house using the PLEX app on your phone. Once you finish you can then Remote Access your PC and shut it down to save power.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments