-1

In my current setup I have a RPI 4B, which is connected to a sensor. Every now and then the raspi has to upload the sensor data into a databank. The deployment scenarios of this system are quite versatile. Currently we have either internet access via a UMTS Stick with a SIM card or via a WIFI hotspot. The wifi hotspot functionality is also used for SSH communication/debugging. As a next step we also want to allow for ethernet usage. The implementation of the functioniality will be done via python 2.7. The challenge is now, if everything is present at once (very unlikely, but possible) each interface, some of them or none could have currently access to the internet.

The interfaces would be as follows:

  1. Ethernet cable = eth0
  2. WIFI Hotspot = wlan0
  3. Stick = wwan0 (using sudo dhclient -v wwan0)

What I am looking for is a best practice to handle the beforementioned scenario. What I was thinking about is a routine doing the following to get one interface having internet access and "remove" the others:

  • Checking which interfaces are currently up using the ifconfig cmd for example.
  • Then, I could shutdown interface wlan0 and wwan0 using sudo ifconfig wlan0/wwan0 down.
  • Use the ping cmd to see if I can ping google or whatever to ensure this interface provides internet access.
  • If this fails, i remove the eth0 inteface and use sudo ifconfig wlan0 up and check again...

This routine is repeated periodically or before uploading data. If no internet access is available via any interface the device waits for some minutes and repeats the routine again.

Any comment/support is very welcome since I am quite new to this networking topic.

EDIT: After some testing, I would say it runs smoother then expected. I provided all three options to the raspi and pinged google.com. And disabled first ethernet, than wifi and successfully pinged google every time. To ensure that the traffic is not running over the pay-to-use gateway (UMTS stick) I simply disable this interface except no internet connection is available by the other two interfaces. There might certainly be some edge cases I will have to test. I will report back if I will find some issues during these tests.

Slev1n
  • 1
  • 3
  • 1
    Which interface is used by what is determined by the [routing table](https://en.wikipedia.org/wiki/Routing_table), which you can examine and manipulate via `ip route` (WRT tools, note that `ifconfig` is considered "obsolete", see `man ifconfig`). So it is possible to have multiple interfaces up online and control at a system level which one is used for internet traffic. If you want finer grained control (eg., using different interfaces for different applications), look into [network namespaces](https://en.wikipedia.org/wiki/Linux_namespaces#Network_(net)). – goldilocks Mar 18 '22 at 19:12
  • You are using a collection of obsolete tools (programming and networking). There is no reason this can't be done with the default Raspberry Pi OS network manager dhcpcd - no programming needed. – See [How to set up networking/WiFi](https://raspberrypi.stackexchange.com/a/37921/8697) – Milliways Mar 18 '22 at 23:01
  • The other answers mention routing table and metrics. While it is possible to modify the routing table you should not as most network managers manage the routing table. In particular dhcpcd applies default metrics (dependent on interface type) but has a directive to change metric. – Milliways Mar 19 '22 at 03:15
  • Thank you ALL for your answers. @goldilocks I will have a look at your link. @Milliways thank you for your link. I read through it and learned a lot. But what do you mean by "you are using a collection obsolete tools". I guess with programming you refer to python 2.7 but what do you mean by networking tools? The `sudo dhclient -v wwan0` cmd? – Slev1n Mar 19 '22 at 09:21

1 Answers1

1

If you have no preference on what interface is used, let the Pi's routing table sort it out. If you do have preferences, use ifmetric to set, for example, the ethernet as lowest, then the WiFi and the UMTS as the highest. That is, of course, if your network is configured correctly.

If you ifconfig wlan0 down, then your ssh access is also gone, so that is a bad idea.

You could keep the UMTS down, for example because it is a pay-per-use service, and configure it only if the database server is not available.

Also, you should probably ping the databank, not some generic address on the Internet. For the upload, you don't care whether Google is reachable, as long as you can upload to the databank.

As for your choice of Python 2.7: The Python Software Foundation declared Python 2 End of Life (EOL) on January 1, 2020, and ended support. Python 2.7. 18 was the final release of Python 2, released in April 2020. No version of Python 2 will receive updates, not even for critical security vulnerabilities.

The best solution depends on the networks that you connect to. There are just too many unknowns here, and if you want to support them all. To name a few:

  • Do all the networks support DHCP? Does DHCP deliver a gateway and name-server?
  • Are the ethernet and WiFi (always) the same subnet, and therefore do they have the same gateway?
  • If you plan to use multiple gateways, you could look at iproute2 or to the answer to https://serverfault.com/q/377062/454038.
  • It looks to me, that it is a good idea to leave he UMTS down, unless you cannot reach the databank.
Ljm Dullaart
  • 2,301
  • 7
  • 13
  • Thanks for your answer. You are right setting wlan0 down is not that good an idea. And pinging AWS might be better than google. Regarding python, our device is already deployed and we are not sure regarding updating python to 3.x due to either in-the-field updates via bad internet connections or legacy issues. The ìfmetric` command seems interesting. I will try to get familiar with it. Ethernet or WIFI should have a priority over UMTS since it is pay-per-use. But what happens if there are eth0 and wlan0 interfaces up though no internet connection via those interfaces? Is then wwan0 used? – Slev1n Mar 19 '22 at 09:35
  • [quote]Do all the networks support DHCP? Does DHCP deliver a gateway and name-server?[/quote] I cannot answer these questions thoroughly but neglecting edge cases, I would say that using the UMTS Stick - YES to both. Wifi hotspot or wired ethernet should always be provided by the customer's router directly. Thus, I would say - YES to both again. [quote]Are the ethernet and WiFi (always) the same subnet, and therefore do they have the same gateway?[/quote] Yes, at least we can arrange for this by telling the customer to only use one router - same gateway. – Slev1n Mar 21 '22 at 09:42