Last active
October 3, 2017 20:07
-
-
Save entone/a59fd1fa9accca06d7ca38f0976ebd76 to your computer and use it in GitHub Desktop.
iterate over local network interfaces and find first non local ip4v address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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