Find out if an IP is within a range of IPs

前端 未结 7 951
萌比男神i
萌比男神i 2021-01-30 09:18

How can you tell if an ip, say 62.156.244.13 is within the range of 62.0.0.0 and 62.255.255.255

7条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 09:31

    jdl's answer is good. I would make the in_range? function a one-liner:

    def ip_addr_in_range?(low, high, addr)
      (numeric_ip(low) .. numeric_ip(high)) === numeric_ip(addr)
    end
    

提交回复
热议问题