supertp
Posts: 23
Joined: Tue Apr 10, 2018 3:50 pm

RPI connectivity to internet via different interfaces and SSIDs

Fri Jun 14, 2019 7:09 am

I have developed an alarm system using an Rpi.

For its connectivity to the internet it can be connected via the ethernet or via the WiFi NICs.

The ethernet connectivity is provided via a DSL router.

The WiFi can be provided via the same router1 (SSID1) but another SSID (SSID2) via another router2 is also available "on demand" and also configured at the RPI.

In case the DSL router gets out of service, i.e. the ethernet and the WiFi connections go out of service, I expect that the RPI will auto connect via SSDI2 to router2.

The alarm app running is in Python 3 and is checking at intervals whether there is connectivity (ping response) to www.google.com to check whether internet access is available.

In case the router1's WAN access is unavailable but the LAN is ok, normally the RPI will remain connected via Ethernet (or via SSID1) but the app will have no internet connectivity.

What I need in this case is the RPI to connect via router2-SSID2.

Can this be configured at OS-Raspbian level to be done automatically or will I have to do this in Python and with os commands to switch between the interfaces and SSIDs?

The order in which I need the RPI to gain internet access is

1. Ethernet cable to router1
2. WiFi to SSID1 of router1
3. WiFi to SSID2 of router2

Thank you in advance

epoch1970
Posts: 3325
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: RPI connectivity to internet via different interfaces and SSIDs

Fri Jun 14, 2019 9:37 am

The fact google.com or any other site is not responding only matters to the application, you'll need to switch configs yourself from your python program.
I am not good (at all) at python but I know what the program has to do: get a hold of wpa_supplicant and steer it to connect to the alternate AP.

wpa_supplicant has a cli interface that works in batch mode for simple commands (eg "wpa_cli -i wlan0 terminate") or interactively for compound commands. More scriptable I think is the D-Bus API that wpa_supplicant also has.
I think I've seen python libraries for wpa_supplicant management, too. No idea about coverage/quality.

In your case perhaps a simple system call to "wpa_cli -i wlan0 select_network 1" (assuming 2 network blocks in wpa_supplicant-wlan0.conf) could be enough?
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

supertp
Posts: 23
Joined: Tue Apr 10, 2018 3:50 pm

Re: RPI connectivity to internet via different interfaces and SSIDs

Fri Jun 14, 2019 9:52 am

epoch1970 wrote:
Fri Jun 14, 2019 9:37 am
The fact google.com or any other site is not responding only matters to the application, you'll need to switch configs yourself from your python program.
I am not good (at all) at python but I know what the program has to do: get a hold of wpa_supplicant and steer it to connect to the alternate AP.

wpa_supplicant has a cli interface that works in batch mode for simple commands (eg "wpa_cli -i wlan0 terminate") or interactively for compound commands. More scriptable I think is the D-Bus API that wpa_supplicant also has.
I think I've seen python libraries for wpa_supplicant management, too. No idea about coverage/quality.

In your case perhaps a simple system call to "wpa_cli -i wlan0 select_network 1" (assuming 2 network blocks in wpa_supplicant-wlan0.conf) could be enough?

Thanks for the feedback. Clear that the application has to handle how to access the internet.
A system call to switch between SSID1 and SSID2 would be fine.
But what about switching between Ethernet cable NIC and WiFI NIC? Is there a similar cli command available?

epoch1970
Posts: 3325
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: RPI connectivity to internet via different interfaces and SSIDs

Fri Jun 14, 2019 11:04 am

supertp wrote:
Fri Jun 14, 2019 9:52 am
But what about switching between Ethernet cable NIC and WiFI NIC? Is there a similar cli command available?
I'm unclear about the concern. By default on Raspbian and I imagine other systems if both interfaces are connected to the same network, eth has priority over wifi. This is done via route metric.
If the ethernet cable gets disconnected I don't recall exactly what Raspbian does, I assume it brings the interface down, so only the routes over wifi remain.

I would try to avoid being too specific about which interface the packets are using.
It is said that on Linux "the address belongs to the host" (mostly IP, but also MAC addresses). Basically, you give a destination and linux selects the best route/interface for you. You can get a ping on one interface and pong from an other one in the host. Fuzzy behaviour, but it works in most cases.
On systems like BSD (and variants) the addresses clearly belongs to the interface. The behaviour is more defined but actual connectivity is not necessarily better if you use the wrong interface.
(lookup "network [strong|weak] host model" for a better description of the differences.)
Point is, if you turn linux into a strong host, you're going to have to start using kernel tunables and "source-based routing" that are not mainstream and your settings might clash with the way a general purpose distribution (and I suspect systemd) expects things to work. Strong host linux is better used in router distros or appliances where the OS is specific.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

supertp
Posts: 23
Joined: Tue Apr 10, 2018 3:50 pm

Re: RPI connectivity to internet via different interfaces and SSIDs

Fri Jun 14, 2019 11:31 am

epoch1970 wrote:
Fri Jun 14, 2019 11:04 am
I'm unclear about the concern. By default on Raspbian and I imagine other systems if both interfaces are connected to the same network, eth has priority over wifi. This is done via route metric.
If the ethernet cable gets disconnected I don't recall exactly what Raspbian does, I assume it brings the interface down, so only the routes over wifi remain.
Ok, the issue is that the Ethernet NIC may be connected (LAN available) but with no internet connectivity (router1 WAN disconnected).
The Ethernet NIC in this case will remain connected (will not switch to WiFi NIC) as connectivity to the LAN/router1 exists, though without internet access. Correct?

So in this case, I want to make it the RPI to switch to the WiFi NIC. (e.g. via sudo ifconfig eth0 down && ifconfig eth1 up).

In this case the WiFi NIC will be enabled. Normally, it will connect again to SSID1. Similarly, it will get connected to the LAN (router1) but will not have internet connectivity.

So in this case, I'll have to make it (wpa cli commands you mentioned) to switch to SSID2 (router2).

So my understanding is that I'll have to cause the above (via a Python called cli command), using the corresponding cli commands to switch/toggle between NICs and SSIDs (SSIDs using the same WiFi NIC).

Right?

All the above to cover the case in which both routers router1 and router2 are available, providing LAN, RPI is connected to router1 via either cable or Wifi BUT WAN/internet access via router1 is not available. So I'll have to somehow switch to router2.

epoch1970
Posts: 3325
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: RPI connectivity to internet via different interfaces and SSIDs

Fri Jun 14, 2019 12:11 pm

supertp wrote:
Fri Jun 14, 2019 7:09 am
The ethernet connectivity is provided via a DSL router.
The WiFi can be provided via the same router1 (SSID1) but another SSID (SSID2) via another router2 is also available "on demand"
If the ethernet switch, wifi AP, dhcp server, router are colocated, I don't see why eth0 could enter into zombie mode alone. I imagined you had both links eth0 and wlan0-SSID1 up at the same time, and would use the system-provided route priority based on metric (or automatic interface deletion).

Assuming the improbable, you can easily manipulate the status of eth0 via ip commands from python (and I am sure a fancy lib exists for that, too). Eg. "ip link set eth0 down", "ip address flush dev eth0" etc. (Daniil Baturin's excellent iproute2 guide)
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Return to “Advanced users”