true >> low = I-6ren">
gpt4 book ai didi

ruby - 查明一个IP是否在IP范围内

转载 作者:数据小太阳 更新时间:2023-10-29 06:24:17 24 4
gpt4 key购买 nike

你怎么知道一个ip,比如说62.156.244.1362.0.0.0 范围内和 62.255.255.255

最佳答案

>> require "ipaddr"
=> true
>> low = IPAddr.new("62.0.0.0").to_i
=> 1040187392
>> high = IPAddr.new("62.255.255.255").to_i
=> 1056964607
>> ip = IPAddr.new("62.156.244.13").to_i
=> 1050473485
>> (low..high)===ip
=> true

如果给你的是网络而不是起始地址和结束地址,那就更简单了

>> net = IPAddr.new("62.0.0.0/8")
=> #<IPAddr: IPv4:62.0.0.0/255.0.0.0>
>> net===IPAddr.new("62.156.244.13")
=> true

IPAddr 也适用于 IPv6 地址

>> low = IPAddr.new('1::')
=> #<IPAddr: IPv6:0001:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
>> high = IPAddr.new('2::')
=> #<IPAddr: IPv6:0002:0000:0000:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
>> (low..high)===IPAddr.new('1::1')
=> true

关于ruby - 查明一个IP是否在IP范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3518365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com