hi!
I'm trying to figure out a way to get the IP addresses associated with all network interfaces in my laptop. I have two interfaces :
1) eth0 - ethernet
2) eth1 - cisco wireless card
I read around and wrote this small program:
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
int main() {
char hostname[100];
int ret = gethostname(hostname, sizeof(hostname));
printf("Hostname: %s\n", hostname);
struct hostent *ht = gethostbyname(hostname);
int i=0;
while(ht->h_addr_list[i] != NULL) {
printf("Address[%d]: %s\n", i, inet_ntoa(*((struct in_addr *)ht->h_addr_list[i])));
i++;
}
return 0;
}
But the problem is that this only returns me the IP of the loopback interface 127.0.0.1 and nothing on eth0 and eth1 when both have IPs assigned to them.
I will be grateful for any help in this regard.
Fahad.
Start Free Trial