Unix Programming - get ip addresses

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2004 > get ip addresses





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author get ip addresses
vertigo

2004-09-02, 6:51 pm

Hello
I want to read all ip addresses connected with local machine. I did:

if (gethostname(hostname,MAX_STRING_SIZE)!=
0){
exit(0);
}
struct hostent *h=gethostbyname((const char*)hostname);
int n=0;
for (char** ptr=h->h_addr_list; *ptr; ++ptr,n++){
printf("%s\n",inet_ntoa(*(struct in_addr*)h->h_addr_list[n]));
}

but i received only one ip address connected with eth0.
Morover on freebsd i received only 127.0.0.1.
What about aliases ? On linux i have eth0:1 and i did not received
that ip this way.
Where's my mistake ?

Thanx
Michal

Fletcher Glenn

2004-09-02, 6:51 pm


"vertigo" <none@dev.null> wrote in message
news:ch79d8$ert$1@atlantis.news.tpi.pl...
> Hello
> I want to read all ip addresses connected with local machine. I did:
>
> if (gethostname(hostname,MAX_STRING_SIZE)!=
0){
> exit(0);
> }
> struct hostent *h=gethostbyname((const char*)hostname);
> int n=0;
> for (char** ptr=h->h_addr_list; *ptr; ++ptr,n++){
> printf("%s\n",inet_ntoa(*(struct in_addr*)h->h_addr_list[n]));
> }
>
> but i received only one ip address connected with eth0.
> Morover on freebsd i received only 127.0.0.1.
> What about aliases ? On linux i have eth0:1 and i did not received
> that ip this way.
> Where's my mistake ?
>
> Thanx
> Michal
>


The program "netstat" knows how to get the answer. Try looking at the
FreeBSD source for netstat. I'm sure you can get some hints from that.
However, whenever you're looking for information about stuff that is not
part of your own process, be prepared to have to be root in order to get the
info.

--

Fletcher Glenn


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com