Grep IP addresses from ifconfig

Posted in Linux

Here’s a bunch of ways to get the ip addresses for all your network interfaces using ifconfig.

$ ifconfig | awk ‘/dr:/{gsub(/.*:/,”",$2);print$2}’
$ ifconfig | grep inet | awk -F: ‘{ print $2 }’ | awk ‘{ print $1 }’
$ ifconfig | sed -n ‘/dr:/{;s/.*dr:\([0-9.]\+\) .*/\1/;p;}’
$ ifconfig | perl -nle ‘/addr:([^ ]+)/ and print $1′
$ ifconfig | perl -nle ‘/dr:(\S+)/ && print $1′
$ ifconfig | sed -rn ’s/.*r:([^ ]+) .*/\1/p’
$ ifconfig | perl -nE ‘/dr:(\S+)/&&say$1′

If you know which interface you want, add it to the ifconfig command, like

ifconfig eth0 | blah blah blah…

If you just want the first one no matter what name it is, the easiest way would be to add a “-m1″ to grep. I had to do this because some hosts had eth0 others were using eth1, but all had their regular network connection listed first. the hosts all have 2 nic’s and it just depends on which one they’re plugging in.

Posted by admica   @   5 February 2009

Related Posts

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« Setup linux host monitoring and self healing using Monit
Next Post
My product reviews on Amazon »
Powered by Wordpress   |   Lunated designed by ZenVerse