Tuesday 6 January 2009

Finding IP addresses hidden in "ifconfig -a"

Wow...is it really over a year since my last blog post ?

Anyway, here's something which I hope will be useful.

I find it awkward to pick out my Mac's IP addresses at a glance from the output of "ifconfig -a".  Here's a command-line that makes it very easy...

[eamonn@stig ~]$ ifconfig -a | perl -ne '$interface=$1 if (/^([a-z]+[0-9]+:)/); print "$interface $1\n" if (/inet (\d+\.\d+\.\d+\.\d+)/);'

Now even the most commited command-line disciple is likely to baulk at typing all that, so I have an alias set up in ~/.bash_login.  The quotes are a little fiddly to get right, so here's the alias...

alias ipa='ifconfig -a | perl -ne '\''$interface=$1 if (/^([a-z]+[0-9]+:)/); print "$interface $1\n" if (/inet (\d+\.\d+\.\d+\.\d+)/);'\'
A slight variation of the same thing works on Linux also (with adjustments to compensate for the differences  in output format)...
alias ipa='ifconfig -a | perl -ne '\''$interface=$1 if (/^([a-z]+[0-9]+) /); print "$interface $1\n" if (/inet addr:(\d+\.\d+\.\d+\.\d+)/);'\'

I hope its useful...

No comments:

Post a Comment