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:
- Ethernet cable = eth0
- WIFI Hotspot = wlan0
- 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.
ip route
(WRT tools, note thatifconfig
is considered "obsolete", seeman 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. – goldilocks Mar 18 '22 at 19:12sudo dhclient -v wwan0
cmd? – Slev1n Mar 19 '22 at 09:21