# networking scenarios

## curmudgeon

One of the things I have never been able to figure out with gentoo is handling various network contingencies.

I would like to set up the following.

1. If there is a ethernet cable plugged in (and a connection) at boot, set up a specific set of parameters (static ip, dns servers, routes), and make sure that wireless networking is disabled.

2. If there is no ethernet cable, but a connection to a specific wireless access point on boot, set up a specific set of parameters (static ip, dns servers, routes), and make sure that wired networking is disabled.

3. If neither of the above conditions are met on boot, make sure that all networking is DISABLED until the user specifically enables and configures it.

Is there some way of doing that, and if so, can someone explain how? Thank you in advance.

----------

## eccerr0r

This is the problem that a static, init-based configuration has issues with.

What about the case where you've been connected to Ethernet, and then later disconnect it?  Should it automatically switch over?  And if you connect Ethernet while you've been on wireless?

I don't know if a script that does this, but in any case you will need a program to monitor status of all the network ports and switch accordingly.  A full program does exist to do this: this is what NetworkManager does.

Networkmanager will unfortunately connect both Ethernet and wireless at the same time if both are available.  However it gives Ethernet a lower metric and thus will favor Ethernet when opening new connections, so it's close to what you want...  Both network addresses will respond as expected, which is pretty cool IMHO.

----------

## The Doctor

Assuming you only care about boot up configuration I'd think a simple script would do.

This script is very incomplete and probably more than a few errors

```

#!/bin/bash

# ethernet check

if [ $(grep ""  /sys/class/net/eth0/carrier) -eq 1]

  exec ethernet_script

# wireless check

else if [$(some_command_I'm_too_lazy_to_look_up | grep network_name)]

   exec wireless_script

# default case

else

  ifconfig eth0 down

  ifconfig wlan0 down

fi
```

Is that what you are looking for?

----------

## charles17

 *curmudgeon wrote:*   

> 1. If there is a ethernet cable plugged in ...

 On some laptops ethernet is combined with a physical rfkill switch.  So if ethernet cable is plugged in, wireless is automatically switched of and dhcpcd then selects ethernet for connection.

 *curmudgeon wrote:*   

> 2. If there is no ethernet cable ....

 Then rfkill is off and dhcpcd automatically selects wireless. wpa_supplicant then connects according to its settings.

----------

## khayyam

The Doctor ...

the problem with doing it outside of rc is that should you then need openntp, openresolv, or some other service, this will then want something to 'provide net' (and so will start a service that will do that causing your settings to go bye-bye). Also, there are other tie ins, like wpa_cli, which start/stop /etc/init.d/net.$IFACE based on CONNECTED/DISCONNECTED on the cntl_interface. Both of these will not be covered by an external script, so either you intergrate something more robust with /etc/conf.d/net (using 'preup()', etc) or use some other method to 'provide net' (ie, dhcpcd, newnet, ifplugd, netplug, etc).

Asuming this is a laptop (which by the sounds of it, it is) then the OP should be using the rfkill button as this should then trigger hotplug and which could be tied to ifplugd/netplug (via modules_$IFACE="plug"), ifplugd/netplug can then be configured to bring up eth0 on cable, and switch to wireless if/when it becomes available (again, via rfkill). As for the interfaces being 'down', I don't see how this matters, they are not 'active', and you will only save power by disabling them via hardware.

best ... khay

----------

