Skip to content

Instantly share code, notes, and snippets.

@wesleyit
Created October 18, 2017 01:44
Show Gist options
  • Save wesleyit/b01c5718de61f015e7d0eb54554bae71 to your computer and use it in GitHub Desktop.
Save wesleyit/b01c5718de61f015e7d0eb54554bae71 to your computer and use it in GitHub Desktop.
Get all IPs addresses using Elixir
defmodule IPCluster do
def get_inet(), do: elem(:inet.getif(), 1)
def get_ip_list([]), do: []
def get_ip_list([h|t]) do
{ip, _bcast, mask} = h
[[Tuple.to_list(ip), Tuple.to_list(mask)] | get_ip_list(t)]
end
def ip_list([]), do: []
def ip_list([h|t]) do
[Enum.at(h,0) | ip_list(t)]
end
end
IPCluster.get_inet() |>
IPCluster.get_ip_list() |>
IPCluster.ip_list()
@wesleyit
Copy link
Author

a = 192
b = 168
c = 0..255
d = 1..254

ips = for x <- c, y <- d, do: [a, b, x, y]
Enum.slice(ips, (length(ips) - 10), 10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment