Skip to content

Instantly share code, notes, and snippets.

@felixhummel
Last active February 8, 2024 21:16
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save felixhummel/6334e04e00e565b6bae8 to your computer and use it in GitHub Desktop.
Save felixhummel/6334e04e00e565b6bae8 to your computer and use it in GitHub Desktop.
openWRT automatic Wake on LAN

To make iptables work, make sure that you have netfilter installed:

opkg install kmod-br-netfilter

Thanks, @Reutertu3

#!/bin/sh
# Inspired by https://wiki.openwrt.org/doc/howto/auto_wake_on_lan
# - code that does not hurt my eyes
# - log that does eat up all the memory
set -euo pipefail
#set -x
target=192.168.0.1
mac=01:23:45:67:89:ab
interface=br-lan
interval=2
t_wait_for_boot=5
logfile="/www/wol/index.html"
max_log_lines=200
echo "<meta http-equiv="refresh" content="5">" > $logfile
echo "AUTO WOL started at $(date +%F__%H-%M-%S)<br>" >> $logfile
log() {
#echo $@
echo "$@<br/>" >> $logfile
}
rotate_log() {
count=$(wc -l $logfile | awk '{ print $1 }')
if [ $count -gt $max_log_lines ]; then
head -2 $logfile > /tmp/autowol.log.tmp
tail -50 $logfile >> /tmp/autowol.log.tmp
mv /tmp/autowol.log.tmp $logfile
fi
}
old=""
while sleep $interval; do
rotate_log
last_line=$(logread | grep "WOL_LOG.*DST=$target" | tail -1)
if [ "$last_line" != "" -a "$last_line" != "$old" ]; then
dt=$(date "+%F %H:%M:%S")
src=$(echo $last_line | sed -Ee 's/.* SRC=([0-9.]+) .*/\1/')
if ping -q -W 1 -c 1 $target >/dev/null; then
log "$dt NOWAKE SRC=$src DST=$target"
else
log "$dt WAKE SRC=$src DST=$target"
etherwake -i $interface $mac >> $logfile
log ""
sleep $t_wait_for_boot
fi
old=$last_line
fi
done
iptables -I FORWARD 1 -p tcp -d 192.168.0.1 -m limit --limit 1/min -j LOG --log-prefix "WOL_LOG: " --log-level 7
#!/bin/sh /etc/rc.common
START=90
STOP=90
PIDFILE=/var/run/autowol.pid
start() {
/root/autowol.sh &
echo $! > $PIDFILE
}
stop() {
kill -15 `cat $PIDFILE`
rm $PIDFILE
}
# run bridged traffic through iptables too
# http://superuser.com/a/928246
net.bridge.bridge-nf-call-iptables=1
# and update with
# sysctl -p
The MIT License (MIT)
Copyright (c) 2015 Felix Hummel
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@tyrumus
Copy link

tyrumus commented Jun 8, 2020

Would you mind if I turned this into an OpenWRT package for community use?

@felixhummel
Copy link
Author

@legostax Yes, of course, please do. :)

@Reutertu3
Copy link

Thank you for this amazing script. Bridge firewall doesn't come per default and needs to be installed before the iptables entry works. Could maybe include this in the comment.

opkg install kmod-br-netfilter

@felixhummel
Copy link
Author

Thanks, @Reutertu3!
I put it in the README. :)

@PovilasID
Copy link

Hi, will it still work since OpenWRT moved away from iptables to nftable?

@iiliev2
Copy link

iiliev2 commented Dec 15, 2023

Hi, will it still work since OpenWRT moved away from iptables to nftable?

On 22.03.2 I was able to do it via editing file /etc/nftables.d/10-custom-filter-chains.nft. Uncomment and edit:

 chain user_post_forward {
     type filter hook forward priority 1; policy accept;
     ip daddr <nas-ip> ct state new log prefix "WOL_LOG: "
 }

Afterward, I do see log entries with such prefix when I hit my nas.

logread -f -e WOL_LOG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment