我有一个运行 DD-WRT 的路由器,它有一个启动脚本,用于在某些端口上的 LAN 请求上唤醒我的计算机。我在这里找到了这个 shell 脚本并将其更改为允许在多个端口上唤醒。我尝试做一个 for 循环,这样我就不必重复任何代码,但这不起作用。唯一有效的是:
#!/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
INTERVAL=1
PINGTIME=1
OLD=""
PORTPLEX=32400
PORTFTPCTRL=20
PORTFTPDATA=21
PORTRDP=3389
WOLPORT=9
TARGET=192.168.1.8
BROADCAST=192.168.1.255
MAC=aa:bb:cc:dd:ee:ff
WOL=/usr/sbin/wol
LOGFILE="/tmp/www/wol.html"
echo "<meta http-equiv=\"refresh\" content=\"10\">" > $LOGFILE
echo "["`date`"] AUTO WOL Script started. <br>" >> $LOGFILE
while sleep $INTERVAL;do
NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTPLEX"'/ {print }' | tail -1`
SRC=`dmesg | awk -F'[=| ]' '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTPLEX"'/ {print $7}' | tail -1`
LINE=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTPLEX"'/'`
if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
if ping -qw $PINGTIME $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 -p $WOLPORT $MAC >> $LOGFILE
echo "<br>" >> $LOGFILE
sleep 1
fi
OLD=$NEW
fi
NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPCTRL"'/ {print }' | tail -1`
SRC=`dmesg | awk -F'[=| ]' '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPCTRL"'/ {print $7}' | tail -1`
LINE=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPCTRL"'/'`
if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
if ping -qw $PINGTIME $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 -p $WOLPORT $MAC >> $LOGFILE
echo "<br>" >> $LOGFILE
sleep 1
fi
OLD=$NEW
fi
NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPDATA"'/ {print }' | tail -1`
SRC=`dmesg | awk -F'[=| ]' '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPDATA"'/ {print $7}' | tail -1`
LINE=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTFTPDATA"'/'`
if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
if ping -qw $PINGTIME $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 -p $WOLPORT $MAC >> $LOGFILE
echo "<br>" >> $LOGFILE
sleep 1
fi
OLD=$NEW
fi
NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTRDP"'/ {print }' | tail -1`
SRC=`dmesg | awk -F'[=| ]' '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTRDP"'/ {print $7}' | tail -1`
LINE=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORTRDP"'/'`
if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
if ping -qw $PINGTIME $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 -p $WOLPORT $MAC >> $LOGFILE
echo "<br>" >> $LOGFILE
sleep 1
fi
OLD=$NEW
fi
done
我猜要么(1)我不太了解 for 循环在 bash 中是如何工作的,要么(2)DD-WRT 的 shell 脚本与其他版本的 Linux 不同,或者两者兼而有之。
我如何将这部分代码转换为在每个端口上循环的 for 循环中执行?
NEW=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/ {print }' | tail -1`
SRC=`dmesg | awk -F'[=| ]' '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/ {print $7}' | tail -1`
LINE=`dmesg | awk '/ACCEPT/ && /DST='"$TARGET"'/ && /DPT='"$PORT"'/'`
if [ "$NEW" != "" -a "$NEW" != "$OLD" ]; then
if ping -qw $PINGTIME $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 -p $WOLPORT $MAC >> $LOGFILE
echo "<br>" >> $LOGFILE
sleep 1
fi
OLD=$NEW
fi
谢谢!