1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
Dismiss Notice

Welcome To SNBForums

SNBForums is a community for anyone who wants to learn about or discuss the latest in wireless routers, network storage and the ins and outs of building and maintaining a small network.

If you'd like to post a question, simply register and have at it!

While you're at it, please check out SmallNetBuilder for product reviews and our famous Router Charts, Ranker and plenty more!

Help with WOL Scripting (Multiple Ports)

Discussion in 'Asuswrt-Merlin' started by AnOutgunnedMonk, Jan 21, 2019.

  1. AnOutgunnedMonk

    AnOutgunnedMonk New Around Here

    Joined:
    Jan 21, 2019
    Messages:
    2
    Hey Folks,

    I've been working off of the guide to setup WOL (found here https://github.com/RMerl/asuswrt-merlin/wiki/WOL-Script-Wake-Up-Your-Webserver-On-Internet-Traffic)

    I have most of the scripting working, however I'm struggling with adding a little bit of functionality. There are instructions as to how to specify a single port to allow to wake a server. I would like to expand this to allow a range of ports to wake the server, but have no practical experience with the scripting. For convenience I've included the script below.

    INTERVAL=5
    NUMP=3
    OLD=""
    TARGET=PUT YOUR INTERNAL IP ADRESS HERE!
    PORT=SPECIFY YOUR PORT HERE! [THIS IS WHERE I'D LIKE TO USE MULTIPLE PORTS]
    IFACE=br0
    MAC=PUT YOUR MAC ADRESS HERE!
    WOL=/usr/sbin/ether-wake
    LOGFILE="/var/log/ether-wake.log"

    while sleep $INTERVAL;do
    NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/ {print }' | tail -1`
    SRC=`echo $NEW | awk -F'[=| ]' '/SRC/{for(i=1;i<=NF;i++) if($i ~ /SRC/) print $(i+1)}'`
    DPORT=`echo $NEW | awk -F'[=| ]' '/DPT/{for(i=1;i<=NF;i++) if($i ~ /DPT/) print $(i+1)}'`
    PROTO=`echo $NEW | awk -F'[=| ]' '/PROTO/{for(i=1;i<=NF;i++) if($i ~ /PROTO/) print $(i+1)}'`

    if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
    if ! ping -qc $NUMP $TARGET >/dev/null; then
    # echo "NOWAKE $TARGET was accessed by $SRC, port $DPORT, protocol $PROTO and is already alive at" `date`>> $LOGFILE
    # else
    echo "WAKE $TARGET requested by $SRC, port $DPORT, protocol $PROTO at" `date`>> $LOGFILE
    $WOL -i $IFACE $MAC
    sleep 5
    fi
    OLD=$NEW
    fi
    done


    Thanks for everyone's help
     
  2. elorimer

    elorimer Very Senior Member

    Joined:
    Dec 16, 2013
    Messages:
    551
    I'll be interested in the more knowledgeable responses you get. I didn't think the protocol was dependent on a port, so much as the port triggering the script. To be honest, I found the wiki examples and the script way overcomplicated for anything I needed, even to wake up more than one server.

    I did see from examples that you can use the OpenVPN username to trigger particular servers to wake up and not others. Haven't tried that.
     
    AnOutgunnedMonk likes this.
  3. AnOutgunnedMonk

    AnOutgunnedMonk New Around Here

    Joined:
    Jan 21, 2019
    Messages:
    2
    From what I gather the actual Ether-wake is not dependent on a specific port. This script includes some logic to check the port that was reported in the log file against the port specified at the top of the script. If they match, it would proceed with sending the WoL packet. So I think the solution lies somewhere in these two lines, but I'd just need to figure out how to check against multiple port values instead of just one.

    PORT=SPECIFY YOUR PORT HERE! [THIS IS WHERE I'D LIKE TO USE MULTIPLE PORTS]
    NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/ {print }' | tail -1`