Posted: Tue Aug 14, 2018 16:02 Post subject: Computer Can't Sleep! - Wake-on-LAN Script
I'm using the following startup script on my router to wake up my computer when certain port requests are made to it:
Code:
#!/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
INTERVAL=1
PINGTIME=1
OLD=""
PORTPLEX=32400
PORTFTPCTRL=20
PORTFTPDATA=21
PORTRDP=3389
WOLPORT=9
TARGET=192.168.1.8
BROADCAST=192.168.1.255
MAC=aa:bb:cc:dd:ee:ff
WOL=/usr/sbin/wol
LOGFILE="/tmp/www/wol.html"
echo "<meta http-equiv=\"refresh\" content=\"10\">" > $LOGFILE
echo "["`date`"] AUTO WOL Script started. <br>" >> $LOGFILE
while sleep $INTERVAL;do
NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTPLEX"'/ {print }' | tail -1`
SRC=`dmesg | awk -F'[=| ]' '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTPLEX"'/ {print $7}' | tail -1`
LINE=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTPLEX"'/'`
if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
if ping -qw $PINGTIME $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 -p $WOLPORT $MAC >> $LOGFILE
echo "<br>" >> $LOGFILE
sleep 1
fi
OLD=$NEW
fi
NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPCTRL"'/ {print }' | tail -1`
SRC=`dmesg | awk -F'[=| ]' '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPCTRL"'/ {print $7}' | tail -1`
LINE=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPCTRL"'/'`
if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
if ping -qw $PINGTIME $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 -p $WOLPORT $MAC >> $LOGFILE
echo "<br>" >> $LOGFILE
sleep 1
fi
OLD=$NEW
fi
NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPDATA"'/ {print }' | tail -1`
SRC=`dmesg | awk -F'[=| ]' '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPDATA"'/ {print $7}' | tail -1`
LINE=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPDATA"'/'`
if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
if ping -qw $PINGTIME $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 -p $WOLPORT $MAC >> $LOGFILE
echo "<br>" >> $LOGFILE
sleep 1
fi
OLD=$NEW
fi
NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTRDP"'/ {print }' | tail -1`
SRC=`dmesg | awk -F'[=| ]' '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTRDP"'/ {print $7}' | tail -1`
LINE=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTRDP"'/'`
if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
if ping -qw $PINGTIME $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 -p $WOLPORT $MAC >> $LOGFILE
echo "<br>" >> $LOGFILE
sleep 1
fi
OLD=$NEW
fi
Basically, if someone tries to access my Plex remotely, or FTP, or Remote Desktop, and the computer is sleeping, the router will send a WOL to it and wake it up. This functionality works great, however, it now appears that my computer will not sleep. When putting it to sleep manually, or if it goes to sleep on its own due to being idle, it wakes up within 2 minutes. It sometimes only sleeps for as little as 5 seconds before waking again. I've played with a few different settings to try and isolate the problem and discovered that the only thing that fixes it is disabling this script.
I'd really like to be able to use this script so I can leave my computer in sleep when away from home, but still be able to access Plex and other things remotely. Does anyone know what part of the script might be the culprit, or how I can fix it?
I should say that I'm using a Netgear WNDR4300 (Atheros), but was having the same problems with a Netgear WNDR3300 (Broadcom). My computer is running Windows 10 Pro.
Without going into the details of the script and whether it is written correctly, is it waking up *because* the script is triggering the WOL, or is it waking up in spite of the fact the script is NOT triggering the WOL?
I do know that some network adapters allow you to configure WOL to either trigger only on the magic packet (preferred), or *any* network traffic. And it sounds like it might be the latter.
IOW, what are we debugging here, the script or the WOL configuration of the BIOS/OS?
I don't know if this answers your question (let me know if it doesn't), but I'm about 90% sure it's because the script is triggering WOL. I can wake the computer just fine with a manual WOL when the script is turned off (i.e. not interfering with sleep).
That being said, I'm not sure if my network adapter is configured for magic pack WOL or any traffic. How can I check this?
Locate the network adapter in Device Manager, open Properties, then Advanced. You should see one or more WOL options. Exactly what will appear will vary from adapter to adapter. But usually you have the option to match on the magic packet, a specific pattern, any traffic, or some combination.
Cool. I have the following settings:
Wake on Magic Packet - ENABLED
Wake on pattern match - ENABLED
WOL & Shutdown Link Speed - 100 Mbps First
I decided to test your theory, d0ug, and it turns out you may be correct.
I currently have this setup:
INTERNET --> MODEM --> ROUTER --> COMPUTER
I closed of the relevant ports and had no more problems, my computer sleep the whole night!
I reopened them today and add something to the script so that the log file will tell me which ports are waking up the computer. So far its 3389 every time, every minute. This is what the message looks like:
Code:
WAKE OUT at port 3389 causes wake on lan at Wed Aug 15 08:13:16 MDT 2018
OUT is specified by the $SRC variable in the script. I wonder if that means it's outgoing? Could this mean that it is my computer waking itself up? Or maybe it means it is coming from outside. Anyways, I'm going to close that port on the modem and see what happens. Let me know your thoughts.
Appreciate the help!
Also, side-note, at least the Plex port MUST remain open for me to Plex outside the network. Sure I could just VPN every time I want to use it, but I have family who don't have that option, so I don't know what other choice I have other than leave this port completely exposed. Doesn't sound secure, it's a shame...
After disabling port forwarding on the modem for port 3389, I have had no problems. So it must be that. Now the question is, what does OUT mean in the log file? I guess I'll work on figuring that out to see if someone has been hammering that port or not.
EDIT
I think OUT must be outgoing, because when I try to access the computer through FTP port 21, I get this message:
Code:
WAKE br0 at port 21 causes wake on lan at...
So instead of OUT as the source, it has br0.
Still, I can't be sure. If it is outgoing, what does that mean? RDP is sending an outgoing request from the computer to the router? How can it do this if it's asleep?