79160753

Date: 2024-11-05 21:40:07
Score: 0.5
Natty:
Report link

In addition to what other people suggest, net_address also offers a simple and lightweight way to work with IPs and ranges.

In the OP's case, you can do something like:

iex(9)> import IP
IP
iex(10)> ip_ranges = [~i"49.14.0.0..49.14.63.255", ~i"106.76.96.0..106.76.127.255"]
[~i"49.14.0.0..49.14.63.255", ~i"106.76.96.0..106.76.127.255"]
iex(11)> {:ok, ip} = from_string("49.14.1.2")
{:ok, {49, 14, 1, 2}}
iex(12)> ip
{49, 14, 1, 2}
iex(13)> Enum.any?(ip_ranges, fn ip_range -> ip in ip_range end)
true
iex(14)> {:ok, ip} = from_string("49.14.64.1")
{:ok, {49, 14, 64, 1}}
iex(15)> Enum.any?(ip_ranges, fn ip_range -> ip in ip_range end)
false

For more info, check out this documentation.

Reasons:
  • Blacklisted phrase (1): this document
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Spacez