LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Blogs > sag47
User Name
Password

Notices


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).
Code:
ifconfig | sed -ne '/^eth2/,/^\s\+Interrupt/p' | awk '$0 ~ /inet addr/ { print $2 }' | cut -d: -f2
It works, move along.
Posted in Uncategorized
Views 10197 Comments 2
« Prev     Main     Next »
Total Comments 2

Comments

  1. Old Comment
    Save two commands, get more awk for free:
    Code:
    /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).

    Code:
    ip -o ro get $(ip ro | awk '$1 == "default" { print $3 }') | awk '{print $5}'
    The main reason why I used
    Code:
    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 03:36 AM.

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