# Automatic Network Switching?

## paleck

At work I use my laptop's built in ethernet port to connect to the network and at home I use a Lucent Orinoco Card to connect to the network/internet.  Is there any way to automate switching between the two networks.  For instance if cardmgr is unable to load any cards it automatically switches the network to the eth0(built-in ethernet) card.  Otherwise other programs/daemons will start up even when I am not connected  to my default interface.

----------

## ElCondor

Typically internal network is eth0 and wavelan eth1, so you can make your scripts checkout which interface is up and running and set special actions by the results.

concerning the other daemons/programs like apache, postgres, vmware, samba, ... that only come up when they find a network, you simply have to remove the depencies from the init-scripts, most look like

```
depend() {

        need net

}
```

* ElCondor pasa *

----------

## swingarm

http://edgesolutions.ca/article.php?sid=21

----------

## dc

A really cool solution for automatic network configuration switching is  Felix von Leitner's "divine" ( http://www.fefe.de/divine/ ).

----------

## jessler

Try quickswitch

emerge quickswitch

----------

## bangslash

Anyone actually get divine working on their Gentoo setup?  Also, I set up my machine according to the Gentoo Security FAQ.  Is there anything in there that would prevent divine from working properly?  Thanks!

!/

----------

## xterminus

When I first setup gentoo, I copied and modified the columbus boot script for gentoo.  It's been adapted to gentoo's runlevel scheme, and kicks off required services depending upon the network detected by arping.  Works similarly to how divine works, but gentoo native (yay!)

You'll need to emerge arping btw.

Here's the script and the various files you'll need to create.  Feel free to clean it up - it's still sort of messy; but it works.

/etc/columbus/networks

Each field is tab seperated, first two fields indicate what your looking for (a mac address to arp, and the resulting IP), and then if true; the network "name" your going to use to assign to the network for further processing.

```

00:a0:4b:07:4d:06       10.0.0.1        mauchnet

00:e0:a3:13:a0:1c       168.xxx.xxx.1   ibi

```

/etc/columbus/run.mauchnet

Just kicks off the services needed when I'm at home.

```

#!/bin/sh

/etc/init.d/samba start

/etc/init.d/netmount start

```

Kicks off the specific services I need when I'm at school, especially important are the firewalling features and bringing up my ipsec tunnel.  I also have to drop the extra route provided by my college's network or else ipsec blows up.

/etc/columbus/run.ibi

```

#!/bin/sh

/sbin/route del default gw 168.xxx.xxx.1

/etc/init.d/firewall start

/etc/init.d/ipsec start

```

/etc/columbus/mauchnet/etc/issue

This is what I *love* about columbus.  Create a directory structure in the columbus directory, and any files found within that symbolicly linked to the "real" name when this is run.  So when When I'm home, my /etc/issue is linked to /etc/columbus/mauchnet/issue.  You can get really imaginative here.  I also link my samba configuration files differently, so I participate on my college's network completely differently from how I participate on my lan at home for example.  Go nuts.

```

Welcome to MauchNet

```

/etc/columbus/ibi/etc/issue

```

Welcome to the xxxx xxxxx xxxx College Network

```

/etc/init.d/columbus

Here's the columbus script.  It's kind of messy, but it works well and does all the pretty [ok] boxes on boot.  After you've put this into your /etc/init.d/ directory, you'll want to do an rc-update add columbus (whatever runlevel you want).   You'll also want to check the perms on the shell scripts in /etc/columbus so that they're executable.

Enjoy.  If you get stuck fire me an email; I don't check the forums much anymore.

```

#!/sbin/runscript

#

# columbus 0.0.2

# (C) thomas@apestaart.org

# GPL

# gentoo version created and tweaked by Charles Mauch <cmauch@myrealbox.com>

# automatically guess what network we're on

# and perform network-specific stuff

# using an arp broadcast ping

# use /etc/columbus/networks as the file to use in pinging stuff

# format of that files is

# MAC\tIP\tnetwork

# where MAC is a six-byte address separated by :'s

# IP is the ip address on that network of that host's MAC address

# network is the user-friendly name of the network

# log to syslog

# in /etc/columbus, scripts named pre.[net] are executed before symlinks

# then, each file in the tree /etc/columbus/[net]/ is ln -sf'd to /

# then, the script post.[net] is executed

depend() {

        need net

}

VERSION=0.0.2

IFCONFIG=/sbin/ifconfig

COLUMBUS=/etc/columbus

NETWORKS=$COLUMBUS/networks

ARPING="/usr/sbin/arping -c 1"

RESTART_NET="/etc/rc.d/init.d/network restart"

debug()

{

  if test ! -z $DEBUG; then

    echo "$@"

  fi

}

arp_scan()

# scans the network based on information in $NETWORKS

# puts the probed net name in $NET_FOUND

{

  for a in `cat $NETWORKS | tr "\t" ","`; do

    MAC=`echo $a | cut -d, -f1`

    IP=`echo $a | cut -d, -f2`

    NET=`echo $a | cut -d, -f3`

    # Get the second line of the arping result

    debug "arpinging $IP for MAC address $MAC ..."

    RESULT=`$ARPING $IP | head -n 2 | tail -n 1`

    MAC_FOUND=`echo $RESULT | cut -d " " -f4`

    if test "$MAC" = "$MAC_FOUND"; then

      debug "Network $NET found."

      NET_FOUND=$NET

      return 1

    fi

  done

  # return failure

  eend $? "Could not determine net !"

  return 0

}

check_sync()

{

  debug "Checking if network is in sync ..."

  arp_scan

  if test ! $?; then

    # could not get what net we're in

    echo "could not get net"

    return 0

  fi

  NET_CURRENT=`cat $COLUMBUS/current`

  if test "$NET_CURRENT" != "$NET_FOUND"; then

    debug "Network not in sync."

    return 0

  else

    debug "Network in sync."

    return 1

  fi

}

set_net()

{

  NET=$@

  # apply configuration

  cd $COLUMBUS

  # now apply system configuration

  if test -d "$NET"; then

   ebegin " Creating symlinks from /etc/columbus/$NET to root"

    cd $NET

    find * -type f -exec ln -sf $COLUMBUS/$NET/{} /{} \;

   eend $?

  fi

  cd ..

  # save new last set network

  echo $NET > $COLUMBUS/current

  # Execute a gentoo virtual runlevel

  if test -e "/etc/columbus/run.$NET"; then

    ebegin " Firing up scripts needed for the \"$NET\" network"

    exec /etc/columbus/run.$NET

    eend $?

  fi

}

parse_options()

{

HELP="\

columbus $VERSION, (c) thomas@apestaart.org\n\n\

-a      always sets new network\n\

-c      clear your IP address before starting scan\n\

-d dev  use this device\n\

-h      help\n\

-l      log to syslog\n\

-n net  forcefully set this net\n\

-s      check if the current settings match the current network\n\

        and forcefully sync if they don't\n\

-v      verbose"

  # Note that we use `"$@"' to let each command-line parameter expand to a \

  # separate word. The quotes around `$@' are essential!

  # We need TEMP as the `eval set --' would nuke the return value of getopt.

  TEMP=`getopt -o acd:hln:sv -- "$@"`

  if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

  # defaults

  IFDEV=eth0

  LOGGER="echo"

  eval set -- "$TEMP"

  while true ; do

    case "$1" in

      -a) APPLY="yes" ; shift ;;

      -c) CLEAR="yes" ; shift ;;

      -d) IFDEV="$2" ; shift 2 ;;

      -h) echo -e "$HELP" ; exit ;;

      -l) LOGGER="logger -t columbus -s" ; shift ;;

      -n) NET="$2" ; shift 2;;

      -s) SYNC="yes"; shift ;;

      -v) DEBUG="yes"; shift ;;

      --) shift ; break ;;

       *) echo "List options here!" ; exit 1 ;;

    esac

  done

}

# start of main

start() {

ebegin "Checking Network Connections"

parse_options $@

# if we've been asked to forcefully set a network, then do that

if test ! -z "$NET"; then

  ewend 1 " Forcing network $NET"

  set_net $NET

  exit

fi

# if we're asked to clear the ip address then do that

if test ! -z "$CLEAR"; then

  $IFCONFIG $IFDEV 0.0.0.0

fi

# if we're asked to bring it in sync, then do that

if test ! -z "$SYNC"; then

  check_sync

  if test $? -eq 0; then

    # set the net to what has been found

    ewend 1 " Resyncing net to $NET_FOUND"

    set_net $NET_FOUND

    $RESTART_NET

  fi

  exit

fi

# scan the network

arp_scan

# did we find it ?

if test -z "$NET_FOUND"; then

  ewend 1 "  No network autodetected!"

  exit

fi

eend $? " Devices on the \"$NET_FOUND\" network found!"

# change it if allowed

if test ! -z "$APPLY"; then

  set_net $NET_FOUND

fi

NET_CURRENT=`cat $COLUMBUS/current`

if test "$NET_CURRENT" != "$NET_FOUND"; then

  ewend 2 " Last set network $NET_CURRENT not in sync with current $NET_FOUND !"

fi

}

```

Take it easy

----------

## pilla

I didn't like it very much the two times I tried it.

It makes some changes in some /etc files that I didn't like that much...

 *jessler wrote:*   

> Try quickswitch
> 
> emerge quickswitch

 

----------

## int1

I have a very similar situation.  You could try this script I wrote:

https://forums.gentoo.org/viewtopic.php?t=22717&highlight=

Enjoy!

int1

----------

