How to get an output formatted as “Interface: IP Address” from ifconfig on Mac

无人久伴 提交于 2019-12-05 17:39:12

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
KeshV

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!