rajivmishra13
Posts: 6
Joined: Wed Jul 22, 2015 3:14 pm

Solving Eth0 and Wlan0 active/passive the right way

Wed Jul 22, 2015 3:35 pm

Hello All,

I know there are numerous articles about this but I am posting the solution that worked for me and is indeed latest :)

My requirement:

As I use my pi primarily as a dns server for my home lan, I have it always connected via eth0 and would like to be able to use it even when I place it somewhere else within my network using wireless. With this in mind and also the need that I may want to someday carry the pi to someone elses network without a monitor or pre-supplied wifi config, I left the eth0 to dhcp and configured a static ip in my home router for it say 192.168.1.10 using its mac address for same ip via dhcp.

What I also wanted was that the wlan and eth0 should keep same ip of 192.168.1.10 so my dns is reachable weather its plugged in or not.

I configured wlan0 as static for two reasons, since I do not want to guess the wlan0 ip when its unplugged from eth0 and I can not assign it the same ip since I have already made the static reservation in home router with eth0 mac address.


I hope you are with me on this.

So here is how my interface file looks like:

pi@MyPi / $ sudo cat /etc/network/interfaces
auto lo
iface lo inet loopback

#
allow-hotplug eth0
iface eth0 inet dhcp
metric 0

auto wlan0
iface wlan0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
metric 1

Explanation:

We use the "ifplugd" to do what we need.
1) "ifplugd" does not monitor the interfaces which are set for "auto" startup. So we do not assign "auto eth0" for the eth0 interface
2) So above "ifplugd" is monitoring the eth0 interface, which means, ifplud will act everytime it sees a link down or link up on eth0. We use this monitoring to get out result. As per part of this monitoring ifplugd executes a script ( ifupdown ) . We will modify that script to bring the WLAN0 Down when Eth0 is available and UP when Eth0 link is lost.

Go to "/etc/ifplugd/action.d ".
Make copy of original "ifupdown" i.e sudo cp ifupdown ifupdown.original
We edit this file i.e sudo nano ifupdown
Paste the following :

#!/bin/sh
set -e

case "$2" in
up)
/sbin/ifup "$1"
if [ "$1" = eth0 ]; then /sbin/ifdown wlan0 ; fi # To bring the WLAN down
;;
down)
/sbin/ifdown "$1"
if [ "$1" = eth0 ]; then /sbin/ifup wlan0 ; fi # To bring the WLAN up
;;
esac

Ctr + x , Y, save it exit

sudo shutdown -r now

You should see that eth0 is working now , while wlan0 is not. Unplug the eth0 and that should kick start the wlan0. Plug the eth0 again and that should kill the wlan0.

If you want, you can assign wlan0 another ip address as per your liking. I hope it helps !!

Resources:
http://www.aoakley.com/articles/2013-07 ... orking.php
http://www.debian.org/doc/manuals/debia ... zas-in-eni

syscharger
Posts: 19
Joined: Wed Aug 26, 2015 8:46 am

Re: Solving Eth0 and Wlan0 active/passive the right way

Mon Aug 31, 2015 9:54 am

Hi. i am very new to this bash script.
May i know what is the significant of the "$2" and "$1" below?
What does it represent?
Thanks

Code: Select all

#!/bin/sh
set -e

case "$2" in
up)
/sbin/ifup "$1"
if [ "$1" = eth0 ]; then /sbin/ifdown wlan0 ; fi # To bring the WLAN down
;;
down)
/sbin/ifdown "$1"
if [ "$1" = eth0 ]; then /sbin/ifup wlan0 ; fi # To bring the WLAN up
;;
esac

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Solving Eth0 and Wlan0 active/passive the right way

Mon Aug 31, 2015 9:59 am

syscharger wrote:May i know what is the significant of the "$2" and "$1" below?
They are the command line positional parameters.

The script will usually be called with two parameters. The first is the interface name and the second is the desired state ("up" or "down"). If the script is called like this:

Code: Select all

ifupdown eth0 up
then $1 will refer to the first parameter "eth0", and $2 will refer to the second parameter "up".

syscharger
Posts: 19
Joined: Wed Aug 26, 2015 8:46 am

Re: Solving Eth0 and Wlan0 active/passive the right way

Mon Aug 31, 2015 12:36 pm

Thanks..
How do u interpret the following base on the code above?

Code: Select all

case "$2" in
up)

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Solving Eth0 and Wlan0 active/passive the right way

Mon Aug 31, 2015 2:34 pm

That's a basic "case...esac" statement block in Bash. It's a bit like multiple "if" statement. It works something like this:

Code: Select all

case "$variable" in
  "fred")
    echo "This code is executed if variable contains fred."
    echo "The end of the code is marked by two semi-colons" ;;
  "jim")
    echo "This code is executed if variable contains jim."
    echo "Again, the end of the code is marked by two semi-colons" ;;
esac #This is the end of the "case ... esac" block

syscharger
Posts: 19
Joined: Wed Aug 26, 2015 8:46 am

Re: Solving Eth0 and Wlan0 active/passive the right way

Mon Aug 31, 2015 11:46 pm

Great. Thanks

btw.. in order for me to execute the code, i do the following?
$sudo bash -x ifupdown eth0 up

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Solving Eth0 and Wlan0 active/passive the right way

Tue Sep 01, 2015 4:50 am

syscharger wrote:in order for me to execute the code, i do the following?

Code: Select all

sudo bash -x ifupdown eth0 up
You could (I'd leave out the "-x" which will show the commands being executed), but normally you wouldn't have to run it - it will get executed automatically when the interface state changes.

syscharger
Posts: 19
Joined: Wed Aug 26, 2015 8:46 am

Re: Solving Eth0 and Wlan0 active/passive the right way

Tue Sep 01, 2015 5:20 am

Can i do this, if i want to have wlan0 as my primary/priority interface whenever it is available, and eth1 and my secondary interface?

Code: Select all

case "$2" in
up)
        /sbin/ifup $1
        if [ "$1" = wlan0 ]; then /sbin/ifdown eth1 ; fi # To bring the ETH1 down
        ;;
down)
        /sbin/ifdown $1
        if [ "$1" = wlan0 ]; then /sbin/ifup eth1 ; fi # To bring the eth1 up
        ;;
esac

rajivmishra13
Posts: 6
Joined: Wed Jul 22, 2015 3:14 pm

Re: Solving Eth0 and Wlan0 active/passive the right way

Mon Sep 21, 2015 8:43 am

Probably, I am no expert, but you got to give it a shot and find out :)

Return to “General discussion”