Simple - scripted - automated failover pppoe-wan - 3G modem

Hi,

I've been looking for a simple and elegant solution to switch between the wan uplink and the 3g modem when the default pppoe-wan disconnects. I know there are some OpenWRT tutorials and solutions, I read them, found them complicated and considered using crond with a simple script to do the failover. Worth mentioning that I'd like to achieve this on both OpenWRT 18.x & 19.x

The ppoe-wan is the default interface, enabled on boot, whereas the 3G interfaced is disabled (Bring up on boot - in LuCI unchecked, otherwise it will connect automatically and register as default interface - default wan route).
I learned that the following checks are unreliable:

# cat /sys/class/net/pppoe-wan/operstate
unknown

And that pppoe-wan is somehow "hidden" inside the wan interface:

#ubus list network.interface.*
network.interface.lan
network.interface.loopback
network.interface.wan
network.interface.wifi

That's for the OpenWRT system. As for the kernel, all interfaces are listed and available in /proc/net/dev

I created the following script (I'm about to run every minute or so as a cron job):

#!/bin/sh

PPPOE=`/bin/grep "pppoe-wan" /proc/net/dev`
3G-UMTS=`/bin/grep "3g-UMTS" /proc/net/dev`
if  [ -n "$PPPOE" ] ; then
  if [ -n "$3G-UMTS" ] ; then
   /sbin/ifdown 3g-UMTS
   /usr/bin/logger -t FAILOVER PPPoE up - disabling 3G uplink
  fi
 else
  if [ -c "/dev/ttyUSB2" ] ; then
   /sbin/ifup 3g-UMTS
   /usr/bin/logger -t FAILOVER PPPoE down - enabling 3G uplink
  fi
fi
unset PPPOE
unset 3G-UMTS

And learned that I cannot use ifup/ifdown to control the ppoe-wan & 3g-UMTS interfaces.
This guide wasn't really helpful:
https://openwrt.org/docs/guide-user/base-system/uci#get_wan_interface
I'm stuck now, not knowing how to bring up / down the 3g Interface when needed.

I'll be thankful if someone more "UCI-minded" could provide me some UCI commands (sequences) for activating / deactivating the 3g-UMTS interface when needed, so that I could use them inside my script, substituting the nonfunctional ifup/ifdown.

Thanks in advance.

P.S. I'm only looking for this solution, please don't bother to suggest other alternatives like multi-wan & mwan3 & all the complex stuff I already read in the OpenWRT doc sections. Thank you, but no thank you (don't have time for that stuff).

You can assign a higher metric and will be a backup connection for as long as pppoe is up.
However nor this solution, or yours take into account the fact that the connection might be up but without traffic passing.
Since you don't want to discuss about mwan3, I'll just leave those here and be on my way.

1 Like

@trendy

mwan3 and the whole configuration I found complicated for such a simple feature. Honestly.

Well, I got some inspiration from this older post:

And managed to make my simple script functional. Functional from the OpenWRT/UCI system PoV, but wrong from the PoV of the nested if statements, I'll need to inspect them again. Check if there are some limitations in -ash or if I did some mistakes (syntax) on my own.
Here is the functional but "illogical" script:

#!/bin/sh

PPPOE=`/bin/grep "pppoe-wan" /proc/net/dev`
3G-UMTS=`/bin/grep "3g-UMTS" /proc/net/dev`
if  [ -n "$PPPOE" ] ; then
  if [ -n "$3G-UMTS" ] ; then
   /bin/ubus call network.interface.UMTS down
   #cycle wan to get back the ppoe-wan default route
   /sbin/ifdown wan
   /sbin/ifup wan
   /usr/bin/logger -t FAILOVER PPPoE up - disabling 3G uplink
  fi
 else
  if [ -c "/dev/ttyUSB2" ] ; then
   /bin/ubus call network.interface.UMTS up
   /usr/bin/logger -t FAILOVER PPPoE down - enabling 3G uplink
  fi
fi
unset PPPOE
unset 3G-UMTS

Actual "logical" behavior on every execution:

  • if wan is disconnected then it connects the 3G - every time!
  • if wan is connected it disconnects the 3G and reconnects the wan - every time!

P.S. An observation: If you follow the OpenWRT docs, then the 3G interface will be called wwan and not UMTS. UMTS is the name I used when I defined it. For instance, the actual:
/bin/ubus call network.interface.UMTS up
will be:
/bin/ubus call network.interface.wwan up
The exact name is to be found in /etc/config/network In my case is it was:

config interface 'UMTS'
        option proto '3g'
        option device '/dev/ttyUSB2'
        option service 'umts_only'

i used this simple approach some time ago.. i also think that mwan3 way to complicated for a simple task.. just my 2 cents :slight_smile:

https://forum.archive.openwrt.org/viewtopic.php?id=26767

cheers

1 Like

@yitida

I also considered to do an actual connectivity check with ping per interface, but failed to find a way to properly test the reply - send multiple pings and check if at least 50% were successful (got a reply).
My first draft was something like:

/bin/ping -c 1 -w 1 -I pppoe-wan 1.1.1.1 &>/dev/null
if [ $? -eq 1 ] ; then
...

Anyway, found out what was wrong with my script from the previous post - multiple issues, grep & logic (conditional checking) related.
Here is the final & working script:

#!/bin/sh

if /bin/grep -ql pppoe-wan /proc/net/dev ; then
  if /bin/grep -ql 3g-UMTS /proc/net/dev ; then
   /bin/ubus call network.interface.UMTS down
   #cycle wan to get back the ppoe-wan default route
   /sbin/ifdown wan
   /sbin/ifup wan
   /usr/bin/logger -t FAILOVER PPPoE up - disabling 3G uplink
  fi
 else
  if /bin/grep -ql 3g-UMTS /proc/net/dev ; then
   exit 0
  else
   if [ -c "/dev/ttyUSB2" ] ; then
    /bin/ubus call network.interface.UMTS up
    /usr/bin/logger -t FAILOVER PPPoE down - enabling 3G uplink
   fi
  fi
fi

The only issue I'm facing now is failing to silence crond - it's polluting my syslog on every execution. Tried already the following syntax in LuCI and didn't work:

* * * * * /etc/failover > /dev/null 2>&1

P.S. OK, managed to silence cron by setting: option cronloglevel '9' in /etc/config/system
I was following the advice from here:

well i was using ping strategy since my main connection was not pppoe and sometimes there was no connectivity even if the link was ok.. but if you solved we are all happy :slight_smile: just for referce:

TEST=`ping -I eth0 -w 10 -c 6 1.1.1.1 | awk '/received/ {print $4}'`

P=3

if [ "$TEST" -ge "$P" ]; then
...

also i had simple ifttt code to get a mail notification on my phone, pretty nice "useless" addition :stuck_out_tongue:

cheers

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.