Debian – Wait for a network device to come up before bringing another network device up on boot

debiannetworkingraspberry pi

I'm in the middle of setting up my Raspberry Pi running Raspbian as a bridge between my university's wireless network (which requires WPA2-Enterprise authentication) and my private wireless router (via its WAN port).

I've received some amazing help here:
How can I most simply transparently bridge traffic between wlan0 and eth0?

For the most part my setup is now working as intended. The only problem is that, if I set eth0 to auto or allow-hotplug on my Raspberry Pi (as I'd like to do, so that everything comes up headlessly on boot), it seems as though all internet traffic is routed through eth0. This is incorrect; my internet connection is on wlan0, and eth0 shares that connection out to my private router.

I can overcome this issue by not having eth0 come up automatically. If I leave auto or allow-hotplug for eth0 out of /etc/network/interfaces, allow the Raspberry Pi to boot, and then manually run "ifup eth0", I get behaviour as expected.

Is there a way to either ensure that wlan0 has been fully brought up before bringing up eth0 on boot (even though eth0 is the faster interface to bring up)? Or, failing that, how can I force internet traffic (noting that this comes from eth0) through wlan0 only?

Edit: For now, as a work-around, I'm running a script to bring up eth0 on start-up with a thirty second delay to allow wlan0 to come up first. This solution is neither elegant nor reliable (what if wlan0 authentication takes longer than thirty seconds?), but it does seem to be doing the trick for now. I'm still looking for a better solution, though!

Best Answer

Hmm.. Just a thought. But, I do assume your using DHCP on your Wi-Fi network. Therefore, you wouldn't have an IP address until your network is fully up.

So, let's just have a little while loop.

while [ "$(ifconfig wlan0 | grep inet | grep 192.168.)" = "" ];  do sleep 1; done

The 192.168. should be replaced with your standard range. Now it would check every second once if you're already having a connection and as soon as you're having a connection via wifi you can follow up directly with ifup eth0

Related Question