What's new

How to detect when WAN failover mode is activated on the router ?

  • 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!

SR-G

Occasional Visitor
Hello,

I have a RT-AC88U (with Asus Merlin firmware) at home, WAN connected through a fiber connection (via the "free" french network), on which i have quite often some shutdowns (it seems that it's vandalism and that some guys are unplugging everything in the neighborhood (sigh) ...)

So i configured Dual Wan, in failover mode, and everything is working fine (with a TP-LINK 4G router connected to LAN1).

But is there any way for me to easily set up "something" in order to be warned when the router is switching to the failover WAN ?

I don't see any built-in notifications about this, so is there something like an ASUS or ASUS-MERLIN API, or a SSH command maybe, that would easily allow me to remotely check this like twice a day (in order for me to be warned that my main connection is down and that i have to create a ticket for the situation to be solved by the provider ...) ? (i would cron a small script on one of my server to the check, for example)

Thanks in advance for any clue about this.
 
I don't use dual WAN. But if you configure DDNS on the router (even if you don't need it for remote access purposes), I would think you should be able to detect a change to the public IP associated w/ that domain name that is NOT typically associated w/ your ISP, strongly suggesting a changeover has occurred. That is unless a failover doesn't update DDNS (again, I don't use dual WAN, so I'm just assuming it does).
 
Check the following nvram variables to see which is the primary interface

nvram get wan0_primary
nvram get wan1_primary

The active wan will return '1'.....the inactive '0'
 
Thanks - it does the trick.

Current status on router :
ibuxhyN.png


Quick script to detect changes :

Code:
#!/bin/zsh

# nohup check-dual-wan-status.sh >/var/log/check-dual-wan-status.log 2>&1 & disown

PREVIOUS_WAN_PRIMARY=-1
PREVIOUS_WAN_SECONDARY=-1

DEST="<email>@<domain.tld>"
INTERVAL=3600

while [[ true ]] ; do
  ssh admin@192.168.8.1 /bin/sh << EOF | tr "\n" " " | read WAN_PRIMARY WAN_SECONDARY
    nvram get wan0_primary
    nvram get wan1_primary
EOF
  DATE=$(date "+%Y-%m-%d %H:%M")
  if [[ $PREVIOUS_WAN_PRIMARY -eq -1 && $PREVIOUS_WAN_SECONDARY -eq -1 ]] ; then
    echo "$DATE --- Initialization ... \n - Primary WAN detected as [$WAN_PRIMARY]\n - Secondary WAN detected as [$WAN_SECONDARY]"
  elif [[ $PREVIOUS_WAN_PRIMARY -ne $WAN_PRIMARY || $PREVIOUS_WAN_SECONDARY -ne $WAN_SECONDARY ]] ; then
    EXPLANATIONS=""
    [[ $PREVIOUS_WAN_PRIMARY -eq 1 && $WAN_PRIMARY -eq 0 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Primary WAN not connected anymore"
    [[ $PREVIOUS_WAN_PRIMARY -eq 0 && $WAN_PRIMARY -eq 1 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Primary WAN connected again"
    [[ $PREVIOUS_WAN_SECONDARY -eq 1 && $WAN_SECONDARY -eq 0 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Secondary WAN not connected anymore"
    [[ $PREVIOUS_WAN_SECONDARY -eq 0 && $WAN_SECONDARY -eq 1 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Secondary WAN connected again"

    echo "$DATE --- Something has changed ... : $EXPLANATIONS"
    echo "$DATE --- Something has changed ... : $EXPLANATIONS" | mail -v -s "[$(hostname)] Controls : check-dual-wan-status" "$DEST"

  else
    echo "$DATE --- No changes"
  fi
  PREVIOUS_WAN_PRIMARY=$WAN_PRIMARY
  PREVIOUS_WAN_SECONDARY=$WAN_SECONDARY
  sleep $INTERVAL
done
 
You can try using my Dual WAN Failover Script, it will use the built Alert Preferences that you get under AIProtection and email you when a Failover or Failback event occurs, it also can be disabled.

 
Thanks - it does the trick.

Current status on router :
ibuxhyN.png


Quick script to detect changes :

Code:
#!/bin/zsh

# nohup check-dual-wan-status.sh >/var/log/check-dual-wan-status.log 2>&1 & disown

PREVIOUS_WAN_PRIMARY=-1
PREVIOUS_WAN_SECONDARY=-1

DEST="<email>@<domain.tld>"
INTERVAL=3600

while [[ true ]] ; do
  ssh admin@192.168.8.1 /bin/sh << EOF | tr "\n" " " | read WAN_PRIMARY WAN_SECONDARY
    nvram get wan0_primary
    nvram get wan1_primary
EOF
  DATE=$(date "+%Y-%m-%d %H:%M")
  if [[ $PREVIOUS_WAN_PRIMARY -eq -1 && $PREVIOUS_WAN_SECONDARY -eq -1 ]] ; then
    echo "$DATE --- Initialization ... \n - Primary WAN detected as [$WAN_PRIMARY]\n - Secondary WAN detected as [$WAN_SECONDARY]"
  elif [[ $PREVIOUS_WAN_PRIMARY -ne $WAN_PRIMARY || $PREVIOUS_WAN_SECONDARY -ne $WAN_SECONDARY ]] ; then
    EXPLANATIONS=""
    [[ $PREVIOUS_WAN_PRIMARY -eq 1 && $WAN_PRIMARY -eq 0 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Primary WAN not connected anymore"
    [[ $PREVIOUS_WAN_PRIMARY -eq 0 && $WAN_PRIMARY -eq 1 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Primary WAN connected again"
    [[ $PREVIOUS_WAN_SECONDARY -eq 1 && $WAN_SECONDARY -eq 0 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Secondary WAN not connected anymore"
    [[ $PREVIOUS_WAN_SECONDARY -eq 0 && $WAN_SECONDARY -eq 1 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Secondary WAN connected again"

    echo "$DATE --- Something has changed ... : $EXPLANATIONS"
    echo "$DATE --- Something has changed ... : $EXPLANATIONS" | mail -v -s "[$(hostname)] Controls : check-dual-wan-status" "$DEST"

  else
    echo "$DATE --- No changes"
  fi
  PREVIOUS_WAN_PRIMARY=$WAN_PRIMARY
  PREVIOUS_WAN_SECONDARY=$WAN_SECONDARY
  sleep $INTERVAL
done
Hi, I'm interested to get this going.. so what will the script do? Send an email or so? If yes, wouldn't you need an SMTP server defined somewhere? Sorry, still learning.. I just as you need a quick notification if the failover happened without replacing the whole automated process by Asus too much.... Thanks :)
 
Hi, I'm interested to get this going.. so what will the script do? Send an email or so? If yes, wouldn't you need an SMTP server defined somewhere? Sorry, still learning.. I just as you need a quick notification if the failover happened without replacing the whole automated process by Asus too much.... Thanks :)

Well in the script you quoting in your reply, there is nothing magic : it's just about having system-wide something configured under the "mail" command, and that will take care of the technical connectivity against SMTP (hence why it's then straightforward inside the script).
On my side i use : https://wiki.archlinux.org/title/msmtp (with smtp host, login, TLS, ... configured in a ~/.msmtprc configuration file)
Otherwise the previous script is to be executed on any linux box (nas, ...), and then :
- is at periodic time connecting inside the ASUS router through SSH
- is checking what the current WAN is (primary or secondary)
- is sending a mail in case of a change

On my side i need this as my failover router is running a 4G SIM with a low amount of data (so i need to be careful about not generating too much download when the wan has been switched to the 4G router)

edit : slightly updated script with better self explanatory variables

Code:
#!/bin/zsh
# nohup check-dual-wan-status.sh >/var/log/check-dual-wan-status.log 2>&1 & disown

# Mail to which the notifications are going to be sent ("mail" command has to be configured correctly at system side, for example through mstmp)
DEST="serge.simon@gmail.com"

# IP of ASUS router. Login will be done through 'admin' account. SSH keys have to be published inside the ASUS ROUTER configuration, and SSH has of course to be activated
ASUS_ROUTER_IP=192.168.8.1

# Interval in seconds between two checks - 3600 = 1 hour
INTERVAL=3600

# Init of internal variables
PREVIOUS_WAN_PRIMARY=-1
PREVIOUS_WAN_SECONDARY=-1

echo "Starting dual wan status check against ASUS ROUTER IP [$ASUS_ROUTER_IP], interval [${INTERVAL}s], notifications will be sent to [$DEST]"

# Main loop
while [[ true ]] ; do
  ssh admin@${ASUS_ROUTER_IP} /bin/sh << EOF | tr "\n" " " | head -1 | read WAN_PRIMARY WAN_SECONDARY DUMMY
nvram get wan0_primary
nvram get wan1_primary
EOF
  DATE=$(date "+%Y-%m-%d %H:%M")
  if [[ $PREVIOUS_WAN_PRIMARY -eq -1 && $PREVIOUS_WAN_SECONDARY -eq -1 ]] ; then
    echo "$DATE --- Initialization ... \n - Primary WAN detected as [$WAN_PRIMARY]\n - Secondary WAN detected as [$WAN_SECONDARY]"
  elif [[ $PREVIOUS_WAN_PRIMARY -ne $WAN_PRIMARY || $PREVIOUS_WAN_SECONDARY -ne $WAN_SECONDARY ]] ; then
    EXPLANATIONS=""
    [[ $PREVIOUS_WAN_PRIMARY -eq 1 && $WAN_PRIMARY -eq 0 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Primary WAN not connected anymore"
    [[ $PREVIOUS_WAN_PRIMARY -eq 0 && $WAN_PRIMARY -eq 1 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Primary WAN connected again"
    [[ $PREVIOUS_WAN_SECONDARY -eq 1 && $WAN_SECONDARY -eq 0 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Secondary WAN not connected anymore"
    [[ $PREVIOUS_WAN_SECONDARY -eq 0 && $WAN_SECONDARY -eq 1 ]] && EXPLANATIONS="${EXPLANATIONS}\n - Secondary WAN connected again"

    echo "$DATE --- Something has changed ... : $EXPLANATIONS"
    echo "$DATE --- Something has changed ... : $EXPLANATIONS" | mail -v -s "[$(hostname)] Controls : check-dual-wan-status" "$DEST"

  else
    echo "$DATE --- No changes"
  fi
  PREVIOUS_WAN_PRIMARY=$WAN_PRIMARY
  PREVIOUS_WAN_SECONDARY=$WAN_SECONDARY
  sleep $INTERVAL
done



and service systemctl unit file (systemctl daemon-reload ; systemctl enable check-dual-wan-status.service ; systemctl start check-dual-wan-status.service)

Code:
[Unit]
Description=Check Dual Wan status inside ASUS ROUTER

[Service]
ExecStart=/home/bin/check-dual-wan-status.sh

[Install]
WantedBy=multi-user.target
 
Last edited:

Latest threads

Sign Up For SNBForums Daily Digest

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