OpenWrt Forum Archive

Topic: Openwrt and Wake on LAN (WOL)

The content of this topic has been archived on 1 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi guys! Please help me!!!!!
I want configure wol  host by request
This code and instructions for configure and script for wake on lan computer when request client on port 32400 (to server).

New to PLEX as of a few weeks ago...someone showed it to me and it seemed awesome. So I set it up on an old PC I had at home.
I didn't like the idea of having that computer run all the time. Of course, googling "PLEX + WOL" only comes up with people complaining how the app doesn't have a built in wake on lan function. Not a big help.
My first thing was to just use a separate app to send a Wake on LAN/WAN message to my server. This worked just fine...for me. Then I realized that I'd have to install and configure this same app for other people on their phone if I wanted to share the server with them. This seemed really inconvenient.
So, here's what I did.
Flashed my Linksys E4200 v1 with tomato. dd-wrt or openWRT will work just fine for this. Basically, a router where you can run scripts and inject iptables rules. The router was giving me problems with stock firmware so I was happy to have a reason to flash custom fw.
In Administration > Scripts, added the 2 following lines to the Firewall section
 # Rule to log all new connections on LAN for the plex server for the WOL script to trigger
 iptables -I FORWARD -i br0 -p tcp --dport 32400 -m state --state NEW -j LOG --log-prefix "PLEX LAN Connection "

 # Rule to log all new connections on WAN for the plex server
 iptables -I FORWARD -i vlan2 -p tcp --dport 32400 -m state --state NEW -j LOG --log-prefix "PLEX WAN Connection "
Basically, all new TCP connections to dst port 32400 are sent to /var/log/messages. They look like this:
 Jul 30 14:13:04 unknown user.warn kernel: PLEX WAN Connection IN=vlan2 OUT=br0 SRC=xxx.xxx.xxx.xxx DST=192.168.1.yyy LEN=60 TOS=0x00 PREC=0x00 TTL=49 ID=4796 DF PROTO=TCP SPT=51833 DPT=32400 SEQ=3659947144 ACK=0 WINDOW=65535 RES=0x00 SYN URGP=0 OPT (020403DA0402080A02144E340000000001030308)
(Note, you could just omit the -i in the FORWARD chain and combine them into a single rule, but I have them separate just in case I want to do something different on WAN vs LAN).
Anyway, I have one final script which is checking that /var/log/messages file every few seconds for new lines. If it finds the above line, it tries to ping the plex server. If no response, sends a wol message.
Here is the full script (I put it in the 'WAN Up' section of the event scripts):
 #!/bin/sh

 TARGET=<ip of plex server>
 MAC=<mac of plex server>
 PORTS="(32400)"

 INTERVAL=2
 NUMP=1

 OLD_LC=`wc -l /var/log/messages | awk '{print $1}'`

 while sleep $INTERVAL
 do
     # Only care about new lines since the script last ran
     LC=`wc -l /var/log/messages | awk '{print $1}'`
     NEWLINES=`expr $LC - $OLD_LC`
     if [ "$LC" -ne "$OLD_LC" ]; then
         # Could handle WAN vs LAN different if I wanted. Just do the same thing on either for now...
         LINE=`tail -$NEWLINES /var/log/messages | egrep "PLEX .* DST=$TARGET .* DPT=$PORTS" | tail -1`
         SRC=`echo $LINE | awk '{print $10}' | sed -e "s/SRC=//g"`
         if [ "$SRC" != "" ]; then
             # Found a matching line. Try to ping the server
             RET=`ping -c $NUMP -W 1 $TARGET 2> /dev/null | awk '/packets received/ {print $4}'`
             if [ "$RET" -ne "$NUMP" ]; then
                 # Guess it's sleeping. Send WoL.
                 echo "[`date -Iseconds`] $SRC causes WOL. Line was $LINE"  >> /var/log/wol
                 /usr/sbin/ether-wake $MAC
                 # Could sleep for 20 minutes I guess...I mean, there's no real reason to check again.
                 # Whatever...10 seconds is fine.
                 sleep 10
             fi
         fi
     fi
     OLD_LC=`wc -l /var/log/messages | awk '{print $1}'`
 done
And that's it. I have my server sleep in 20 minutes of no activity...so it is off most of the time. But, whenever someones opens a plex app (android, windows, etc), it will turn on my server (and yes, it stays on as long as stuff is streaming).
It takes about 10-12 seconds for my media to show up from the hibernate state which isn't too bad. Not sure where the bottleneck is...possibly because I don't have a solid state drive for my OS on the server. The script will take a maximum of 3 seconds to send the WoL and a minimum of 1 second (ping on tomato has a minimum of 1 second ping reply wait). The PLEX app might just wait a few seconds for server discovery if it doesn't get a reply right away. Haven't really tried to shorten it yet.
Hope someone here finds this useful!

say me please how can i run this script in my router ( D-link dir 615 Openwrt)
Where i must put the script for run in in startup.



Thank you very much!

(Last edited by Corleone on 1 Nov 2016, 12:24)

Hi. You don't need any external script.
Just install luci-app-wol (inlucude etherwake)
Can you wake target computer over lan?

Yes! I Can wake target computer over Lan! But I need wake computer by request to port 32400!
How can I do this?

follow these steps. I assume you hace luci-app-wol installed

https://picload.org/image/rdiaccdd/portforwardwow.png

I suggest, use an external web service, like depicus, just for test.
4343 its the default port for depicus.

(Last edited by mOrfiUs on 4 Nov 2016, 00:35)

The discussion might have continued from here.