ifconfig -u|grep -v inet6|grep -v media| grep -v lladdr|grep -v ether|grep -v status|sed "s/flags=.*//"|sed "s/^.*inet \(.*\) netmask.*$/\1/"|sed "s/^\([elfv]\)/#\1/"|tr -d '\n'|tr '#' '\n' && echo
I just want a simple display of the interfaces on my system and their IPs. I was in a rush and came up with that disgusting line. On the one hand it demonstrates the power of Unix, on the other hand it demonstrates the problems with it. So, dear interwebs, please provide me with (in order of preference):
- A better way of doing it (I'm thinking sysctl, [I'm on a Mac])
- The right command line magic to get better greppable output from ifconfig
- An optimised command line, specifically:
- How can you combine the multiple "grep -v" commands?
- How can I combine the sed & tr commands?
Failing that, here's a command you too can use to give you a fragile list of interfaces and their ipv4 addresses. I've embedded it on my desktop with GeekTool (OSX). It makes the FW logs also embedded on my desktop make more sense :)
UPDATE: I love you my fellow Geeks. The winning solution is from Craig Balding via twitter, who put us all to shame with the ridiculously simple piece of cli kung-fu that is:
ifconfig|awk '/mtu/ {nic=$1} /inet / {print nic " " $2}'
Notable mentions to:
Doug Burks who initially
came
up with both the shortest, easiest to read & least fragile solution
with a combination of (1) & (2). However, it was noticeably slower
than the others due to awk. At only 2chars longer, but faster, my
modification of his is:
for i in `ifconfig -lu`; do echo -n $i:\ ; ifconfig $i |grep inet\ |grep -o "[1-9][^ ]* " | tr -d '\n'; echo; done
Rogan Dawes who greatly optimised ala (3) (with some ugliness from mine to make the tuples take one line):
ifconfig -u | egrep "^[a-z]|inet " | sed -e "s/ flags.*$//" -e"s/^.*inet \(.*\) netmask.*$/ \1/"|sed "s/^\([elfv]\)/#\1/"|tr -d '\n'|tr '#' '\n'&& echo
Haroon Meer who aimed for (1)
using OSX's networksetup. However, networksetup doesn't list all
interfaces (e.g. the vmware interfaces). Also, combing the output to one
line per tuple is a pain.
Tracked: Jun 22, 21:39