-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 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,539
  • 11
  • 16