Advertisement

08.19.2005 at 04:07AM PDT, ID: 21532777
[x]
Attachment Details

find IP address of local machine

Asked by fahadkhowaja in Linux Programming

Tags: ip, address, find, machine, get

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
 
Keywords: find IP address of local machine
 
Loading Advertisement...
 
[+][-]08.19.2005 at 04:13AM PDT, ID: 14707874

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.19.2005 at 07:12AM PDT, ID: 14709300

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Linux Programming
Tags: ip, address, find, machine, get
Sign Up Now!
Solution Provided By: ravenpl
Participating Experts: 3
Solution Grade: A
 
 
[+][-]08.19.2005 at 07:52AM PDT, ID: 14709726

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]08.24.2005 at 06:24AM PDT, ID: 14742203

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32