scottsweb / ham

✅ Podman powered home automation. Home Assistant, VPN, Pi-Hole, MQTT, Caddy and more...
20 stars 1 forks source link

Router: WOL for Plex #92

Closed scottsweb closed 11 months ago

scottsweb commented 11 months ago

From: https://www.thiemo.ch/plex-wake-on-lan/ (now offline)

In the AsusWRT web interface, enable SSH login, as we need later to log into a remote shell on the router and create the two scripts at their correct locations.

After that, you’ll want to enable the persistent JFFS2 partition, which makes a little place on the router’s persistent flash memory for our scripts. You can find this option under Administration -> System -> Persistent JFFS2 partition -> Enable JFFS custom scripts and configs

The next step is to log into our router remotely via SSH

ssh routeradminname@routerip

and to create two scripts on the JFFS partition. For this, first cd into the right directory, which on AsusWRT is

cd /jffs/scripts/

Per default, there are no scripts in here, so we will create our set of two

touch firewall-start wan-start

Per naming convention, AsusWRT will fire up the first script when it’s firewall goes up, so it’s the perfect place to put in some custom iptables commands. The second script is run when the WAN network interface is up.

So fire up your favourite text editor so we can get to business editing those scripts. If you have not installed any other text editor, vi is installed per default on AsusWRT’s busybox.

First, edit the script named firewall-start as follows. Be sure to pay special attention to the highlighted lines and alter them according to your IPs, ports and MAC adresses:

#!/bin/sh

#eth0=WAN
#br0=bridge
#eth1=Wifi 2,5GHz
#eth2=Wifi 5Ghz
#vlan1=Lan Switch
#br0 is vlan1, eth1, eth2, basically whole LAN

#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 eth0 -p tcp --dport 32400 -m state --state NEW -j LOG --log-prefix "PLEX WAN Connection "

These two iptables commands will log to syslog every time there is traffic at the specified port (32400) either from WAN or from LAN. If your Plex Server Port differs from mine, you have to change the script accordingly.

Now it’s time to edit the wan-start as follows:

#!/bin/sh

# IP of the machine that should be woken
TARGET=192.168.1.3

# MAC address of the machine that should be woken
MAC=00:1F:5B:3C:9F:F8

# WAN and LAN ports of the plex app (in my case it's both 32400)
PORTS="(32400)"

INTERVAL=2
NUMP=1

OLD_LC=`wc -l /tmp/syslog.log | awk '{print $1}'`

while sleep $INTERVAL
do
    # Only care about new lines since the script last ran
    LC=`wc -l /tmp/syslog.log | 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 /tmp/syslog.log | 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
                # default interface of ether-wake is eth0, but on AC87U eth0 is WAN, so we use br0 which is LAN.
                /usr/bin/ether-wake -i br0 $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 /tmp/syslog.log | awk '{print $1}'`
done

Now go and reboot your router and that’s all there is to it, your computer should wake every time there is traffic on the Plex Server port.