Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #DEBUG= # uncomment/comment to enable/disable debug mode
- # name: merlin-wol-port-forward.sh
- # version: 2.1.0, 27-jul-2022, by eibgrad
- # purpose: wol to target of port forwarding
- # type(s): init-start, nat-start
- # href: https://tinyurl.com/2hwea3y7
- # installation:
- # 1. enable jffs custom scripts and configs (administration->system)
- # 2. ssh to router and copy/paste the following command:
- # curl -kLs bit.ly/merlin-installer|tr -d '\r'|sh -s b7p6f102
- # 3. modify script w/ your preferred options using nano editor:
- # nano /jffs/configs/merlin-wol-port-forward.options
- # 4. reboot
- CONFIGS_DIR='/jffs/configs'
- CONFIG="$CONFIGS_DIR/merlin-wol-port-forward.options"
- SCRIPTS_DIR='/jffs/scripts'
- SCRIPT1="$SCRIPTS_DIR/merlin-wol-port-forward.nat"
- SCRIPT2="$SCRIPTS_DIR/merlin-wol-port-forward.init"
- SCRIPT3="$SCRIPTS_DIR/nat-start"
- SCRIPT4="$SCRIPTS_DIR/init-start"
- mkdir -p $CONFIGS_DIR $SCRIPTS_DIR
- # ------------------ begin merlin-wol-port-forward.options ------------------- #
- cat << 'EOF' > $CONFIG
- # ------------------------------ BEGIN OPTIONS ------------------------------- #
- # protocol (tcp|udp) of port forward
- PROTO='tcp'
- # source ip(s)/network(s) of port forward (comma-separated)
- SOURCE='0.0.0.0/0'
- # external port of port forward
- EXT_PORT='5900'
- # internal ip of port forward
- INT_IP='192.168.1.100'
- # internal port of port forward
- INT_PORT='5900'
- # mac address of internal ip (unspecified = static lease search)
- MAC_ADDR='' # hexidecimal format: XX:XX:XX:XX:XX:XX
- # broadcast interface of internal ip
- BCAST_IF='br0'
- # how often (in secs) to check for new wol messages
- INTERVAL=10
- # ------------------------------- END OPTIONS -------------------------------- #
- EOF
- echo "installed: $CONFIG"
- # ------------------- end merlin-wol-port-forward.options -------------------- #
- # -------------------- begin merlin-wol-port-forward.nat --------------------- #
- cat << 'EOF' > $SCRIPT1
- #!/bin/sh
- #set -x # comment/uncomment to disable/enable debug mode
- {
- . $CONFIG
- # create port forward
- iptables -t nat -I VSERVER -p $PROTO -s $SOURCE --dport $EXT_PORT \
- -j DNAT --to $INT_IP:$INT_PORT
- # record access of port forward to log
- iptables -t nat -I VSERVER -p $PROTO -s $SOURCE --dport $EXT_PORT \
- -j LOG --log-prefix "WPF: DIP=$INT_IP "
- exit 0
- } 2>&1 | logger -t $(basename $0)[$$]
- EOF
- [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT1
- sed -i "s:\$CONFIG:$CONFIG:g" $SCRIPT1
- chmod +x $SCRIPT1
- echo "installed: $SCRIPT1"
- # --------------------- end merlin-wol-port-forward.nat ---------------------- #
- # -------------------- begin merlin-wol-port-forward.init -------------------- #
- cat << 'EOF' > $SCRIPT2
- #!/bin/sh
- #set -x # comment/uncomment to disable/enable debug mode
- (
- . $CONFIG
- # mask used for finding wol messages
- WOL_MSG_MASK="[W]PF: DIP=$INT_IP .* PROTO=$PROTO .* DPT=$INT_PORT "
- # work files
- CURR_MSG="/tmp/tmp.$$.curr_msg"
- PREV_MSG="/tmp/tmp.$$.prev_msg"; > $PREV_MSG
- # ignore any saved wol messages across a reboot
- if [ -f /jffs/syslog.log ]; then
- grep "$WOL_MSG_MASK" /jffs/syslog.log > $PREV_MSG
- fi
- # wait for *reliable* internet connection
- until ping -qc1 -W3 8.8.8.8 &>/dev/null; do sleep 10; done
- # validate mac address
- if [ ! "$MAC_ADDR" ]; then
- MAC_ADDR=$(get_mac $INT_IP)
- if [ ! "$MAC_ADDR" ]; then
- echo "fatal error: mac address not found: $INT_IP"
- exit 1
- fi
- else
- if ! echo "$MAC_ADDR" | \
- grep -qE '^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$'; then
- echo "fatal error: invalid/malformed mac address: $MAC_ADDR"
- exit 1
- fi
- fi
- while sleep $INTERVAL; do
- # extract all wol messages
- grep "$WOL_MSG_MASK" /tmp/syslog.log > $CURR_MSG
- # if there are any new wol messages, then wol as necessary
- if [ -s $CURR_MSG ] && ! ping -qc1 -W3 $INT_IP &>/dev/null; then
- if [ ! -s $PREV_MSG ] || \
- grep -m1 -Fxvf $PREV_MSG $CURR_MSG >/dev/null; then
- # try up to three (3) times to wake the device
- for i in 1 2 3; do
- echo "info: waking up $MAC_ADDR (attempt #${i}) ..."
- ether-wake -bi $BCAST_IF $MAC_ADDR && sleep 20 || break
- if ping -qc1 -W3 $INT_IP &>/dev/null; then
- echo "info: $MAC_ADDR is alive!"
- break
- fi
- [ $i -eq 3 ] && echo "warning: $MAC_ADDR did NOT respond :("
- done
- fi
- fi
- # remember which wol messages have already been processed
- mv $CURR_MSG $PREV_MSG
- done
- ) 2>&1 | logger -t $(basename $0)[$$] &
- EOF
- [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT2
- sed -i "s:\$CONFIG:$CONFIG:g" $SCRIPT2
- chmod +x $SCRIPT2
- echo "installed: $SCRIPT2"
- # --------------------- end merlin-wol-port-forward.init --------------------- #
- # ----------------------------- begin nat-start ------------------------------ #
- create_script() {
- cat << 'EOF' > $SCRIPT3
- #!/bin/sh
- #set -x # comment/uncomment to disable/enable debug mode
- {
- $SCRIPT1
- } 2>&1 | logger -t $(basename $0)[$$]
- EOF
- [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT3
- sed -i "s:\$SCRIPT1:$SCRIPT1:g" $SCRIPT3
- chmod +x $SCRIPT3
- }
- if [ -f $SCRIPT3 ]; then
- echo "error: $SCRIPT3 already exists; requires manual installation"
- else
- create_script
- echo "installed: $SCRIPT3"
- fi
- # ------------------------------ end nat-start ------------------------------- #
- # ----------------------------- begin init-start ----------------------------- #
- create_script() {
- cat << 'EOF' > $SCRIPT4
- #!/bin/sh
- #set -x # comment/uncomment to disable/enable debug mode
- {
- $SCRIPT2
- } 2>&1 | logger -t $(basename $0)[$$]
- EOF
- [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT4
- sed -i "s:\$SCRIPT2:$SCRIPT2:g" $SCRIPT4
- chmod +x $SCRIPT4
- }
- if [ -f $SCRIPT4 ]; then
- echo "error: $SCRIPT4 already exists; requires manual installation"
- else
- create_script
- echo "installed: $SCRIPT4"
- fi
- # ------------------------------ end init-start ------------------------------ #
Add Comment
Please, Sign In to add comment