#!/bin/sh
#
# Echoes the list of IPv4/IPv6 addresses corresponding to configured
# network interfaces. If the script is renamed to get_iflist4, it will
# output only IPv4 interfaces. Likewise, get_iflist6 will deal with
# IPv6 only.

IFLIST4=""
IFLIST6=""
IFLIST=""
IFCONFIG=""
NETSTAT=""
DO_IPV4=1
DO_IPV6=1

# See if we have to display only IPv4 or IPv6 interfaces...
case "$0" in
    *6) DO_IPV4=0 ;;
    *4) DO_IPV6=0 ;;
esac

for dir in /sbin /usr/sbin /bin /usr/bin /usr/etc
do
   if [ -x $dir/ifconfig ]
   then
      IFCONFIG=$dir/ifconfig
      break
   fi
done

if [ -z "$IFCONFIG" ]
then
   echo "Can't find 'ifconfig' on your system."
   exit 1
fi

for dir in /sbin /usr/sbin /bin /usr/bin /usr/etc
do
   if [ -x $dir/netstat ]
   then
      NETSTAT=$dir/netstat
      break
   fi
done

if [ -z "$NETSTAT" ]
then
   echo "Can't find 'netstat' on your system."
   exit 1
fi

SYS=`uname -s`
case $SYS in
   SunOS)
      if [ "$DO_IPV4" = "1" ]
      then
	 IFLIST4=`$IFCONFIG -a | grep inet | sed -e "/inet6/d" -e "/127.0.0.1/d" \
		  -e "s/.*inet//" -e "s/ *//" -e "s/ .*//"`
      fi
      if [ "$DO_IPV6" = "1" ]
      then
	 IFLIST6=`$IFCONFIG -a | grep inet6 | sed -e "s/.*inet6//" \
		  -e "s/ *//" -e "s/ .*//" -e "s:/.*::" -e "/^::1$/d" -e "/^::1%/d"`
      fi
      ;;
   Linux)
      if [ "$DO_IPV4" = "1" ]
      then
	 IFLIST4=`$IFCONFIG -a | grep inet | sed -e "/inet6/d" -e "/127.0.0.1/d" \
		  -e "s/addr://" -e "s/ *//" -e "s/inet *//" -e "s/ .*//"`
      fi
      if [ "$DO_IPV6" = "1" ]
      then
         IFLIST6=`$IFCONFIG -a | grep inet6 | sed -e "s/addr://" \
	          -e "s/ *//" -e "s:/.*::" -e "/ ::1/d" -e "s/inet6 *//" -e "s/ .*//"`
      fi
      ;;
   AIX)
      if [ "$DO_IPV4" = "1" ]
      then
	 IFLIST4=`$IFCONFIG -a | grep inet | sed -e "/inet6/d" -e "/127.0.0.1/d" \
		  -e "s/.*inet//" -e "s/ *//" -e "s/ .*//"`
      fi
      if [ "$DO_IPV6" = "1" ]
      then
	 IFLIST6=`$IFCONFIG -a | grep inet6 | sed -e "s/.*inet6//" \
		  -e "s/ *//" -e "s/ .*//" -e "s:/.*::" -e "/^::1$/d" -e "/^::1%/d"`
      fi
      ;;
   HP*)
      if [ "$DO_IPV4" = "1" ]
      then
         IFLIST4=`$NETSTAT -in -f inet | egrep -v "^lo" | grep -v none | awk '{ if (line++!=0) print $4 }'`
      fi
      if [ "$DO_IPV6" = "1" ]
      then
         IFLIST6=`$NETSTAT -in -f inet6 2>/dev/null | egrep -v "^lo" | grep -v none | awk '{ if (line++!=0) print $3 }' | sed "s:/.*::"`
      fi
      ;;
   OSF*)
      if [ "$DO_IPV4" = "1" ]
      then
         IFLIST4=`$IFCONFIG -a | grep inet | awk '$1 == "inet" && $2 != "127.0.0.1" { print $2 }'`
      fi
      if [ "$DO_IPV6" = "1" ]
      then
         IFLIST6=`$IFCONFIG -a | grep inet | awk '$1 == "inet6" && $2 != "::1" { print $2 }'`
      fi
      ;;
   Darwin)
      if [ "$DO_IPV4" = "1" ]
      then
         IFLIST4=`$IFCONFIG -a | grep inet | sed -e "/inet6/d" -e "/127.0.0.1/d" \
		  -e "/tunnel/d" -e "s/.*inet//" -e "s/ *//" -e "s/ .*//"`
      fi
      if [ "$DO_IPV6" = "1" ]
      then
         IFLIST6=`$IFCONFIG -a | grep inet6 | sed -e "/%lo0/d" \
                  -e "/tunnel/d" -e "s/.*inet6//" -e "s/ *//" -e "s/ .*//" -e "/^::1$/d" -e "s/%.*//"`
      fi
      ;;
   FreeBSD*)
      if [ "$DO_IPV4" = "1" ]
      then
         IFLIST4=`$IFCONFIG -a | grep inet | sed -e "/inet6/d" -e "/127.0.0.1/d" \
		  -e "/tunnel/d" -e "s/.*inet//" -e "s/ *//" -e "s/ .*//"`
      fi
      if [ "$DO_IPV6" = "1" ]
      then
         IFLIST6=`$IFCONFIG -a | grep inet6 | sed -e "/%lo0/d" \
                  -e "/tunnel/d" -e "s/.*inet6//" -e "s/ *//" -e "s/ .*//" -e "/^::1$/d" -e "s/%.*//"`
      fi
      ;;
   IRIX*)
      if [ "$DO_IPV4" = "1" ]
      then
         IFLIST4=`$IFCONFIG -a | grep inet | sed -e "/inet6/d" -e "/127.0.0.1/d" \
		  -e "s/.*inet//" -e "s/ *//" -e "s/ .*//"`
      fi
      ;;
   *)
      echo "Don't know how to get list of network interfaces on $SYS"
      exit 1
      ;;
esac

IFLIST4=`echo $IFLIST4 | sed "s/0\.0\.0\.0//g"`

# Must return "OK" as the first word
echo OK $IFLIST4 $IFLIST6

exit 0
