Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
conditional networking
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
curmudgeon
Veteran
Veteran


Joined: 08 Aug 2003
Posts: 1523

PostPosted: Fri Mar 22, 2019 10:53 pm    Post subject: conditional networking Reply with quote

I am setting up a laptop for someone else, and I am wondering if it is possible to build some configuration that will do the following:

Code:

if an ethernet connection is available,
    {
    don't bring up the wireless interface
    if the connection is the home network
        {
        configure the interface with a static IP
        }
    else
        {
        use DHCP
        }
    }
else (no ethernet connection available)
    {
    don't bring up the ethernet interface (or bring it down after determining the status).
    if a wireless network is available
        {
        if the connection is the home network
            {
            configure the interface with a static IP
            }
        else
            {
            use DHCP
            }
        }
    else (no networks available)
        {
        don't bring up either interface (make certain that they are off}
        }
    }
Back to top
View user's profile Send private message
mike155
Veteran
Veteran


Joined: 17 Sep 2010
Posts: 1004
Location: Frankfurt, Germany

PostPosted: Sat Mar 23, 2019 12:29 am    Post subject: Reply with quote

I have a bash script that does something like that. I start it INSTEAD of the OpenRC / Systemd / Network manager tools for network setup. The bash script uses tools like ethtool, ifconfig, ping, dhclient, iw, etc. to check link and network status and to setup network devices.
Back to top
View user's profile Send private message
curmudgeon
Veteran
Veteran


Joined: 08 Aug 2003
Posts: 1523

PostPosted: Sat Mar 23, 2019 7:34 am    Post subject: Reply with quote

mike155 wrote:
I have a bash script that does something like that. I start it INSTEAD of the OpenRC / Systemd / Network manager tools for network setup. The bash script uses tools like ethtool, ifconfig, ping, dhclient, iw, etc. to check link and network status and to setup network devices.


Can you share it (sanitized, of course)? :)
Back to top
View user's profile Send private message
UberLord
Retired Dev
Retired Dev


Joined: 18 Sep 2003
Posts: 6737
Location: Blighty

PostPosted: Sat Mar 23, 2019 10:35 pm    Post subject: Reply with quote

You can do this with dhcpcd + wpa_supplicant - both in the boot OR default runlevels.
The only caveat is that the wifi interface will be up alongside the ethernet interface, but the ethernet interace will always be preferred when that is up.
You want this anyway to switch fast between the two.

The premise is that you use dhcpcd to ARPing a node (by IP address).
If it replies, it will use that profile - either by IP address or hardware address.
If there is no reply it will move onto the next ARPing address.
Once out of addresse to ARPing it will fallback to DHCP/IPv4LL.

So this config should work - just typed up, not really tried.
See dhcpcd.conf(5) for the full details

Code:
interface eth0
   # Ping home router
   arping 192.168.0.1

# Wireless SSID name could clash with home and somewhere else, so ping our router still
profile HomeNetworkSSID
   # Ping home router
   arping 192.168.0.1

# Home router has this hardware address
profie 11:22:33:44:55:66
   static ip_address=192.168.0.88/24
   static routers=191.168.0.1
   static domain_name_servers=192.168.0.1


Now, you *could* do this with shell scripts as well - infact the netifrc stuff in Gentoo has the tooling for it.
I know, because I originally wrote it. But it's really overly complex and also quite slow at switching between network states when you want a solution like you describe.
It also works like dhcpcd, so that the wifi isn't really down as such, it's just not preferred when ethernet is up.

Good luck!
_________________
Use dhcpcd for all your automated network configuration needs
Use dhcpcd-ui (GTK+/Qt) as your System Tray Network tool


Last edited by UberLord on Thu Apr 04, 2019 3:51 pm; edited 1 time in total
Back to top
View user's profile Send private message
Syl20
Guru
Guru


Joined: 04 Aug 2005
Posts: 554
Location: France

PostPosted: Thu Apr 04, 2019 1:54 pm    Post subject: Re: conditional networking Reply with quote

On my laptop, as I need to be able to choose which network (my own wired network, my own wifi, another wired or wifi network, through my smartphone as a modem, or... nothing) I want to use all the time, and start or stop some services and tune some config files according to the network I choose, I use a home-made pair of scripts.

The first one is run by me, as a regular user, asks me what to do, via zenity menus, and launches the second one with some parameters, via sudo. To ease its use, I added a .desktop file on my desktop, and I defined an autostarting application in my desktop manager. So the first window I see when I log in is the network chooser.

The second script reads the first's parameters, unloads and loads the appropriate kernel modules (then, I'm sure there is at best one, and only one, network interface up), unloads and loads the appropriate netfilter rules, sets the network configuration or launches wicd (it's easier to configure an unknown and one-shot network), changes the appropriate parameters inside the appropriate config files (NTP servers, SMTP relay, and so on), and stops and starts the appropriate services (SSH server, monitoring agents, and so on).

At the same time, I disabled all the net.* and the network-related services in all the runlevels. One unexpected result is a really fast boot.

I don't have my laptop on hand, so I can't share these scripts now. Anyway, as they're heavily customized (and awfully nasty, I guess...), I wonder if someone else can use them as is...
Back to top
View user's profile Send private message
UberLord
Retired Dev
Retired Dev


Joined: 18 Sep 2003
Posts: 6737
Location: Blighty

PostPosted: Thu Apr 04, 2019 3:54 pm    Post subject: Reply with quote

That's a good point about SSID selection via UI.

I edited my original post to include a profile for home SSID to ping the same router in case of conflict.
You can also run dhcpcd-gtk+ or dhcpcd-qt from the dhcpcd-ui package in place of wicd (wicd does NOT support dhcpcd using IPv6 btw because wicd has a strange idea of how dhcpcd works) or network manager or other tools.
_________________
Use dhcpcd for all your automated network configuration needs
Use dhcpcd-ui (GTK+/Qt) as your System Tray Network tool
Back to top
View user's profile Send private message
krinn
Watchman
Watchman


Joined: 02 May 2003
Posts: 6970

PostPosted: Thu Apr 04, 2019 5:23 pm    Post subject: Reply with quote

i would check in preup() state if cable is sense, i didn't test, but some mii-tool eth0 | grep "link ok" should build your if an ethernet connection is available,
as i assume if a cable is plug, i would be able to configure and bring ethernet up, and without cable, no need to check anymore and better switch to wireless
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum