#!/bin/bash ############################################################################### # # This script provides a version of the `ifconfig` command that expects to # see the ethernet address associated with a particular port, and performs # the stated actions on whichever ethernet interface has the specified # ethernet address. All actions associated with the `ifconfig` command are # supported, and the only change in syntax is that the port name of "eth?" # is replaced by the associated ethernet address. # # If no ethernet interface is found with the specified ethernet address, # this script uses a return value of 1, otherwise it uses a return value # of 0. # ############################################################################### function assign() { local HW="$1" OK=Y declare -i N=0 shift 1 while [ $OK = Y ]; do if ! port eth$N ; then if ifconfig eth$N up 2> /dev/null ; then if [ "`hwaddr eth$N`" != "${HW}" ]; then ifconfig eth$N down else ifconfig eth$N "$@" OK=$? fi else OK=0 fi fi N=$N+1 done return $OK } function hwaddr() { ifconfig "$1" | grep "^$1" | tr ' ' '\n' | grep '[0-9A-F]:[0-9A-F]' } function port() { ifconfig | grep ^eth | fgrep "$1" > /dev/null } if [ $# -gt 0 ]; then if assign "$@" ; then echo No ethernet interface with address "$1" found. >&2 exit 1 else exit 0 fi else ifconfig exit 0 fi