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 2: Install an SSH client on Windows
Step 3: Connect to your router via SSH using PuTTY
Step 4: Create the script file
Open PuTTY and run the following command:
Step 5: Enter Insert Mode
Press
Step 6: Copy and paste the script
Step 7: Save and exit
Step 8: Permit the script file to be executable
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)
Step 13: Self-Test
After completing the setup, unplug the WAN connection to simulate an internet outage and observe whether the script triggers a reboot correctly.
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.
Step 2: Install an SSH client on Windows
- Download and install PuTTY (https://www.putty.org/)
Step 3: Connect to your router via SSH using PuTTY
- 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.
Step 4: Create the script file
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
# Script to monitor internet connectivity and reboot the router if all ping attempts fail.
# Log file and self-check mechanism ensure efficient and safe operation.
# Log file location (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
# List of ping targets (space-separated IPs)
PING_TARGETS="8.8.8.8 1.1.1.1 9.9.9.9"
# Minimum time between reboots (in seconds, e.g., 30 minutes = 1800 seconds)
MIN_REBOOT_INTERVAL=1800
# File to store the timestamp of the last reboot
LAST_REBOOT_FILE="/jffs/last_reboot_time"
# Get the current time in seconds since the epoch
CURRENT_TIME=$(date +%s)
# Rotate the log file if it exceeds the maximum size
rotate_log() {
if [ -f "$LOG_FILE" ]; then
LOG_SIZE=$(stat -c %s "$LOG_FILE") # Get the size of the log file
if [ "$LOG_SIZE" -ge "$MAX_LOG_SIZE" ]; then
echo "Log file size exceeds limit. Rotating log..." > "$LOG_FILE"
fi
fi
}
# Self-check to prevent repeated reboots within the specified interval
if [ -f "$LAST_REBOOT_FILE" ]; then
LAST_REBOOT=$(cat "$LAST_REBOOT_FILE") # Read the last reboot timestamp
if [ $((CURRENT_TIME - LAST_REBOOT)) -lt "$MIN_REBOOT_INTERVAL" ]; then
echo "Last reboot was less than $((MIN_REBOOT_INTERVAL / 60)) minutes ago. Exiting script." >> "$LOG_FILE"
logger -t "$(basename $0)" "Last reboot was less than $((MIN_REBOOT_INTERVAL / 60)) minutes ago. Exiting script."
exit 0
fi
fi
# Log tag for identifying messages in the ASUS GUI system log
TAG="$(basename $0)"
# Start the ping test
echo "Starting the ping test at $(date)"
logger -t "$TAG" "Starting the ping test at $(date)"
# Loop through the list of 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..."
# Ping the target
ping -c 4 "$PING_TARGET" > /dev/null 2>&1
if [ "$?" -eq 0 ]; then # If ping succeeds
echo "Ping to $PING_TARGET succeeded."
logger -t "$TAG" "Ping to $PING_TARGET succeeded."
exit 0
fi
# Log failed attempt and increment the counter
COUNTER=$((COUNTER + 1))
rotate_log # Check if the log file 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)"
# Wait 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 the router
rotate_log # Check if the log file needs rotation
echo "All pings failed. Rebooting router at $(date)" >> "$LOG_FILE"
logger -t "$TAG" "All pings failed. Rebooting router at $(date)"
echo "Rebooting router due to failed ping attempts..." >> "$LOG_FILE"
logger -t "$TAG" "Rebooting router due to failed ping attempts..."
# Record the current time as the last reboot timestamp
echo "$CURRENT_TIME" > "$LAST_REBOOT_FILE"
# Allow time for the log to be written before reboot
sleep 10
service reboot
Step 7: Save and exit
- After pasting the script, press
Esc
- Type
:wq
and hitEnter
to save and exit.
Step 8: Permit the script file to be executable
- 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
Step 13: Self-Test
After completing the setup, unplug the WAN connection to simulate an internet outage and observe whether the script triggers a reboot correctly.
Last edited: