Visit Jeremy's Blog.
Go Back > Blogs > sag47
User Name


Rate this Entry

Using a script to get your IP address

Posted 01-07-2013 at 11:33 AM by sag47

A friend of mine was complaining today how their work place doesn't parse the IP address from ifconfig but asks for manual input from the user. This is the dumbest thing ever. In case you're ever encountered by this here's a horrible one liner to get the job done quick and dirty.

Also, you may need to replace eth2 with your correct interface (such as eth1).
ifconfig | sed -ne '/^eth2/,/^\s\+Interrupt/p' | awk '$0 ~ /inet addr/ { print $2 }' | cut -d: -f2
It works, move along.
Posted in Uncategorized
Views 9636 Comments 2
« Prev     Main     Next »
Total Comments 2


  1. Old Comment
    Save two commands, get more awk for free:
    /sbin/ifconfig | awk '/inet addr:/ {if ($2 !~ "127.*") print substr($2,6)}'
    Posted 01-07-2013 at 06:32 PM by unSpawn unSpawn is offline
  2. Old Comment
    Thanks . I was discussing it with my local LUG mailing list and here's a good solution they came up with which will present the IP address of the default route (I substituted some of their cuts with awk instead).

    ip -o ro get $(ip ro | awk '$1 == "default" { print $3 }') | awk '{print $5}'
    The main reason why I used
    sed -ne '/^eth2/,/^\s\+Interrupt/p'
    is because I wanted to assume there may be more than one network interface. The sed command helps with cutting out the information only for the intended interface, eth2. It was a fun discussion.
    Posted 01-08-2013 at 04:35 PM by sag47 sag47 is offline


All times are GMT -5. The time now is 09:34 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration