What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Tutorial Automatically Reboot Asus Router on Network Failures

@Net Noob try this:

1. Variable interface name (dynamic)
Hardcoded "wgs1_enable" (not dynamic)
ENABLED=$(nvram get wgs1_enable)

to

ENABLED=$(nvram get "${IF}_enable")

2. Incorrect NVRAM Check (s/b 1 or 0)
Checking for value "2"
if [ -z "$2" ] || [ -z "$ENABLED" ] || [ "$ENABLED" -ne "2" ] ; then

to

if [ "$ENABLED" -eq 1 ]; then

3. Cleanup/simplify
if [ -z "$1" ] || [ -z "$ENABLED" ] || [ "$ENABLED" -ne "1" ] ; then
# ...
if [ -z "$1" ] ; then
# ...
fi
fi

to

if [ "$ENABLED" -ne 1 ]; then
# Deactivate
else
# Activate
fi

4. Add service restart
service restart_wgs
service restart_dnsmasq

5. Act/Deact hardcoded to variable
nvram set wgs1_enable=0
nvram set wgs1_enable=1

to

nvram set "${IF}_enable=0"
nvram set "${IF}_enable=1"
 
Last edited:
@ColinTaylor , @ZebMcKayhan , @bg604 ,

Thank you so much for your input. Apologies for taking so long. Iv finally come up with something which i think works pretty well:

Code:
#!/bin/sh

# Number of ping attempts per target
ATTEMPTS=5

# Log tag for identifying messages in the ASUS GUI system log
TAG="$(basename $0)"

# 1. Check Wireguard server and android status - If android is not on LAN & Wireguard server is on - Exit script

#    logger -t "$TAG" "Checking Wireguard Server Status..."
        ENABLED=$(nvram get wgs_enable)
            if [ "$ENABLED" == 1 ] ; then
                ping -c 1 -w 1 192.168.50.241 > /dev/null 2>&1
                    if [ "$?" -ne 0 ]; then
                        logger -t "$TAG" "Android Device Offline... Wireguard Server: Active."
                        exit 0
                    fi
            fi

# If above condition is not met - ping for android phone - 5 attempts, 45 seconds between each attempt

COUNTER=0

    while [ "$COUNTER" -lt "$ATTEMPTS" ]; do
        logger -t "$TAG" "Detecting Android Device... Ping Attempt $((COUNTER + 1))"

# Ping the target

        ping -c 1 -w 1 192.168.50.241 > /dev/null 2>&1

# If ping succeeds - check if Wireguard server is active, if not, exit. If it is, switch it off and exit.
        
            if [ "$?" -eq 0 ] ; then
            ENABLED=$(nvram get wgs1_enable)
                if [ "$ENABLED" == 0 ] ; then
                logger -t "$TAG" "Android Device Detected. Wireguard Server: Offline."
                exit 0

                    else
                    logger -t "$TAG" "Android Device Detected. Wireguard Server: Deactivating."
                    nvram set wgs1_enable=0
                    service restart_wgs
                    service restart_dnsmasq
                    exit 0
                    fi
                fi



# If ping fails - log failed attempt and increment the counter

        COUNTER=$((COUNTER + 1))
        logger -t "$TAG" "Ping Unsuccessful... Next Attempt In 45 Seconds..."
    
# Wait 45 seconds before the next attempt

        sleep 45

#Restart loop if counter is less than 5, otherwise exit loop and continue

    done

# If all pings fail... Activate the Wireguard server and exit

    logger -t "$TAG" "Android Device Detection Failed... Wireguard Server: Activating."
    nvram set wgs1_enable=1
    service restart_wgs
    service restart_dnsmasq
    killall smbd
    smbd -D
    exit 0

Its a simple little thing but iv learned a lot :)

Iv used smbd on this when starting the wireguard server because that is what happens on the AX86U when you turn on the Wireguard server from the GUI. Iv tried to emulate what happens in the GUI system log as much as possible.

PLEASE BE AWARE - Iv hardcoded the wireguard server interface and this script for use on the AX86U. You may have to change the script depending on which model of Asus router you use.

All that would be left is to add a cron job to services-start to run this whenever it is required. Iv got it running every 15 mins.

e.g

cru a wgcontrol "*/15 * * * * /jffs/scripts/wgcontrol"

Please do point out any mistakes or additions which would make the script better.

Thank you everyone for your help and input. Special thanks to @bg604 for setting me off on this tangent :)
 
Iv used smbd on this when starting the wireguard server because that is what happens on the AX86U when you turn on the Wireguard server from the GUI. Iv tried to emulate what happens in the GUI system log as much as possible.
There should be no need to explicitly restart smbd as that happens as part of service restart_wgs.
 
The common advice for ISP outages is to reboot the modem, not the router.
I used to do that but later discovered my network issues were resolved when i addressed simpler issues first. You see, my router and cable modem take "forever" to reboot and so, for me, i'd rather deal with a fast restarting service than to wait 3 to 5 minutes for the hardware to restart. ;)

My python script, running within the router, monitors the network connectivity every 30 secs:

Connectivity Check Order

1. Check WAN Interface (eg. ip link show eth0)
** This command directly checks the physical ethernet connection providing immediate insight into whether it is UP or DOWN such as an unplugged cable. This step wont move to the next steps if the state is DOWN. For example, when i intentionally unplug the ethernet cable. By not proceeding with the next steps (which would fail anyway) and not proceeding with the Internet Down / Recovery process, it avoids/prevents the router and cable modem from rebooting when i'm likely trying to deal with another issue.

2. Ping Check (eg. ping -c 2 8.8.8.8 ):
** Attempt to ping external IPs. If ok move to the next step. If not ok, jump to Internet Down / Recovery

3. DNS Check (eg. dig +short google.com)
** Attempt to do a DNS check on different domains. If not ok, jump to Internet Down / Recovery

If all of the above are OK then internet is working. I used to do an HTTP connection but figured the above was reliable enough.

#####

Internet Down / Recovery Process

1. If DNS Check fails then run: service restart_dnsmasq
** Check internet. If still down, move to next step.

2. Renew Modem's DHCP info: killall -USR1 udhcpc
** Check internet. If still down, move to next step.

3. Restart WAN Interface: service restart_wan
** Check internet. If still down, move to next step.

4. Reboot router: reboot
** Before rebooting I store a file into /jffs so that when the script is relaunched it will know the router was already restarted. At that time it will check the internet again. If the internet is still not working, we move to the next step.

5. Restart modem: ** API call to Kasa Smart Plug HS103 to power cycle the Cable Modem **
** The script keeps track of when the modem was last rebooted -- it will try power cycling every 15 mins.

6. The script keeps checking the internet every 30 secs. If it fails it will continue to retry step #5 when the time is right. It wont bother with steps #1 to #4 since we now assume the problem is with the ISP / Modem connectivity.

The above has worked well for me. Minimal downtime. Often the resolution has been Recovery step #1 through to #3. I have rarely made it down to step #4 to reboot the router. I used to have Unbound installed and that caused many issues. Step #1 DNS check is able to check Unbound upstatus and if (and when) it was down to restart it; this would resolve 99% of my issues. I've since uninstalled Unbound. It was too problematic for me.
 
Last edited:
I did a similar script a few years ago when I was having loads of connectivity problems.
I didn’t use python, just PlainOldBash.

@Net-Noob - as @Tech9 mentioned, sometimes it’s the WAN modem and not the router.
@copperhead uses a progressive step approach. It is similar (but much more through) then the steps I took. I simply pinged some public DNS servers, if failure, attempt router reboot, set flag. If pings still fail - power cycle WAN modem using the exact Kasa Smart Switch mentioned above.
Served me well when I was having numerous connectivity issues (turned out to be a cable…)

Some notes:
1) You can use an old uKasa addon to power cycle the modem:
1a) I would ONLY recommend the Kasa HS103 plug. The newer, SmartThings plugs no longer work with the older Kasa api.
1b) Kasa smart power strips also don’t work well. Stick with the HS103 plug.

2) We should probably move this thread to the Addon area

3) As @Maverickcdn warned, welcome to the world of Asuswrt-Merlin scripting - soon you will be coming up with all kinds of ways to torture yourself and router ;-)
 
I never had to reboot the router on WAN down event. I currently have three residential ISP lines in use. It's always the modem after scheduled ISP maintenance and not after every maintenance. If the router is causing this - it has an issue. Rebooting the router to restore WAN in my understanding is the same band-aid solution like scheduled reboots to keep it going.
 

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Back
Top