Unix Command to get IP Address ?

Status
Not open for further replies.

kalpesh

Registered
HI all ,
I wanted to know what is the unix command to get the IPAddress of the machine on which we are working.I mean the command with which I can get to know that the IPAddress of the machine on which I am working is say "130.212.456.375".

Is there any such unix command ?
 
ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2

the command above will tell you your IP - but if you are connecting via a DSL
modem and using PPPoE or DHCP, you will need to access the DSL GUI and look under Internet Connection Details to get the actual IP address.
 
Ifconfig displays the current configuration for a network interface when
no optional parameters are supplied. If a protocol family is specified,
ifconfig will report only the details specific to that protocol family.

man ifcofig
tells you more.

So if you know you are using e.g. en0
ifconfig en0
will give you the details. No additions gives all your network info, and you can see how you are connected as well.
 
ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2

the command above will tell you your IP - but if you are connecting via a DSL
modem and using PPPoE or DHCP, you will need to access the DSL GUI and look under Internet Connection Details to get the actual IP address.


$ ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2
cut: bad delimiter


$ifconfig | grep "inet " | grep -v 127.0.0.1
inet 130.212.134.208 netmask 0xffffff00 broadcast 130.212.134.255

The one highlighted in blue is my IPAddress.
 
To isolate just the assigned IP address on a machine, you could do something like this:

ifconfig | grep "inet\b" | grep -v '127.0.' | sed 's/inet \([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\) .*$/\1/'

Although if you have more than one active IP address, e.g., if you have a wireless and a wired connection at the same time, you'll get more than one match. To get just the first of these, you can modify sed a bit:

ifconfig | grep "inet\b" | grep -v '127.0.' | sed -e 's/inet \([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\) .*$/\1/' -e '1d'
 
Also, I think g/re/p's "cut" command works like this:

ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d " " -f2

Which is more efficient than my suggestion.
 
$ ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2
cut: bad delimiter

Using spaces as a delimiter: two spaces between the -d\ and -f2

ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\__-f2
(remove the underscores)



* for some reason, cut and paste did not retain the exact syntax
 
Status
Not open for further replies.
Back
Top