How do I find my own IP address?

Joachim Durchholz joachim.durchholz@REDACTED
Thu Jan 22 15:07:14 CET 2004

Samuel Rivas wrote:
> Following your discovery I've found out an (undocumented) way to get the
> host IP within Erlang:
> 1> {ok, S} = inet_udp:open(0, []).
> {ok,#Port<0.4112>}
> 2>prim_inet:ifget(S, "eth0", [addr]).
> {ok,[{addr,{192,168,100,100}}]}

This will work on a vanilla Unix system.
It will not work, for example, if you use FreeS/WAN (where the network 
device to use would be ipsec0 for an encrypted connection, and eth0 for 
an unencrypted one). There may be other software that installs virtual 
network cards ("interfaces"). I'm not sure, but I suspect that even an 
ipsec interface might have a regular IP address, including DNS entry, 
routing table, and all.
It will also not properly work if the machine has two network 
interfaces. For example, I'm sitting on a desktop system that has a 
network card (for the LAN) and an ISDN adapter that pretends to be a 
network card. If I were running Unix, I'd have an eth0 and an eth1 card, 
one of them with a DHCP-generated IP, the other with a local masqueraded 
192.168.x.x address. There's a third local network that's entirely 
virtual, for the virtual network that serves the VMWare machines. I 
think this kind of setup isn't too unusal for telecommuters (well, 
except for the VMWare bit maybe... *g*)
Oh, and I'm running Windows, which means that my machine will respond 
with utter ignorance if you try names like /dev/eth0 on it :-)

... and, no, I'm not picking nits here. FreeS/WAN is an important use 
case (particularly for X sessions over unsecured connections), and both 
X servers and clients do run under Windows. It's OK to rely on eth0 and 
other Unix specifica if you know that your application is going to run 
on machines that you control, but that's not a viable option for 
general-purpose libraries.

Currently looking for a new job.

More information about the erlang-questions mailing list