Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IP Address

0 views
Skip to first unread message

Dan

unread,
Jun 1, 2003, 12:04:14 PM6/1/03
to

I know I can find my ip address with an ifconfig. But is there some
environmental variable or some other way to find out the ip address?
Is there some way to find it from within a program?

Dan

Bill Unruh

unread,
Jun 1, 2003, 1:34:29 PM6/1/03
to
Dan <d...@dontspammecauseidontlikit.com> writes:

]I know I can find my ip address with an ifconfig. But is there some


]environmental variable or some other way to find out the ip address?
]Is there some way to find it from within a program?


You do not have an ip address. Your connection does. Thus "you" could have 5
different addresses, and almost certainly hae at least 2.
If you want to make an environment variable, use
export IPADDR=`ifconfig eth0|grep 'inet addr'|awk '{print $2}'|awk -F: '{print $2}'`
and put that line into .bashrc
That assumes you want the IP to eth0.

Sybren Stuvel

unread,
Jun 1, 2003, 1:36:26 PM6/1/03
to
Dan enlightened us with:

> I know I can find my ip address with an ifconfig. But is there some
> environmental variable or some other way to find out the ip address?

There is no such thing as *the* IP address. Every TCP/IP network device
has an IP address, so a single computer can even have hundreds of them.
Choosing one of them to be *the* IP address is kinda pointless.

> Is there some way to find it from within a program?

Of course there is. Just think: if there were not such a way, how would
ifconfig function? I don't know the answer to your next question (how
can I do that?), but I'm sure the ifconfig source will give you the
answer.

Sybren
--
| Somebody ought to cross ball point pens with coat hangers so that |
| the pens will multiply instead of disappear. |
\-------------------------------------------------------------------/
If you have to use Outlook, fix it: http://flash.to/oe-quotefix/

David

unread,
Jun 1, 2003, 2:00:18 PM6/1/03
to

IPADDR=`/sbin/ifconfig eth0 | grep inet | cut -c 21-35`

--
Confucius: He who play in root, eventually kill tree.
Registered with The Linux Counter. http://counter.li.org/
Slackware 9.0 Kernel 2.4.20 i686 (GCC) 3.3
Uptime: 13 days, 10:04, 1 user, load average: 1.10, 1.31, 1.46

menno

unread,
Jun 1, 2003, 3:13:13 PM6/1/03
to
On Sun, 01 Jun 2003 18:00:18 +0000, David wrote:
> Bill Unruh wrote:
>> Dan <d...@dontspammecauseidontlikit.com> writes:
>>
>> ]I know I can find my ip address with an ifconfig. But is there some
>> ]environmental variable or some other way to find out the ip address?
[...]

>> export IPADDR=`ifconfig eth0|grep 'inet addr'|awk '{print $2}'|awk -F: '{print $2}'`
[...]

> IPADDR=`/sbin/ifconfig eth0 | grep inet | cut -c 21-35`

export IPADDR="`/sbin/ifconfig eth0 |awk '/inet/{print substr($2,6)}'`"

--
-Menno.

Michael Heiming

unread,
Jun 1, 2003, 3:30:54 PM6/1/03
to

ip=$(expr "$($IFCONFIG $DEVICE | grep addr:)" : ".*addr:\([0-9.]*\).*")

;)

--
Michael Heiming

Remove +SIGNS and www. if you expect an answer, sorry for
inconvenience, but I get tons of SPAM

menno

unread,
Jun 1, 2003, 5:11:22 PM6/1/03
to
On Sun, 01 Jun 2003 21:30:54 +0200, Michael Heiming wrote:

[...]


> ip=$(expr "$($IFCONFIG $DEVICE | grep addr:)" : ".*addr:\([0-9.]*\).*")

Wow, kewl.

BTW, if the user has no access to `ifconfig':
IP=`netstat -ei |grep -A1 eth0 |awk '/inet/{print substr($2, 6)}'`

--
-Menno.

Chris F.A. Johnson

unread,
Jun 1, 2003, 8:41:43 PM6/1/03
to

config=`ifconfig eth0`
config=${config#*addr:}
IPADDRESS=${config%% *}

--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2003, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License

Bill Marcum

unread,
Jun 2, 2003, 2:31:11 AM6/2/03
to
You might just think you have no access to ifconfig, because it's in /sbin,
which is not usually in the PATH for non-root users.

--
This place is called Antarctica. There is no kitten here.

Davorin Vlahovic

unread,
Jun 3, 2003, 3:54:50 PM6/3/03
to
In article <slrnbdkecp.2...@sybren.thirdtower.com>, Sybren Stuvel wrote:
>
> Of course there is. Just think: if there were not such a way, how would
> ifconfig function? I don't know the answer to your next question (how
> can I do that?), but I'm sure the ifconfig source will give you the
> answer.
>

The ipconfig is fed by a startup script. Depending on which
distribution you use, you have to look it up in a file.

This is what it looks like in my Debian v. 3.0:
/etc/network/interfaces :
------------start-----------------
iface lo inet loopback

iface eth0 inet static
address 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

iface eth1 inet static
address 192.168.1.10
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

iface eth1:1 inet static
address 192.168.1.2
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
-----------stop-------------------

So, if you write a proggie to parse it all up, you can find IP address
of a specific adapter.

If you wish to know an IP for, let's say, ppp connection, just add this
into /etc/ppp/ip-up:
echo "REMOTE-IP=$PPP_REMOTE" > /etc/remote-ip

and on every connect, the remote IP address will be stored in
/etc/remote-ip file. To clean it up, just add:
echo "" > /etc/remote-ip
into a /etc/ppp/ip-down file.

That's all folks!
--
<html><form><input type crash></form></html>
if (!shost) panic("Splunge!");
-- iz /usr/src/linux-2.4.20/drivers/scsi/aha1542.c

0 new messages