Main Topics
Browse All Topicshi!
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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
fd = socket (AF_INET, SOCK_DGRAM,IPPROTO_IP);
memset (&ifr, 0, sizeof (struct ifreq));
strcpy (ifr.ifr_name, IFNAME);
ifr.ifr_addr.sa_family = AF_INET;
ioctl(fd, SIOCGIFADDR, &ifr);
addr=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_addr)->sin_addr)
printf("eth %s, addr %d.%d.%d.%d\n", ifr.ifr_name,addr[0],addr[
close(fd);
You can find all the interfaces on your system as well as IP adresses assigned to each interface. Check this link
http://www.daimi.au.dk/~ka
Business Accounts
Answer for Membership
by: ozoPosted on 2005-08-19 at 04:13:39ID: 14707874
netstat -nr
ifconfig eth0
or
ipconfig