[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to switch between network interfaces?



On Fri, Apr 30, 2004 at 06:25:35PM +0100, Piers Kittel wrote:

| 2) Is there a way for the laptop to detect if the wired cable is 
| unplugged then it'd bypass or "disable" the eth0 interface at bootup so 
| preventing the dhcp client trying to find an IP via eth0?

I don't know the ideal solution to your other question, but I do know
the answer to this one!

--- /etc/network/interfaces
auto eth0
iface eth0 inet dhcp
    # only try DHCP if there is a physical link
    pre-up /usr/local/sbin/check-link.sh eth0


--- /usr/local/sbin/check-link.sh
#!/bin/sh
# check-link.sh
# tests interface for link
#
# Usage: check-link.sh INTERFACE
#
# Greg Wiley
# greg@orthogony.com
#
# Script is meant for use with the ifupdown
# program as a pre-up command.  It returns
# success if the given interface has a link.
# Created to avoid the laptop boot delay
# caused by trying to DHCP an unconnected
# interface.
#
# Currently uses MII to check link status.
# This might not be universal since not all
# NICs use MII.  It should work for a large
# number of ethernet NICs, though
#
# History:
#  11/15/2001  gjwiley      create
#  11/16/2001  gjwiley      add syntax checking, support
#                             prog checking, comments, etc.
#                             for publish
# TODO:
#  - if useful, add support for non-ether,
#    non-mii xfcs


iface="$1"

# what to grep to determine no link
MIINACK='no link'

# where is ifconfig?
IFCTOOL='/sbin/ifconfig'
IFCCMD="eval ( ${IFCTOOL} ${iface} > /dev/null 2>&1 )"

# the mii link checking program
MIITOOL='/sbin/mii-tool'
MIICMD="${MIITOOL} ${iface}"

# some systems don't use egrep
GREPTOOL='/bin/egrep'
GREPMIICMD="eval ${GREPTOOL} -q '${MIINACK}'" 


#default to internal error return
rval='3';
if [ -f ${MIITOOL} -a -f ${GREPTOOL} -a -f ${IFCTOOL} ]; then
  if [ -x ${MIITOOL} -a -x ${GREPTOOL} -a -x ${IFCTOOL} ]; then
    if [ "x${iface}" != "x" ]; then
      if  ${IFCCMD}; then
        if ( ${MIICMD} | ${GREPMIICMD} ); then
          rval='1'
        else
          rval='0'
        fi
      else
        echo "${0}: invalid interface" >&2
        rval='2'
      fi
    else
      echo "${0}: interface not specified" >&2
      rval='2'
    fi
  else
   echo "${0}: required program not executable." >&2
  fi
else
  echo "${0}: required program not found." >&2
fi

exit ${rval}



As the comments indicate, I didn't come up with this.  I found it on
the 'net when someone on d-u pointed it out.

The only downside is ifupdown puts an entry in the state file even
when it doesn't bring up the interface.  Therefore after you plug a
cable in, you either need to run ifup with the '--force' parameter or
run 'ifdown' before running ifup.

-D

-- 
Yes, Java is so bulletproofed that to a C programmer it feels like being in a
straightjacket, but it's a really comfy and warm straightjacket, and the world
would be a safer place if everyone was straightjacketed most of the time.
                                                     -- Mark 'Kamikaze' Hughes
 
www: http://dman13.dyndns.org/~dman/            jabber: dman@dman13.dyndns.org

Attachment: signature.asc
Description: Digital signature


Reply to: