How do I find my own IP address?

Bengt Kleberg Bengt.Kleberg@REDACTED
Wed Jan 21 17:01:03 CET 2004


Joe Armstrong wrote:
> I need to know my own IP address
> 
> I can find my hostname
> 
> 1> inet:gethostname().
> {ok,"enfield"}
> 
> And do this
> 
> 2> inet:gethostbyname("enfield").
>  
> {ok,{hostent,"localhost.localdomain",
>              ["localhost","enfield"],
>              inet,
>              4,
>              [{127,0,0,1}]}}
> 
> But 127.0.0.1 is not my IP address

if you are interested i have written a c program to do some of the 
things above (this is what i suppose the inet module is/does).
if you run it, what kind of information does it print?


bengt

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>






main(int argc, const char* argv[])
{
	char name[80];
	struct hostent *hp;
	char **p;

	if (0 != gethostname(name, sizeof (name))) {
		(void) printf("gethostname failed\n");
		exit (1);
	}

	hp = gethostbyname(name);
	if (hp == NULL) {
		(void) printf("host information for %s not found\n", name);
		exit (3);
	}

	for (p = hp->h_addr_list; *p != 0; p++) {
		struct in_addr in;
		char **q;

		(void) memcpy(&in.s_addr,	*p, sizeof (in.s_addr));
		(void)printf("%s\t%s", inet_ntoa(in),	hp->h_name);

		for (q = hp->h_aliases; *q != 0; q++) {
			(void) printf(" %s", *q);
		}

		(void) putchar('\n');
	}

	exit (0);
}



More information about the erlang-questions mailing list