Skip to content

Instantly share code, notes, and snippets.

@entone
Last active October 3, 2017 20:07
Show Gist options
  • Save entone/a59fd1fa9accca06d7ca38f0976ebd76 to your computer and use it in GitHub Desktop.
Save entone/a59fd1fa9accca06d7ca38f0976ebd76 to your computer and use it in GitHub Desktop.
iterate over local network interfaces and find first non local ip4v address
def get_ipv4_address() do
:inet.getifaddrs()
|> elem(1)
|> Enum.find(fn {_interface, attr} ->
case attr |> Keyword.get_values(:addr) do
[] -> false
list -> list |> Enum.find(fn(addr) ->
case addr do
nil -> false
{127, 0, 0, 1} -> false
{_, _, _, _, _, _, _, _} -> false
{_, _, _, _} -> true
end
end)
end
end)
|> elem(1)
|> Keyword.fetch(:addr)
|> elem(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment