4

I am trying to configure my network interfaces. I would like to make my wireless interface to be disabled when I plug in an Ethernet cable. When I take the Ethernet cable out, my wireless interface should be enabled again.

I have created /etc/network/if-up.d/wlan-down:

#!/bin/sh

[ "$IFACE" != "eth1" ] || exit 0
ifdown wlan0

exit 0

and /etc/network/if-down.d/wlan-up:

#!/bin/sh

[ "$IFACE" != "eth1" ] || exit 0
ifup wlan0
wpa_cli -i wlan0 reassociate

exit 0

but it just doesn't seem to work at all. WPA still tries to connect all the time. I can see wlan0 appears and disappears all the time. My questions are:

  1. How can I debug these scrips, e.g. what happened and what was executed?
  2. What is the correct way to implement what I am after?
  3. Also, it seems that nothing happens when I disconnect the Ethernet cable. The interface is up and the IP address is assigned. I would like to get it down in such a case, so that I could use wireless instead.

I am using Debian unstable

8 Answers 8

2

Is this for a desktop system? Have you considered using network manager or wicd. These tools take care a lot of this for you automatically.

How can I debug these scrips, e.g. what happened and what was executed?

You might want to use something like logger to submit debug information to syslog. Or you could simply put in a few echo commands here and there to write things to a file somewhere.

1
  • Thanks for the tip with logger. I tried network manager, but I would like something that is not GUI dependent. The KDE network manager frontend is a pile of sh... btw.
    – Grzenio
    May 27, 2009 at 12:49
3

Looks like ifplugd package has all the scripts I need! After installing:

apt-get install ifplugd

and adding eth1 to the config file /etc/default/ifplugd everything started to work as I wanted, without adding any extra scripts (I cleaned everything to the initial state).

Thanks everyone for help

1

Heres a possible "untested" solution,

in your /etc/network/interfaces

allow-hotplug eth0
iface eth1 inet dhcp

down ifconfig wlan0 up
down wpa_cli -i wlan0 reassociate

up ifconfig wlan0 down

allow-hotplug wlan0
iface wlan0 inet dhcp

up ifconfig eth0 down
down ifconfig eth0 up

well theres my five cents, I use the above setup to modify routes based on interface up/down on a server of mine, it might work for you too, reason I did it using the interface file was that my scripts didnt seem to execute from the /ifup.d dir even with 755.

Let me know if it works unfortunately I dont have a debian machine with a wifi card to test with :(

2
  • Hi, this seems reasonable, but the problem I discovered is that my eth1 doesn't seem to be hotpluggable - it is up at the system start, and then dhcp tries to obtain an address every 5 mins or so (unsuccessful because the cable is uplugged), and unfortunately the up and down scripts don't get executed. Any idea?
    – Grzenio
    May 31, 2009 at 11:10
  • You could try a messy little script, #!/bin/bash interval=5 while [ 0 -lt 1 ]; do ifconfig eth0 up ifconfig eth0 | grep 'inet addr' | awk '{ print $2 }' | cut -c 6- > /tmp/eth0.stat if [ -z /tmp/eth0.stat ]; then ifconfig eth0 down sleep $interval else echo "We have an address (cat /tmp/eth0.stat), looking good." > /var/log/eth0.stat.log sleep 10 fi done let me know.. Jun 2, 2009 at 12:22
0

I still think that adjusting the metric on the wireless interface is the way to go, as I described in How can I configure wpa_supplicant so that my wlan interface is disabled when I plug in ethernet cable?

0

You want to set something up in the /etc/network/ subtree, perhaps as womble suggests by increasing the metric of the wifi route, or perhaps by some other means involving a custom script in /etc/network/if-up.d/ that would take the wifi interface down if the ethernet interface came up.

Info on making scripts for /etc/network/if-up.d/ is on the interfaces man page, under the 'IFACE OPTIONS' section.

0

The problem is that you cannot run ifup/down recursively. I have tried to do something similar where I wanted to bring up a VPN interface when a WAN interface was brought up.

My solution is:

echo ifup vpn0 | at now >/dev/null 2>&1

To automatically bring an interface up and down when the cable is plugged or unplugged, use ifplugd from the package of the same name.

0

hmm comments don't like code.

#!/bin/bash

interval=5

while [ 0 -lt 1 ]; do 
         ifconfig eth0 up 
         ifconfig eth0 | grep 'inet addr' | awk '{ print $2 }' | cut -c 6- > /tmp/eth0.stat
         if [ -z /tmp/eth0.stat ]; then
         ifconfig eth0 down
         sleep $interval
         else
         echo "We have an address (`cat /tmp/eth0.stat`), looking good." > /var/log/eth0.stat.log
         sleep 10
         fi
done

create a daemon to run this on startup.

let me know how it works out

-1

At least in Fedora-related distributions, if you look at the ifup script and its relatives, you'll see that if /sbin/ifup-local and /sbin/ifdown-local exist, they'll be executed. This gives you a safe place to plug in machine-local scripts. The one argument passed to these scripts will be the device going up or down.

You could create these scripts and have them start and stop eth0 if the device being started or stopped is your wireless device.

I don't know if the networking scripts are configured to work in the same way under Debian and related distributions, but it's worth a look.

Not the answer you're looking for? Browse other questions tagged .