4

I am trying to get the following formatted output out of ifconfig:

en0: 10.52.30.105
en1: 10.52.164.63

I've been able to at least figure out how to get just the IP addresses (weeding out localhost) with the following command, but it's not sufficient for my requirements:

ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'

Thanks!

Joey
  • 344,408
  • 85
  • 689
  • 683
Frank
  • 110
  • 8

2 Answers2

7

This works on FreeBSD, which is at the heart of an apple :-)

#!/bin/sh
for i in $(ifconfig -l); do
   case $i in
   (lo0)
      ;;
   (*)
      set -- $(ifconfig $i | grep "inet [1-9]")
      if test $# -gt 1; then
         echo $i: $2
      fi
   esac
done
Jens
  • 69,818
  • 15
  • 125
  • 179
  • @Frank: Good. The next step is voting up (clicking the triangle above the number to the left) and/or "accepting" the answer by clicking the check sign below the lower triangle. – Jens Aug 29 '11 at 19:14
  • Apologies for the delay. I accepted the answer and would love to vote it up but don't have enough "reputation" yet. – Frank Sep 09 '11 at 23:23
0

On Debian/RHEL systems you can do the following ---

#!/bin/sh
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

echo "Interface: IP : MASK : BROADCAST : HWADDR"

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

for i in $(ifconfig -a| grep -v ^$| grep ^[a-z*] | awk '{print $1}')

do

     case $i in 

           (lo)
                   ;;

           (*)
         ip=`(/sbin/ifconfig $i | awk /'inet addr/ {print $2}' | cut -f2 -d":" )`
         bcast=`(/sbin/ifconfig $i | awk /'Bcast/ {print $3}' | cut -f2 -d":" )`
         mask=`(/sbin/ifconfig $i | awk /'inet addr/ {print $4}' | cut -f2 -d":" )`
         hwaddr=`(/sbin/ifconfig $i | awk /'HWaddr/ {print $4,$5}' | cut -f2 -d" " )`

         if [ -z $ip ]; then
            ip="NA"
         fi

         if [ -z $bcast ]; then
           bcast="NA"
         fi

         if [ -z $mask ]; then
           mask="NA"
         fi

         if [ -z $hwaddr ]; then
           hwaddr="NA"
         fi

            echo $i: $ip : $mask : $bcast : $hwaddr
            ;;

    esac
done
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
KeshV
  • 626
  • 7
  • 8