I wrote a script to automatically detect internet outages and reboot the router if needed, ensuring minimal downtime.
It monitors internet connectivity by pinging multiple well-known IP addresses (Google DNS, Cloudflare, and Quad9) up to three times each. If any of the pings succeed, the script exits.
If all attempts fail, the script triggers a reboot of the router, which helps address common connectivity issues. This approach ensures the router's settings are refreshed and can help resolve certain network problems without manual intervention.
Please let me know what you think, first time doing something like this.
Step 1: Enable Persistent JFFS2 partition and SSH
Step 5: Enter Insert Mode
Press
Step 6: Copy and paste the script
Step 7: Save and exit
Step 10: Test the script as-is (run manually)
Step 11: Reboot router and verify the Cron Job
1. Reboot router
2. After rebooting the router, check if the cron job is active by running:
You should see:
Step 12: View the log
1. Navigate to the /jffs directory:
2. View the entire log file:
3. Clear the log (Optional)
It monitors internet connectivity by pinging multiple well-known IP addresses (Google DNS, Cloudflare, and Quad9) up to three times each. If any of the pings succeed, the script exits.
If all attempts fail, the script triggers a reboot of the router, which helps address common connectivity issues. This approach ensures the router's settings are refreshed and can help resolve certain network problems without manual intervention.
Please let me know what you think, first time doing something like this.
Step 1: Enable Persistent JFFS2 partition and SSH
- Log into Asus router –
192.168.xxx.xxx
and log in using your credentials. - Navigate to Administration -> System tab.
- In Persistent JFFS2 partition section:
- Enable JFFS custom scripts and configs – Yes.
- In Service section:
- Enable SSH - LAN only.
- Allow SSH Port Forwarding - No.
- Set SSH Port - 22.
- Enable Allow Password Login - Yes.
- Leave Authorized Keys blank.
- Set Idle Timeout - 20.
- Download and install PuTTY (https://www.putty.org/)
- Open PuTTY.
- Enter Host Name:
192.168.xxx.xxx.
- Set Port:
22
- Select Connection:
SSH
- Click Open and confirm the security alert.
- Log in using your Asus router credentials.
- Open PuTTY and run the following command:
Bash:
vi /jffs/scripts/ping_reboot.sh
Step 5: Enter Insert Mode
Press
i
to enter insert mode in the editorStep 6: Copy and paste the script
Bash:
#!/bin/sh
# Log file location on /jffs (persistent across reboots)
LOG_FILE="/jffs/ping_reboot.log"
# Maximum log size in bytes (e.g., 512KB = 524288 bytes)
MAX_LOG_SIZE=524288
# Number of ping attempts per target
ATTEMPTS=3
# Ping targets (space-separated instead of an array)
PING_TARGETS="8.8.8.8 1.1.1.1 9.9.9.9"
# Function to rotate the log if it exceeds the max size
rotate_log() {
if [ -f "$LOG_FILE" ]; then
LOG_SIZE=$(stat -c %s "$LOG_FILE") # Get file size
if [ "$LOG_SIZE" -ge "$MAX_LOG_SIZE" ]; then
echo "Log file size exceeds limit. Rotating log..." > "$LOG_FILE"
fi
fi
}
TAG="$(basename $0)" # Use the script filename as the log tag
echo "Starting the ping test at $(date)"
logger -t "$TAG" "Starting the ping test at $(date)" # Log to ASUS GUI system log
# Loop through the ping targets
for PING_TARGET in $PING_TARGETS; do
echo "Pinging target: $PING_TARGET"
logger -t "$TAG" "Pinging target: $PING_TARGET"
COUNTER=0
while [ "$COUNTER" -lt "$ATTEMPTS" ]; do
echo "Attempt $((COUNTER + 1)) to ping $PING_TARGET..."
logger -t "$TAG" "Attempt $((COUNTER + 1)) to ping $PING_TARGET..."
# Try to ping the target
ping -c 4 "$PING_TARGET" > /dev/null 2>&1
# Check if the ping was successful
if [ "$?" -eq 0 ]; then
echo "Ping to $PING_TARGET succeeded."
logger -t "$TAG" "Ping to $PING_TARGET succeeded."
exit 0
fi
# Increment the counter if ping failed
COUNTER=$((COUNTER + 1))
rotate_log # Check if log file size needs rotation
echo "Ping to $PING_TARGET failed (attempt $COUNTER) at $(date)" >> "$LOG_FILE"
logger -t "$TAG" "Ping to $PING_TARGET failed (attempt $COUNTER) at $(date)" # Log failure to ASUS GUI system log
# Wait for 5 seconds before the next attempt
echo "Waiting 5 seconds before the next attempt..."
sleep 5
done
done
# If all pings fail, log the failure and reboot
rotate_log # Check if log file size needs rotation
echo "All pings failed. Rebooting router at $(date)" >> "$LOG_FILE"
logger -t "$TAG" "All pings failed. Rebooting router at $(date)" # Log reboot to ASUS GUI system log
echo "Rebooting router due to failed ping attempts..." >> "$LOG_FILE"
logger -t "$TAG" "Rebooting router due to failed ping attempts..." # Log reboot message
sleep 10 # Allow time for the log to be written
service reboot
Step 7: Save and exit
- After pasting the script, press
Esc
- Type
:wq
and hitEnter
to save and exit.
- Run the following command:
Bash:
chmod +x /jffs/scripts/ping_reboot.sh
Step 9: Add the Persistent Cron Job
- Open the
services-start
file for editing:
Bash:vi /jffs/scripts/services-start
- Enter insert mode by pressing
i
and add the following lines:
Bash:#!/bin/sh cru a PingReboot "*/5 * * * * /jffs/scripts/ping_reboot.sh"
- Save and exit the file by pressing
Esc
, typing:wq
, and pressingEnter
. - Make the services-start script executable:
Bash:chmod +x /jffs/scripts/services-start
Step 10: Test the script as-is (run manually)
Bash:
cd /jffs/scripts
Bash:
./ping_reboot.sh
Output should be:
Starting the ping test at DATE
Pinging target: 8.8.8.8
Attempt 1 to ping 8.8.8.8...
Ping to 8.8.8.8 succeeded.
Step 11: Reboot router and verify the Cron Job
1. Reboot router
2. After rebooting the router, check if the cron job is active by running:
Bash:
cru l
PingReboot = */5 * * * * /jffs/scripts/ping_reboot.sh #PingReboot#
Step 12: View the log
1. Navigate to the /jffs directory:
Bash:
cd /jffs
2. View the entire log file:
Bash:
cat ping_reboot.log
3. Clear the log (Optional)
Bash:
> ping_reboot.log
Last edited: