Wake on LAN script

I'm trying to make a script that wakes a server on the same LAN when openwrt boots. Should be fairly easy but couldn't find any well documented examples and I'm kinda clueless on this type of thing.

The server is protected by a ups, while this openwrt access point isn't. So when there's a power outage, the server gets shut down by the ups and stays shut, while openwrt turns back on soon as power returns.

The idea is to have it run a script at boot that keeps sending the magic packet every minute till it gets a ping response from the server and then stop. How hard can it be, right?

Would appreciate any assistance.

Call the script from /etc/rc.local
Run it in the background using the ampersand (&) symbol.

#!/bin/sh

IP=192.168.1.100         #IP to ping
MAC=11:22:33:44:55:66    #MAC to wake up 
ifname=br-lan            #Iface from which to send WOL packets
timeout=60

while true; do
 if ping -c 3 -W 1 $IP >/dev/null; then
  break
 else
  etherwake -i $ifname $MAC
 fi
sleep $timeout
done
4 Likes

Worked beautifully, thank you.

Just gonna add a couple things I discovered in the process, to help others that might find this thread in the future.

If you copy paste and save this script with any regular editor, it might not run because of different ASCII or whatever (I was getting -ash: not found). If it ain't working, ssh into the device and vi the script to be sure it looks right.

And if there's vlans involved in your setup, br-lan might not work for sending the magic packet. br-lan.X (x=vlan) should work in this case.

Other than that, just save this script in a file anywhere, chmod +x and call it from rc.local with the full path and an ambersand. So if you saved it as wol.sh in /root, the call in rc.local would look like that

/root/wol.sh &
exit 0

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.