本文最后更新于 179 天前,其中的信息可能已经有所发展或是发生改变。

路由器每10分钟检测是否能ping通百度,如果不能,就重连wan口的脚本
用来应对学校的奇怪网络环境

#!/bin/sh

TARGET="www.baidu.com"
LOG_FILE="/tmp/check_wan.log"
MAX_SIZE=10240
CHECK_INTERVAL_FILE="/tmp/check_wan_last_check"
MIN_INTERVAL=600

current_time=$(date +%s)
if [ -f "$CHECK_INTERVAL_FILE" ]; then
    last_check_time=$(cat "$CHECK_INTERVAL_FILE")
else
    last_check_time=0
fi

time_diff=$((current_time - last_check_time))
if [ "$time_diff" -lt "$MIN_INTERVAL" ]; then
    echo "$(date): Skipping check. Last check was $time_diff seconds ago." >> "$LOG_FILE"
    exit 0
fi

echo "$current_time" > "$CHECK_INTERVAL_FILE"

if ! ping -c 3 -W 2 $TARGET >/dev/null 2>&1; then
    echo "$(date): Unable to reach $TARGET. Restarting WAN..." >> "$LOG_FILE"
    ifdown wan
    ifup wan
else
    echo "$(date): Network is OK." >> "$LOG_FILE"
fi

if [ -f "$LOG_FILE" ] && [ $(stat -c%s "$LOG_FILE") -gt $MAX_SIZE ]; then
    echo "$(date): Log file size exceeded. Clearing log file." > "$LOG_FILE"
fi