# conditional networking

## curmudgeon

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:

```

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}

        }

    }

```

----------

## mike155

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.

----------

## curmudgeon

 *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)? :)

----------

## UberLord

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

```
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!

----------

## Syl20

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...

----------

## UberLord

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.

----------

## krinn

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

----------

