Skip to content

Instantly share code, notes, and snippets.

@marccampbell
Created January 30, 2012 23:56
Show Gist options
  • Save marccampbell/1707641 to your computer and use it in GitHub Desktop.
Save marccampbell/1707641 to your computer and use it in GitHub Desktop.
Get Private IP Address
{ok, Interfaces} = inet:getifaddrs(),
Addresses = lists:map(fun({_, Props}) -> proplists:get_all_values(addr, Props) end, Interfaces),
Private_Addresses = lists:filter(fun({F,_,_,_}) -> lists:member(F, [10, 172, 192]); (_) -> false end, lists:flatten(Addresses)),
io:format("Private Addresses = ~p\n", [Private_Addresses]),
case Private_Addresses of
[] -> io:format("Unable to start monitor listener, no private address was found.\n", []),
undefined;
_ -> {A, B, C, D} = lists:nth(1, Private_Addresses),
IP_Address = lists:flatten(io_lib:format("~.10B.~.10B.~.10B.~.10B", [A, B, C, D])),
io:format("Starting listener on ~s\n", [IP_Address]),
IP_Address
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment