script to easily display IP aliases

From: Alan C. Davis <davis_at_tessco.com>
Date: Fri, 28 Jul 2000 15:05:07 -0400 (EDT)

This is a script that I wrote to easily display the IP aliases defined
for an ethernet interface. I'm presenting it as an example only, since
I don't have the full range of hardware and OS release versions available
to test it on. I've tested it on tu and fta interfaces on v4.0e and v4.0f.

sample usages :

        ifalias
        ifalias -h
        ifalias fta0

It defaults to tu0, if you want a different default, change the variable
DEF_IF in line 17.

Alan Davis
davis_at_tessco.com



#!/usr/bin/ksh
# ifalias -- returns ip aliases for an interface
# default is tu0, arg1 specifies alternate if to query

trap cleanup 1 2 3 6 15 18
trap my_exit 0

my_exit () {
        /usr/bin/rm -f /tmp/*.$$
}
cleanup () {
        /bin/true
}

PROGNAME=${0##[./]*/}

DEF_IF=tu0

usage () {
        echo "Usage :
        $PROGNAME [ifname] -h

        Queries an ethernet interface and displays any IP aliases defined on that interface.
        Default interface is $DEF_IF, interface name may be specified as argument 1.
        
        -h -- display this help

        Defined interfaces may be determined from /etc/rc.config file using rcmgr :
                rcmgr get NUM_NETCONFIG -- returns number of interfaces defined
                rcmgr get NETDEV_n -- where 0 <= n <= NUM_NETCONFIG
        "
        exit
}

# site.rc contains all the node-specific config and variables
if [ -r /local/etc/site.rc ]
then
        . /local/etc/site.rc
else
        echo "$PROGNAME : Can't read /local/etc/site.rc."
        exit 1
fi

[ `/usr/bin/id -u` -eq 0 ] || {
        perror "You must be root."
        exit 1
}

# parse command line into arguments
set -- `/bin/getopt "h" $*`

# check result of parsing
if [ "$?" != 0 ]
then
       exit 1
fi
while [ "$1" != -- ]
do
        case $1 in
        -h) usage $0 # display help
                ;;
         esac
       shift # next flag
done
shift # skip double dash

#
# Local functions
#
function get_primary_addr {
#set -x -v
integer cnt i

[ "$1" != "" ] && {
        IF=$1
        cnt=`/usr/sbin/rcmgr get NUM_NETCONFIG`
        i=$cnt
        while [ $i -ge 0 ]
        do
                IF_CONFIG=`/usr/sbin/rcmgr get NETDEV_$i`
                [ -z "IF_CONFIG" ] && {
                        perror "NUM_NETCONFIG doesn't match number of NETDEV_x entries"
                        return 1
                }
                [ "$IF_CONFIG" = "$IF" ] && {
                        IP=`/usr/sbin/rcmgr get IFCONFIG_$i | cut -f1 -d' '`
                        [ -z "$IP" ] && {
                                perror "IFCONFIG misconfigured, no IFCONFIG_$i for defined NETDEV_$i"
                                return 1
                        }
                        echo "$IP"
                        return 0
                }
                i=i-1
        done
        return 1
        }
        perror "func get_primary_addr requires ifname argument"
        return 1
}

function validate_if {
#set -x -v
IF_GOOD="no"
integer cnt i

[ "$1" != "" ] && {
        IF=$1
        cnt=`/usr/sbin/rcmgr get NUM_NETCONFIG`
        i=$cnt
        while [ $i -ge 0 ]
        do
                i=i-1
                IF_CONFIG=`/usr/sbin/rcmgr get NETDEV_$i`
                [ -z "IF_CONFIG" ] && {
                        perror "NUM_NETCONFIG doesn't match number of NETDEV_x entries"
                        return 1
                }
                [ "$IF_CONFIG" = "$IF" ] && {
                        IF_GOOD="yes"
                        return 0
                }
        done
        return 1
        }
        perror "func validate_if requires ifname argument"
        return 1
}

#
# Main starts here
#

# use default if $1 is empty
IF=${1:-$DEF_IF}

# make sure interface exists
validate_if $IF
IF_OK=$?
[ "$IF_OK" -ne 0 ] && {
        perror "Interface $IF not configured."
        exit 1
}
# get primary address as defined in rc.config
PRIMARY=`get_primary_addr $IF`

# netstat shows all addresses for an interface
/usr/sbin/netstat -n -I $IF > /tmp/${PROGNAME}.$$

# get just the ethernet addresses and print all but the primary
awk -v p="$PRIMARY" -v intf="$IF" 'BEGIN { i = 0 }
$4 ~ /[0-9]*\.[0-9]*\.[0-9]*\.[0-9]/ { if_a[i++] = $4 }
END { for ( x = 0; x < i; x++)
                if ( if_a[x] != p) { print if_a[x] }
}
' /tmp/${PROGNAME}.$$
exit 0
Received on Fri Jul 28 2000 - 19:04:41 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:41 NZDT