Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's my IP? 3

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
How do I figure out my IP address on a linux machine? (It was assigned via DHCP.

Thanks,

Rob

(And I'm behind a firewall, so a website which'll spit it back won't work)
 
This little script works for me -
[tt]
#!/bin/bash
/sbin/ifconfig | (grep 'inet addr:.*P\-t\-P' || echo '127.0.0.1') | awk '{sub(/inet addr:/, ""); printf("%s\n", $1)}'
[/tt]
 
'ifconfig' will give you all network connections.


'ifconfig eth0' will give you only eth0 (the first ethernet device) information.


Next examine the output

inet addr: is your IP address. Bcast: Broadcast and Mask: is your netmask

The above script from ppc386 is more complicated than you need.

This will work
ifconfig eth0 | grep inet | awk '{print $2}'

Exchange eth0 for each device you want or just leave off eth0 to see all devices.

If you want to just see the ip and not see addr:x.x.x.x then add | awk -F ":" '{print $2}' to the command earlier. This will split addr:x.x.x.x into 2 seperate fields split on the :. $1 everything before the : and $2 everything after the colon.

 
> The above script from ppc386 is more complicated than you need.

I guess that depends on what you need.

The reason I put "#!/bin/bash" at the beginning of my scripts
is so the "file" command will tell me it is a script.
So if I do:
% file ~/bin/my-ip
I get:
~/bin/my-ip: Bourne-Again shell script text
instead of:
~/bin/my-ip: ASCII English text

That way, I can do something like:
file ~/bin/* | grep Bourne
- to get a list of all the scripts in a directory.

Since /sbin/ is only in the path for root on my machine,
I have to specify /sbin/ifconfig to run the script as
a normal user.

I connect to the net with a dial-up connection, so if the
connection is down, doing "ifconfig ppp0" gives me nothing
but an ugly error message:

ppp0: error fetching interface information: Device not found

That's not what I want, I would rather get the loopback IP if
ppp0 is down ( that's what the "|| echo 127.0.0.1" is about )

As far as chopping the "inet addr:" from the string...

I really don't see how my:
awk '{sub(/inet addr:/, ""); printf("%s\n", $1)}'

is any more complicated than your:
awk '{print $2}' | awk -F ":" '{print $2}'

Why use two awks when one will do?

I like your idea of specifying the interface though,
( it cleans up the grep expression )

So I changed my script a little -
[tt]
/sbin/ifconfig ppp0 2> /dev/null | (grep inet || echo 127.0.0.1) | awk '{sub(/inet addr:/, ""); printf("%s\n", $1)}'
[/tt]
 
ppc386

you are correct. It really does depend on your needs. And true why use 2 awks if you can use just 1. Kinda habit really for me from my earlier days.

I like your awk better, I should take time to learn more advanced awk techniques. Thanx for the tip as well.
 
That's the beautiful thing about open source, we all learn
from each other. To be honest, I didn't even know you could
specify an interface to ifconfig. So I learned something here
as well.

I tried reading through the awk maual, and picked up a couple
of tricks just before smoke started coming out my ears. [tt]*-@v@-*[/tt]

So I still have a long way to go...

tnx 2u2

- Jeff
 
Well, why use [tt]grep[/tt] and [tt]awk[/tt] when you can just use [tt]awk[/tt]?

[tt]#!/bin/bash

/sbin/ifconfig | awk '{
if ($0 ~ "inet addr")
{
sub("addr:", "", $2)
print $2
}
}'[/tt] //Daniel
 
Cool !

One more slight change:

[tt]
#!/bin/bash

/sbin/ifconfig ppp0 2> /dev/null | awk '{
if ($0 ~ "inet addr") {
sub("addr:", "", $2)
print $2
found = 1
}
} END { if ( !found ) { print "127.0.0.1" }
}'

[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top