53 lines
1.2 KiB
Bash
53 lines
1.2 KiB
Bash
#!/bin/sh
|
|
|
|
# Configuration
|
|
PING_HOST="1.1.1.1"
|
|
PING_WAIT=2
|
|
MAX_TRIES=4
|
|
|
|
# Function to log messages
|
|
wan_monitor() {
|
|
echo "WAN Monitor: $@" | /usr/bin/logger -s
|
|
}
|
|
|
|
# Function to restart the WAN interface
|
|
restart_wan_interface() {
|
|
wan_monitor "Force restarting WAN interface now..."
|
|
service "restart_wan_if 0" # Bring down the interface
|
|
sleep 10 # Wait for 10 seconds
|
|
service "restart_wan_if 1" # Bring up the interface
|
|
wan_monitor "WAN interface is restarted."
|
|
}
|
|
|
|
# Function to perform a ping test
|
|
ping_test() {
|
|
local count_tries=0
|
|
local ping_test_passed=0
|
|
wan_monitor "Running ping test..."
|
|
|
|
while [ $count_tries -lt $MAX_TRIES ]; do
|
|
if /bin/ping -c 1 -W $PING_WAIT $@ >/tmp/wan_check.log; then
|
|
ping_test_passed=1
|
|
wan_monitor "Ping test succeeded within $PING_WAIT secs."
|
|
exit 0
|
|
else
|
|
sleep 1
|
|
count_tries=$((count_tries + 1))
|
|
wan_monitor "Ping failed [$count_tries]"
|
|
fi
|
|
done
|
|
|
|
return $ping_test_passed
|
|
}
|
|
|
|
|
|
|
|
if [ $(ping_test $PING_HOST) -gt 0 ]; then
|
|
wan_monitor "Internet was reachable. No need to restart WAN."
|
|
else
|
|
wan_monitor "Pings failed. Internet must be down."
|
|
restart_wan_interface
|
|
fi
|
|
|
|
|