How to get an output formatted as "Interface: IP Address" from ifconfig on Mac
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) w开发者_如何学Goith 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!
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
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
精彩评论