Skip to content

Instantly share code, notes, and snippets.

@emilwojcik93
Forked from felixhummel/LICENSE.txt
Created May 12, 2021 21:44
Show Gist options
  • Save emilwojcik93/6c6fa9a81908b95d331986d6281a976b to your computer and use it in GitHub Desktop.
Save emilwojcik93/6c6fa9a81908b95d331986d6281a976b 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment