LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-25-2007, 07:47 AM   #1
Big Dog XII
LQ Newbie
 
Registered: May 2004
Location: /UK/ENG/
Distribution: Gentoo
Posts: 7

Rep: Reputation: 0
How to get an IP Address from command line?


Hi there,

Here is a fun tidbit for a Thursday.

Is there a command that will return me just the IP Address for a given interface. Something akin to....

#getip eth0
192.168.1.60
#

And yes I know I could parse out the results from ifconfig, but if there is another way, please let me know.
 
Old 01-25-2007, 08:02 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Why don't you create a simple script and put it on your PATH?
Code:
#/bin/bash
ifconfig $1 | grep "inet addr" | gawk -F: '{print $2}' | gawk '{print $1}'
or alternatively create an alias with the command above. I don't know about any command to do this, except ifconfig.

Last edited by colucix; 01-25-2007 at 08:16 AM.
 
Old 01-25-2007, 08:15 AM   #3
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,009
Blog Entries: 5

Rep: Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785
You could do it by extracting it from the config file for the interface.

e.g. for RH/FC:

Code:
grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= '{print $2}'
You dismissed ifconfig for some reason. I don't know of a command that will give you just the IP. I wonder if you're aware you can do ifconfig by interface so you only have to parse from the specific interface you're interested in:

Code:
ifconfig eth0 |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}'
So far as making a single command you could easily do it with a script:

Code:
#!/bin/bash
IFACE=$1
ifconfig $IFACE |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}'
You just name the above script "getip" and put execute permission on it with chmod. You can then do your "getip eth0" using this script. A side benefit is if you don't put eth0 or another interface at command line (i.e. just type "getip") it will show the IPs for all active interfaces including lo0 (127.0.0.1).
 
1 members found this post helpful.
Old 01-25-2007, 08:37 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Just a suggestion:
Code:
#!/bin/bash

/sbin/ifconfig $1 | grep "inet addr" | gawk -F: '{print $2}' | gawk '{print $1}'
Save as a script (getip.sh), and execute with the syntax: "getip.sh eth1" without the quotes. Change eth1 to whichever interface you wish to know the IP of...

edit: beaten by jlightner... damn!
 
Old 01-25-2007, 11:54 AM   #5
Big Dog XII
LQ Newbie
 
Registered: May 2004
Location: /UK/ENG/
Distribution: Gentoo
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks!

Hi all,

Thanks for the replys. I knew about the ifconfig. Just checking to see if there was something else.

Thanks again.

Big Dog XII (woof)
 
Old 09-17-2014, 04:22 AM   #6
vimal09
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Rep: Reputation: 0
Quote:
Originally Posted by Big Dog XII View Post
Hi there,

Here is a fun tidbit for a Thursday.

Is there a command that will return me just the IP Address for a given interface. Something akin to....

#getip eth0
192.168.1.60
#

And yes I know I could parse out the results from ifconfig, but if there is another way, please let me know.

Nice post its really useful for me to find my internal or private ip address of my system with linux OS ..After finding my internal ip address i visited sites like Ip-Details.com to find my present network ip address ...
 
  


Reply

Tags
commandline, networking


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
ip address using command line Ammad Linux - Networking 2 04-24-2007 06:12 PM
command line address book program oneferna Linux - Software 3 02-12-2006 02:24 PM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 07:45 PM
51 characters only in the 1st Line of command line eggCover Linux - General 2 07-29-2004 01:28 PM


All times are GMT -5. The time now is 08:13 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration