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

Getting eth0 ip address

1 view
Skip to first unread message

Teemu

unread,
Jan 31, 2005, 8:59:24 AM1/31/05
to
Hi!

Can anyone tell me howto read the external ip adress of i.e. eth0?
And no I don't want to use ifconfig for it.

-Teemu

Juha Laiho

unread,
Jan 31, 2005, 12:42:25 PM1/31/05
to
johanna...@welho.com said:
>Can anyone tell me howto read the external ip adress of i.e. eth0?
>And no I don't want to use ifconfig for it.

~$ /sbin/ip -f inet -o addr show dev eth0 primary
2: eth0 inet 192.168.105.10/24 brd 192.168.105.255 scope global eth0

.. but if you meant that you don't want to use an external command for
finding the address, then I guess you could look at the source of
ifconfig (or 'ip') command to find out how they do it.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

Lew Pitcher

unread,
Jan 31, 2005, 12:57:41 PM1/31/05
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I was once told (and I quote):

open a
fh=socket(PF_INET,SOCK_DGRAM,IPPROTO_IP);
then use a
struct ifreq if;
and set
strcpy(if.ifr_name,"eth0");
then query by
ioctl(fh,SIOGIFADDR,&if);
and see in
if.ifr_addr
the value!.

HTH

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB/nGVagVFX4UWr64RAusRAKCpFXMs00FAM4udAqkjcSn3Q8NluACgkWwO
RtqvGjgjElE37u/FWMsurlM=
=+dvj
-----END PGP SIGNATURE-----

Jose Maria Lopez Hernandez

unread,
Jan 31, 2005, 1:23:07 PM1/31/05
to

ip address show eth0

> -Teemu

Regards.

--

Jose Maria Lopez Hernandez
Director Tecnico de bgSEC
jker...@bgsec.com
bgSEC Seguridad y Consultoria de Sistemas Informaticos
http://www.bgsec.com
ESPAÑA

The only people for me are the mad ones -- the ones who are mad to live,
mad to talk, mad to be saved, desirous of everything at the same time,
the ones who never yawn or say a commonplace thing, but burn, burn, burn
like fabulous yellow Roman candles.
-- Jack Kerouac, "On the Road"

Kasper Dupont

unread,
Jan 31, 2005, 4:35:59 PM1/31/05
to

Robert Larsen

unread,
Mar 16, 2005, 10:06:25 AM3/16/05
to

#include <iostream>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>

using namespace std;

int main(int argc, char ** argv)
{
char hostname[256];

if(gethostname(hostname,256) != 0)
{
cout << "Could not get hostname." << endl;
return -1;
}

cout << hostname << endl;
struct ifreq my_ifreqs[10];
struct ifconf my_ifconf;
my_ifconf.ifc_len = sizeof(my_ifreqs);
my_ifconf.ifc_req = my_ifreqs;
int num, i, sock = socket(AF_INET,SOCK_DGRAM,0);
if(socket == 0)
{
cout << "Could not create socket." << endl;
return -1;
}

if(ioctl(sock,SIOCGIFCONF,&my_ifconf) < 0)
{
cout << "Error in ioctl()." << endl;
close(sock);
return -1;
}
close(sock);

num = my_ifconf.ifc_len / sizeof(struct ifreq);
for(i = 0; i < num; i++)
{
struct sockaddr_in * adr = (struct sockaddr_in *)&my_ifreqs[ i
].ifr_ifru.ifru_addr;
cout << inet_ntoa((struct in_addr)adr->sin_addr) << endl;
}

return 0;
}

robert@robert:~/code$ ./test
robert
127.0.0.1
192.168.1.106
robert@robert:~/code$ /sbin/ifconfig
eth0 Link encap:Ethernet HWaddr 00:02:B3:A2:1B:66
inet addr:192.168.1.106 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:35556 errors:0 dropped:0 overruns:0 frame:0
TX packets:35647 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12387898 (11.8 MiB) TX bytes:5655407 (5.3 MiB)
Interrupt:9 Base address:0xd800 Memory:dd800000-dd800038

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:304659 errors:0 dropped:0 overruns:0 frame:0
TX packets:304659 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:25107465 (23.9 MiB) TX bytes:25107465 (23.9 MiB)

robert@robert:~/code$

0 new messages