Hi Tim,
Great question - this comes up a lot. First, I am going to answer the general question of swapping networks for people and then get in to how to detect your network topology to know if you are on LTE.
Swapping Networks Cleanly
If your LTE Modem supports both Wifi Bridging and LTE
Many higher end LTE modems (Peplink, Cradlepoint, etc.) allow you to set up a priority of wifi networks which it will use to route internet traffic with and then, if there aren’t any stable routes, will fall back to LTE on the SIM card. If this is the case, you just have to set this up in the settings for your modem.
If your LTE Modem just connects to Cellular
For simpler modems which just have cellular connectivity, then you will need to set up simple routing yourself inside linux. It has more steps than this and depends on your exact use case, but these are the basic steps:
- Continuously connect using Wifi to a prioritized list of wifi networks
- Check if there is connectivity check wifi status and also ping the outside world
- If wifi disconnects or is connected but not able to get out, change the routing to put everything through eth0
- If wifi comes back, swap everything to go back through wlan0
Knowing where you are connected
If your router has a programmatic interface
Some routers have a local API (Usually accessible at http:/192.168.0.1/index.html for login) and possibly with a status page you can see information. A few of them, like Peplink (With their InControl API), actually have an API where you can view the status of WAN connections, cellular or otherwise. It also lets you pull bandwidth consumption data for each device.
If your router does not have a programmatic interface
It does get a little more tricky, but there are a few hacks.
Use Traceroute
traceroute [google.com](http://google.com/)
will tell you the IP address of each hop as data goes out. In my case, when I am on my wifi, it has one set of IPs and goes through known comcast.net servers. When I swap to an AT&T hotspot,
When connected to WIFI:
> traceroute [8](http://google.com/).8.8.8
1 192.168.0.1 (192.168.0.1) 3.005 ms 4.607 ms 5.202 ms
…
6 [be-297-ar01.santaclara.ca.sfba.comcast.net](http://be-297-ar01.santaclara.ca.sfba.comcast.net/) (96.108.99.13) 51.731 ms 53.496 ms 51.366 ms
…
When connected to LTE:
> traceroute [8](http://google.com/).8.8.8
1 172.20.10.1 (172.20.10.1) 4.431 ms 8.919 ms 1.906 ms
…
6 [sffca22crs.ip.att.net](http://sffca22crs.ip.att.net/) (12.122.3.70) 39.999 ms 60.329 ms 82.778 ms
…
The challenge in here overall is that this trace route is a slow script, but what it does mean is that you can do very simple pattern matching of both your local IP address and ones down the trail and cache it so that you can then just check if your IP changed. If you don’t have a classification for that IP address or address family, then you run trace route with some simple regex based on your LTE provider to differentiate the two, cache it and then use that data.