troubleshooting Question

find IP address of local machine

Avatar of fahadkhowaja
fahadkhowaja asked on
Linux OS Dev
4 Comments1 Solution38507 ViewsLast Modified:

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])));
        return 0;

But the problem is that this only returns me the IP of the loopback interface and nothing on eth0 and eth1 when both have IPs assigned to them.

I will be grateful for any help in this regard.

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros