Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- DEBUG= # uncomment/comment to enable/disable debug mode
- # name: ddwrt-wol-port-forward.sh
- # version: 1.2.0, 07-jan-2022, by eibgrad
- # purpose: port forward w/ wol activation
- # script type: wanup (autostart) + startup (autostart)
- # installation:
- # 1. enable jffs2 (administration->jffs2)
- # 2. enable syslogd (services->services->system log)
- # 3. use shell (telnet/ssh) to execute one of the following commands:
- # curl -kLs bit.ly/ddwrt-installer|tr -d '\r'|sh -s -- --dir /tmp NUb73JqK
- # or
- # wget -qO - bit.ly/ddwrt-installer|tr -d '\r'|sh -s -- --dir /tmp NUb73JqK
- # 4. use vi editor to modify installer options:
- # vi /tmp/ddwrt-wol-port-forward.sh
- # 5. execute installer:
- # /tmp/ddwrt-wol-port-forward.sh
- # 6. reboot
- SCRIPTS_DIR='/jffs/etc/config'; mkdir -p $SCRIPTS_DIR
- SCRIPT1="$SCRIPTS_DIR/ddwrt-wol-pf.wanup"
- SCRIPT2="$SCRIPTS_DIR/ddwrt-wol-pf.startup"
- # ------------------------- begin ddwrt-wol-pf.wanup ------------------------- #
- cat << 'EOF' > $SCRIPT1
- #!/bin/sh
- set -x # uncomment/comment to enable/disable debug mode
- {
- #!/bin/sh
- # ------------------------------ 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'
- # ------------------------------- END OPTIONS -------------------------------- #
- # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
- # create port forward
- iptables -t nat -I PREROUTING -p $PROTO -s $SOURCE -d $(nvram get wan_ipaddr) \
- --dport $EXT_PORT -j DNAT --to $INT_IP:$INT_PORT
- iptables -I FORWARD -p $PROTO -s $SOURCE -d $INT_IP --dport $INT_PORT -j ACCEPT
- # record access of port forward to log
- iptables -I FORWARD -p $PROTO -s $SOURCE -d $INT_IP --dport $INT_PORT \
- -m state --state NEW -j LOG --log-prefix "WOL Port Forward "
- } 2>&1 | logger -t $(basename $0)[$$]
- EOF
- [ ${DEBUG+x} ] || sed -ri 's/^(set -x)/#\1/g' $SCRIPT1
- chmod +x $SCRIPT1
- echo "installed: $SCRIPT1"
- # -------------------------- end ddwrt-wol-pf.wanup -------------------------- #
- # ------------------------ begin ddwrt-wol-pf.startup ------------------------ #
- cat << 'EOF' > $SCRIPT2
- #!/bin/sh
- set -x # uncomment/comment to enable/disable debug mode
- (
- # ------------------------------ BEGIN OPTIONS ------------------------------- #
- # 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 kernel messages
- INTERVAL=10
- # ------------------------------- END OPTIONS -------------------------------- #
- # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
- get_val() { sed -rn "s/^$1=(.*)/\1/p" $SCRIPT1 | tr -d \'\"; }
- get_mac() {
- sed -rn \
- "s/^dhcp-host=.*(([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}).*,$1(,.*|$)/\1/p" \
- /tmp/dnsmasq.conf | head -n1
- }
- # internal ip of port forward
- INT_IP=$(get_val 'INT_IP')
- # protocol (tcp|udp) of port forward
- PROTO=$(get_val 'PROTO' | awk '{print toupper($0)}')
- # internal port of port forward
- INT_PORT=$(get_val 'INT_PORT')
- # broadcast ip of internal ip's network interface
- BCAST_IP="$(ifconfig $BCAST_IF | awk '/Bcast/{split ($3,A,":"); print A[2]}')"
- # mask used for finding wol messages
- WOL_MSG_MASK="^WOL Port Forward .* DST=$INT_IP .* PROTO=$PROTO .* DPT=$INT_PORT "
- # work files
- CURR_MSG="/tmp/tmp.$$.curr_msg"
- PREV_MSG="/tmp/tmp.$$.prev_msg"; > $PREV_MSG
- # 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
- dmesg | grep "$WOL_MSG_MASK" > $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}) ..."
- /usr/sbin/wol -i $BCAST_IP $MAC_ADDR >/dev/null && 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 's/^(set -x)/#\1/g' $SCRIPT2
- sed -i "s:\$SCRIPT1:$SCRIPT1:g" $SCRIPT2
- chmod +x $SCRIPT2
- echo "installed: $SCRIPT2"
- # ------------------------- end ddwrt-wol-pf.startup ------------------------- #
Add Comment
Please, Sign In to add comment