Running Your Raspberry Pi As A Wake-On-LAN Server

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Setting up a Setting up a Crea


Raspberry Pi Valheim own
Scanner… Server on t… berr
Server using Raspberry Pi Team

Running your
Raspberry Pi as a
Wake-on-LAN Server
by Emmet

 Published Aug 08, 2023


 Beginner  Network  Servers

   

In this tutorial, we will show you how to use


your Raspberry Pi to send a Wake-on-LAN
(WoL) packet to any connected device.

Wake-On-LAN is a functionality that allows you


to wake a device by sending a particular packet
over the network.

While the Raspberry Pi does not support


receiving Wake-on-LAN packets, it is more
than capable of sending them.

There are various ways you can utilize this


functionality. In our network, we have a
Raspberry Pi set up to power back on a NAS
after a power outage has occurred. While both
are plugged into a UPS, it allows the more
power-hungry NAS to safely shut down and be
powered back on when everything is safe.

To send Wake-on-LAN packets from your


Raspberry Pi, we will be using a piece of
software called etherwake.

Etherwake allows your Raspberry Pi to act as a


Wake- on-LAN server and send packets to
other devices.

search text

Equipment

Below is a list of equipment we used when


setting up our Raspberry Pi as a Wake-on-LAN
server.

Recommended

 Raspberry Pi

 Micro SD Card

 Power Supply

 Ethernet Cable or Wi-Fi

Optional
 Raspberry Pi Case

 USB Mouse

 USB Keyboard

 HDMI Cable

 Monitor

This tutorial was last tested on a Raspberry Pi


400 running the latest version of Raspberry Pi
OS Bullseye.

Preparing your Devices to


Accept Wake-on-LAN

Before you begin, you will want to set up your


other devices so they can wake when sent a
Wake-on-LAN packet from your Raspberry Pi.

These steps will significantly differ from device


to device, so we will not cover this in this guide.
The best way to set up Wake-on-LAN is
through your device’s BIOS. This will allow you
to boot the device even if it has been powered
off.

Once you have Wake-on-LAN enabled on your


target device, you must also know its MAC
address so that you can send the message to
it.

Most routers will reveal these MAC addresses if


you browse their device list.

Setting your Raspberry Pi


up as a Wake-on-LAN
Server

The following steps will walk you through


setting up your Raspberry Pi to work as a
Wake-on-LAN Server. We will achieve this by
installing a free piece of software called
Etherwake.

Installing Etherwake to the


Raspberry Pi

To run your Raspberry Pi as a Wake-on-LAN


server, we will be relying on the etherwake
software. The next few steps will be incredibly
straightforward as this software is available
through the package repository.

1. Before installing Etherwake to our Raspberry


Pi, we must update the package list and the
packages themselves.

Update your Raspberry Pi’s operating system


by using the following two commands.

Terminal $  Copy

sudo apt update


sudo apt upgrade -y

2. Once the update completes, we can install


the Etherwake software using the following
command.

Terminal $  Copy

sudo apt install etherwake

Using Etherwake on your


Raspberry Pi to Wake-on-LAN

3. Once you have etherwake installed on your


Raspberry Pi you can put it to use and send
Wake-on-LAN packets.

For this to work however, you will need to know


the MAC address of the device you want to
wake. A MAC address is used to identify
network adapters, so your Wi-Fi and Ethernet
for example will have different MAC addresses.

You may continue to the next step once you


have a MAC address ready.

4. To send a Wake-on-LAN packet from your


Raspberry Pi, you only need to use “ etherwake
” followed by the “ -b ” option, and finally, the
MAC address.

The “ -b ” option tells etherwake to broadcast


the Wake-on-LAN packet to the specified
address.

Replace With >  Copy

sudo etherwake MACADDRESS

5. Now that you know how to etherwake, you


can SSH into your Raspberry Pi to wake up
various devices on your network.

Just ensure that any device you want to wake


has its “ Wake-on-LAN ” functionality enabled.
Not all devices will support this.

Keeping a Device Awake


Using Your Raspberry Pi
Wake-on-LAN Server

It is possible to use your Raspberry Pi’s Wake-


on-LAN server to keep a device awake.

We achieve this by writing a short script that


pings a specified IP Address and, once it drops
off, sends a Wake-on-LAN packet using the
etherwake software we installed to our
Raspberry Pi.

Preparing your Raspberry Pi

1. Since we will be writing our script in Python,


we must start by ensuring the interpreter is
installed on the system.

Install Python by using the following command


on your Raspberry Pi.

Terminal $  Copy

sudo apt install python

Writing a Wake-on-LAN Script


on your Raspberry Pi

2. Once installed, let us write a Python script


called “ wake_on_lan_server.py ” by using the
following command.

By using the tilde ( ~ ) symbol in front of the


filename, we will save this into your users home
directory.

Terminal $  Copy

nano ~/wake_on_lan_server.py

3. Now, within this file, enter the following


lines of code. We will explain each section as
we go so you can understand how it works.

Add >  Copy

import subprocess, time

With this first line we import two packages that


we will be utilizing within the script.

subprocess – This package will allow us


to run programs outside of Python. Using
this package, we can use the Etherwake
software on our Raspberry Pi to send
Wake-On-LAN packets to your devices.

time – The time package will allow us to


pause the script between loops to prevent
it from flooding the device.

Add >  Copy

def main():

We now define our “ main() ” function. When


the script is run, this function will be called and
will contain our main loop.

Add >  Copy

while True:

Next, let us define an infinite loop by using “


while ” followed by “ True “.

Add >  Copy

is_alive = subprocess.call([

Next, on every loop, we want to call the “ ping ”


command and store the result in the “ is_alive
” variable. Ensure that you replace “
<IPADDRESS> ” with the machine’s IP you want
to awake.

Add >  Copy

if not is_alive:

We now create a new conditional block where


we check whether the “ is_alive ” variable is
not true. After this point you could extend the
script further by checking additional things
such as the status of a UPS before you send
the wake command.

Add >  Copy

print("Sending Wake Comm


subprocess.Popen('/usr/s

If the system is not responding to a ping, we


start by printing the text “
Sending Wake Command ” to the command line.

You might also like