Set up Debian wifi so it's only enabled/used when ethernet is not connected?

I prefer to use wired ethernet whenever possible for nodes and PCs, etc. but wifi does have its place for portable nodes and for those who don’t like extra wires. If someone wanted to be able to use both however, eg. ethernet at home (for best performance and less RF floating around), and wifi on the road, I wonder if anyone knows a way to set up Debian so it will automatically turn on/enable wifi only if ethernet is not connected?

There seem to be a variety of ways to manage networking on Debian, wpasupplicant seems pretty common and is what I’m using on wifi-enabled nodes I build, whereas iwd seems to be a newer option that may have some advantages but which I have not tried yet. Is there an option with any of these to try ethernet first and only go to wifi if an IP address could not be obtained on ethernet or if no ethernet cable is plugged in?

If not I can probably set up some DMTF commands to run an AGI script to turn wifi on/off so as to have relatively easy manual control, but it would be nice if it was automatic. I have noticed that if ethernet and wifi are both connected and the node then gets 2 IP addresses assigned that this seems to really confuse the OS/networking stack and internet responses are then very slow.

This question does not really relate to ASL and might be better for a Debian forum, but hopefully someone here has already done this and the answer could be useful for others trying to do the same thing on their nodes.

As I remember it, It goes through the list in order placed with connection attempts.
However, I think if you look, there may be a way to change timeout options for all or each.

I placed my wired connection first on the list.
WIFI seems to take longer to time-out before rotating in the list.(by nature of the beast)

A bash script works pretty good as well. If you wanted to rename a pair or files resetting your priorities on a scripted shutdown for the next boot.

I believe you could do it live as well if you restart the network afterwards in the script.

Likely a lot of tricks found in a google search.

edit:
Also, not remembering, I think it also starts with the last good connect in the list on fresh boot.

Hi Mike, What package are you using to manage wifi / networking? I believe that in the default ASL image there is no wifi management app/package by default and you have to install one. And for wpasupplicant or iwd I think they only manage wifi but not ethernet connections, so I don’t know how you could put both in a list and have it try one first. Any details you could provide there would be much appreciated. This would probably be easier if I installed a desktop GUI package such as Mate on the node, however that takes up GB of space and may not all fit on the miniPCs I use (Dell Wyse 3040’s which usually have only 8GB eMMMC). And command line is usually better for keeping things simple and allowing things to be controlled by DTMF commands. Thanks, David. Also BTW Thank you for always being one of the first commenters on the ASL forums and helping so many people.

you can disable the wifi by going into /etc/wpa_supplicant directory and rename the file to BKUP as the extension or you can try 1 of 7 ways to disable wifi on the raspberri pi.

For all.
While I can not publish my full bash shell scripts from nearly a decade ago, they are a bit more complex for most to understand since I was doing a lot of different things most do not…

These are the basic functions from them. Adapt as needed.

You could make each a =CMD, /path/xxx.sh $pharm$ and fill in the $blank$ for/when calling them or specify them in place of the var in the script.

I have not tested these as of late and it’s possible the syntax could have changed as libraries updated.
But a quick lookup on the web would show you the latest.

#Disconnect Eithernet
ifconfig $(ifconfig | grep eth | awk ‘{print $1}’ | head -1 ) down

#Disconnect wifi
killall wpa_supplicant
wlessname=ls /sys/class/net | grep wlan* | head -1
ifconfig $wlessname down

#Connect Ethernet
ifconfig $(ip link show | grep eth | grep state | awk ‘{printf $2}’| sed -e ‘s/:$//’) up
ifconfig $(ifconfig | grep eth | awk ‘{print $1}’| head -1 ) $(ifconfig | grep Bcast | awk

#Connect wifi
wlessname=ls /sys/class/net | grep wlan* | head -1
ifconfig $wlessnameup > /dev/null 2>&1 &
killall wpa_supplicant > /dev/null 2>&1 &
iwconfig $wlessname power off > /dev/null 2>&1 &
wpa_passphrase $1 $2 > /root/wps.conf # $1 & $2 represent passphrase
wpa_supplicant -Dwext -i$wlessname -c/root/wps.conf > /dev/null 2>&1 &
sleep 5 #adjust time as needed
dhclient -r > /dev/null 2>&1 &
dhclient $wlessname > /dev/null 2>&1 & # $wlessname = wireless name to connect

That is enough to get anyone started. But it’s where my time stops in producing scripts that likely end up in for profit systems. These are the basic command tools. Make them work for your need

A BASH script is the logical choice for custom work within our system. Since it can do anything you can do from a command line and will expand your capability in knowing your system without learning many different languages to get the job done. And you might be surprised just what you can do from a command line.
And these can be called with or without parameters from both app_rpt and asterisk. And I might add, nothing is better documented on the web. I can’t remember the syntax on it all and have to look it up very often.

Bash is your friend.

1 Like

Some additional that may help some. Or not.

I quit using the methods posted above in place of something else I will describe briefly here.
It may not fit many folks actual use. Others it may be perfect for.

I took a Linksys wrt54g router and loaded dd-wrt firmware in it. (still a cheap router on ebay)
https://dd-wrt.com/

Inside of that firmware, the ability to make your lan all input and your wan on the wifi.
So, I set the WAN wifi to the tether connection on my cell phone.

The linux box was always LAN/eithernet and got it’s WAN internet from the phone.

This is a great method for operating a Pi on ASL portable.
However, if you want inbound connections, you will need a dynamicdns service as your IP will change as you float within cell towers and networks. And the router & firmware will help with that.

So, I didn’t need to use the above scripts anymore for my laptop while mobile. But you can just tether wifi directly to a cell phone, but now you are going to script dyndns if you need to keep inbound connections.

Know that this method also means you are going to lug around a router with you.
(not a issue if you take it all camping LOL)
I used a big LiPO battery on it and a briefcase to carry it and a laptop, mouse with me.
This admitably is not for everyone.

I did use it a couple of times on a ASL Pi portable, so I know it works.
Since then the Pi portable has been collecting dust (it had a led display and wireless keyboard).

Since then, I use a soft iax phone and a phone app ‘Terminus’ for SSH and can leave the equipment at home or in the cloud and do the same. Not really Ham Radio, Just easy access to it. Which is about all you had the other ways.

Lots of good info above. I think what I’ll do is set wifi off by default and create a script that runs at boot that checks the ethernet status and if no IP address is received on ethernet within X seconds then power up the wifi, and then just in case recheck every once in awhile and if a user happens to have plugged in ethernet and an IP address is assigned there then turn off wifi. This should be simple to do. And yes bash does a lot but the syntax is definitely unintuitive. Fortunately PHP does all the same things but with user-friendly syntax, more simplicity and more flexibility. Will post what I come up with soon.

It would still be nice if there was a network package that could do this switching automatically, ie. turn on wifi only when ethernet is not connected, but it should only take me 1/2 hour to set up the same thing in PHP and I’ll then have a more flexible solution and I can also call the script from DTMF commands.

Just a correction to an obvious typo; the Android SSH app is called “Termius” (no ‘n’).
Amazingly useful app; been using it for years.

Replying to this old thread:

After a discussion last night on the All Nodes Net, it occurred to me that there should be a way to monitor the state of the Ethernet interface with a systemd unit or similar, then disable the wpa_supplicant service if it’s up, turning it back on when it’s down again.
Has anyone here already done this before I try stuff?

If you’re using NetworkManager, it’s pretty straightforward: