How do I find my own IP address?

Samuel Rivas samuel@REDACTED
Thu Jan 22 11:09:23 CET 2004


Joe Armstrong wrote:
> 
> I need to know my own IP address
> 
> The undocumented function inet:getif()
> produces
> 
> 
> 4> inet:getif().
> {ok,[{{193,10,65,254},{193,10,67,255},{255,255,252,0}},
>      {{127,0,0,1},{127,255,255,255},{255,0,0,0}}]}
> 
> The first element of this list appears to be my IP address - 
> can I reply on this????

Hi:

I dealt with the same problem in a host which:
  * Does not have a public name in the network
  * Gets its IP with DHCP
  * Needs to show that IP on a display (to be somehow reachable)

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}}]}

You can also know the broadcast and netmask if you write
[addr,broadaddr,netmask] instead of [addr].

However, as long as it is not a standard way to do this kind of things I
use a more triky solution in my app: I parse the output of
/sbin/ifconfig eht0.

Would be nice if it were possible to do that in cleaner way in future
OTP releases.

> 
> /Joe
> 

Regards

-- 
 ---------------------
|	Samuel        |
 ---------------------



More information about the erlang-questions mailing list