User Tools

Site Tools


doc:howto:auto_wake_on_lan

Auto Wake On LAN Script for Hosts

He guys, i found on https://www.dd-wrt.com/wiki/index.php/Useful_Scripts#Web_Server_Wake-up this useful script for wake up a host by request.
I use it for my NAS to wake up if my Kodi or PC want any from him.

it works under
Firmware Version OpenWrt Barrier Breaker 14.07 / LuCI Trunk (0.12+svn-r10530)
Kernel Version 3.10.49

Using

Install Packages

Before the Script can work you must install:

opkg update
opkg install etherwake

Set a firewall rule for logging

Please Change the IP from the Rule to your Target IP.

Go to Network → Firewall → Custom Rules and add this rule:

iptables -I FORWARD 1 -p tcp -d 192.168.1.1 -m limit --limit 1/min -j LOG --log-prefix "WOL_LOG:  " --log-level 7

Set the script

Reqeuired to Change Variables Default Value Description
optional INTERVAL 5 repeat the script every N second
not required OLD empty should be empty
optional PORT 80 it the port who looks the script
optional NUMP 3 the retries, before the script gave up
yes TARGET 192.168.1.1 the wake up device
yes INTERFACE br-lan here you can type in a broadcast address or a interface, i like more interface
yes MAC 00:00:00:00:00:00 the target mac adress
not required WOL /usr/bin/etherwake the program and path for wol
optional LOGFILE "/www/wol/index.html" the log output folder, in this case for the url <ROUTER-IP>/wol/index.html
optional LOGPROG "logread" i will read the logs from LOGREAD, but you can also read from "dmesg" or something else

Copy and edit the script

Copy the script into a folder like "/script".

mkdir <PATH><FOLDER>
touch <PATH><FOLDER><NAME OF SCRIPT eg. autowol.sh>
chmod +x <PATH><FOLDER><NAME OF SCRIPT eg. autowol.sh>
nano <PATH><FOLDER><NAME OF SCRIPT eg. autowol.sh>
example:

mkdir /script
touch /script/autowol.sh
chmod +x /script/autowol.sh
nano /script/autowol.sh

  #!/bin/sh
  #Enable JFFS2 and place script in /jffs/ then run on startup in web interface.
  #You can check the log from http://192.168.1.1/user/wol.html
 
  #debugging
  #set -xv
 
  INTERVAL=5
  NUMP=3
  OLD=""
  PORT=80
  TARGET=192.168.1.1
  INTERFACE=br-lan
  MAC=00:00:00:00:00:00
  WOL=/usr/bin/etherwake
  LOGFILE="/www/wol/index.html"
  LOGPROG="logread" # default: dmesg
 
  echo "<meta http-equiv=\"refresh\" content=\"10\">" > $LOGFILE
  echo "AUTO WOL Script started at" `date` "<br>" >> $LOGFILE
 
  wake_up () {
	PORT=$1
	TARGET=$2
	BROADCAST=$3
	MAC=$4
 
	NEW=`$LOGPROG | awk '/WOL_LOG/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/ {print }' | tail -1`
        SRC=`$LOGPROG | awk -F'[=| ]' '/WOL_LOG/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/ {print }' | tail -1`
        LINE=`$LOGPROG | awk '/WOL_LOG/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/'`
        if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
             if ping -qc $NUMP $TARGET >/dev/null; then
                echo "NOWAKE $TARGET was accessed by $SRC and is already alive at" `date` "<br>">> $LOGFILE
             else
                echo "WAKE $SRC causes wake on lan at" `date` "<br>">> $LOGFILE
                $WOL -i $BROADCAST $MAC >> $LOGFILE
                echo "<br>" >> $LOGFILE
                sleep 5
             fi
	     OLD=$NEW
       fi
       }
 
  while sleep $INTERVAL; do
	wake_up $PORT $TARGET $INTERFACE $MAC;
  done

Pro Tip: Don't copy the code above as it causes a bunch of errors due to a stray CRLF character at the end of each line. Instead just download a copy of the file here, and save yourself a day of troubleshooting!

Enable autostart

First autostart, if the script is ok.

Go to System → Startup → Local Startup and type in:

/script/autowol.sh
or your:
/<PATH>/<SCRIPTNAME>

and SUBMIT

Check Log file

To check the Log file go to:

<ROUTER IP>/wol/

Troubleshooting

If you have Problem take the command line and uncomment the "set -xv" option.

from

# set -xv 
to
set -xv 

Now Do:

/<PATH to your script>/<scriptname>

and check the output.

doc/howto/auto_wake_on_lan.txt · Last modified: 2015/11/23 16:48 by cal3b