LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to get an IP Address from command line? (https://www.linuxquestions.org/questions/linux-general-1/how-to-get-an-ip-address-from-command-line-522676/)

Big Dog XII 01-25-2007 07:47 AM

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.

colucix 01-25-2007 08:02 AM

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.

MensaWater 01-25-2007 08:15 AM

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).

pwc101 01-25-2007 08:37 AM

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!

Big Dog XII 01-25-2007 11:54 AM

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)


All times are GMT -5. The time now is 10:11 AM.