唤醒主机的WOL(Wake ON LAN )脚本

日志文件(LOGFILE)放在/tmp/。 OpenWrt 不提供与 DD-WRT 同样的页面。

注意: 重新启动运行此脚本的路由器时会出现问题如 https://forum.openwrt.org/t/cant-reboot-while-sleep-script-wol/


伙计们, 我发现 https://www.dd-wrt.com/wiki/index.php/Useful_Scripts#Web_Server_Wake-up 这个脚本通过请求唤醒主机
如果我的 Kodi 或 PC 想要任何东西,我会用它来唤醒我的 NAS。

要求:

  • 固件版本: OpenWrt Barrier Breaker 14.07 / LuCI Trunk (0.12+svn-r10530)
  • 内核版本: 3.10.49
Required to change Variables Default value Description
optional INTERVAL 5 repeat the script every N second
not required OLD empty should be empty
optional PORT 80 it the port who looks the script
optional NUMP 3 the retries, before the script gave up
yes TARGET 192.168.1.1 the wake up device
yes INTERFACE br-lan here you can type in a broadcast address or a interface, i like more interface
yes MAC 00:00:00:00:00:00 the target mac adress
not required WOL /usr/bin/etherwake the program and path for wol
optional LOGFILE “/www/wol/index.html” the log output folder, in this case for the url <ROUTER-IP>/wol/index.html
optional LOGPROG “logread” i will read the logs from LOGREAD, but you can also read from “dmesg” or something else

安装以下软件包

opkg update
opkg install etherwake

保存脚本

cat << "EOF" > /bin/autowol.sh
#!/bin/sh
#Enable JFFS2 and place script in /jffs/ then run on startup in web interface.
#You can check the log from http://192.168.1.1/user/wol.html
 
#debugging
#set -xv
 
INTERVAL=5
NUMP=3
OLD=""
PORT=80
TARGET=192.168.1.1
INTERFACE=br-lan
MAC=00:00:00:00:00:00
WOL=/usr/bin/etherwake
LOGFILE="/www/wol/index.html"
LOGPROG="logread" # default: dmesg
 
echo "<meta http-equiv=\"refresh\" content=\"10\">" > $LOGFILE
echo "AUTO WOL Script started at" `date` "<br>" >> $LOGFILE
 
wake_up () {
	PORT=$1
	TARGET=$2
	BROADCAST=$3
	MAC=$4
	NEW=`$LOGPROG | awk '/WOL_LOG/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/ {print }' | tail -1`
	SRC=`$LOGPROG | awk -F'[=| ]' '/WOL_LOG/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/ {print }' | tail -1`
	LINE=`$LOGPROG | awk '/WOL_LOG/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/'`
	if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
		if ping -qc $NUMP $TARGET >/dev/null; then
			echo "NOWAKE $TARGET was accessed by $SRC and is already alive at" `date` "<br>">> $LOGFILE
		else
			echo "WAKE $SRC causes wake on lan at" `date` "<br>">> $LOGFILE
			$WOL -i $BROADCAST $MAC >> $LOGFILE
			echo "<br>" >> $LOGFILE
			sleep 5
		fi
		OLD=$NEW
	fi
}
 
while sleep $INTERVAL; do
wake_up $PORT $TARGET $INTERFACE $MAC;
done
EOF
chmod +x /bin/autowol.sh

Pro Tip: Don't copy the code above as it causes a bunch of errors due to a stray CRLF character at the end of each line. Instead just download a copy of the file here, and save yourself a day of troubleshooting!

如果脚本正常,则首先自动启动。

System → Startup → Local Startup 后 输入:

/bin/autowol.sh

开启 debug 输出:

sh -x -v /bin/autowol.sh

检查日志文件: http://192.168.1.1/wol/

Network → Firewall → Custom Rules 后添加以下规则:

iptables -I FORWARD 1 -p tcp -d 192.168.1.1 -m limit --limit 1/min -j LOG --log-prefix "WOL_LOG:  " --log-level 7
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • Last modified: 2025/02/06 06:04
  • by starsfall