如何从 IP 范围/子网中获取随机 IP?

Posted

技术标签:

【中文标题】如何从 IP 范围/子网中获取随机 IP?【英文标题】:How to get a random IP from an IP range/subnet? 【发布时间】:2021-12-15 07:19:00 【问题描述】:

假设,我的 IP 范围:“52.93.50.128/32” 如何获取该范围内的随机 IP 地址?

Ruby 会更好。

【问题讨论】:

任何 /32 “范围”都只是一个地址... 【参考方案1】:

我认为this snippet使用this gem应该可以,简单的方法是计算IP范围,然后选择一个新的

#!/usr/bin/env ruby

require 'ipaddr'

family = Socket::AF_INET

subnet_range = IPAddr.new("52.93.50.128/32", family).to_range
from = subnet_range.first(2).last
to = IPAddr.new(subnet_range.last.to_i - 2, family)

puts "From: #from"
puts "To: #to"

random_ip = IPAddr.new(rand(from.to_i..to.to_i), family)

puts "Random IP: #random_ip"

【讨论】:

出于某种原因,这总是给我 '0.0.0.0' 你读过代码吗?应该立即明白为什么它不适用于主机地址或 RFC 3021 子网。 另外,只是好奇:如果这不能回答你的问题,那么你为什么接受它回答了你的问题?拥有一个被接受的答案通常会阻止人们发布其他可能更好的答案。【参考方案2】:

假设,我的 IP 范围:“52.93.50.128/32”我怎样才能获得该范围内的随机 IP 地址?

这很容易。这是一个主机地址,换句话说,它根本不是一个范围,它只是一个地址。因此,您只需再次返回输入:

def random_ip_address_from_subnet(subnet) = subnet

但是,通过考虑主机地址和 RFC 3021 点对点链接的细节,我们可以做一些更智能的事情。

我在这里假设您只需要主机地址,而不需要网络或广播地址。

require 'ipaddr'

def random_ip_from_subnet(subnet)
  ipaddr = IPAddr.new(subnet)
  range = host_address_range(ipaddr)
  range.to_a.sample
end

def host_address_range(subnet)
  range = subnet.to_range

  return range if rfc3021?(subnet) || host?(subnet)

  Range.new(range.begin.succ, IPAddr.new(range.end.to_i - 1, subnet.family))
end

def host?(subnet) = subnet.ipv4? && subnet.prefix == 32 || subnet.ipv6? && subnet.prefix == 128
def rfc3021?(subnet) = subnet.ipv4? && subnet.prefix == 31

require 'minitest/autorun'

class TestRandomIp < Minitest::Test
  def setup
    @subnet_address1 = '52.93.50.129/30'
    @subnet_address2 = '52.93.50.130/30'
    @subnet_address_network = '52.93.50.128/30'
    @subnet_address_broadcast = '52.93.50.131/30'
    @subnet_address1_ip = IPAddr.new(@subnet_address1).succ
    @subnet_address2_ip = @subnet_address1_ip.succ
    @subnet_address_network_ip = IPAddr.new(@subnet_address_network)
    @subnet_address_broadcast_ip = @subnet_address2_ip.succ

    @host_address = '52.93.50.128/32'
    @host_address_ip = IPAddr.new(@host_address)

    @rfc3021_address1 = '52.93.50.128/31'
    @rfc3021_address2 = '52.93.50.129/31'
    @rfc3021_address1_ip = IPAddr.new(@rfc3021_address1)
    @rfc3021_address2_ip = IPAddr.new(@rfc3021_address2).succ
  end

  def test_that_host_address_range_works_with_normal_subnet_addresses
    range1 = host_address_range(@subnet_address1_ip)
    assert_equal @subnet_address1_ip, range1.begin
    assert_equal @subnet_address2_ip, range1.end
    assert_equal @subnet_address1_ip..@subnet_address2_ip, range1
    assert_equal 2, range1.count

    range2 = host_address_range(@subnet_address2_ip)
    assert_equal @subnet_address1_ip, range2.begin
    assert_equal @subnet_address2_ip, range2.end
    assert_equal @subnet_address1_ip..@subnet_address2_ip, range2
    assert_equal 2, range2.count

    range3 = host_address_range(@subnet_address_network_ip)
    assert_equal @subnet_address1_ip, range3.begin
    assert_equal @subnet_address2_ip, range3.end
    assert_equal @subnet_address1_ip..@subnet_address2_ip, range3
    assert_equal 2, range3.count

    range4 = host_address_range(@subnet_address_broadcast_ip)
    assert_equal @subnet_address1_ip, range4.begin
    assert_equal @subnet_address2_ip, range4.end
    assert_equal @subnet_address1_ip..@subnet_address2_ip, range4
    assert_equal 2, range4.count

    assert_equal range1, range2
    assert_equal range1, range3
    assert_equal range1, range4
    assert_equal range2, range3
    assert_equal range2, range4
    assert_equal range3, range4
  end

  def test_that_host_address_range_works_with_host_addresses
    range = host_address_range(@host_address_ip)
    assert_equal @host_address_ip, range.begin
    assert_equal @host_address_ip, range.end
    assert_equal @host_address_ip..@host_address_ip, range
    assert_equal 1, range.count
  end

  def test_that_host_address_range_works_with_rfc3021_addresses
    range1 = host_address_range(@rfc3021_address1_ip)
    assert_equal @rfc3021_address1_ip, range1.begin
    assert_equal @rfc3021_address2_ip, range1.end
    assert_equal @rfc3021_address1_ip..@rfc3021_address2_ip, range1
    assert_equal 2, range1.count

    range2 = host_address_range(@rfc3021_address2_ip)
    assert_equal @rfc3021_address1_ip, range2.begin
    assert_equal @rfc3021_address2_ip, range2.end
    assert_equal @rfc3021_address1_ip..@rfc3021_address2_ip, range2
    assert_equal 2, range2.count

    assert_equal range1, range2
  end

  def test_that_random_ip_from_subnet_works_with_normal_subnet_addresses
    100.times do
      random_ip = random_ip_from_subnet(@subnet_address1)
      assert [@subnet_address1_ip, @subnet_address2_ip].include?(random_ip)
    end

    100.times do
      random_ip = random_ip_from_subnet(@subnet_address2)
      assert [@subnet_address1_ip, @subnet_address2_ip].include?(random_ip)
    end

    100.times do
      random_ip = random_ip_from_subnet(@subnet_address_network)
      assert [@subnet_address1_ip, @subnet_address2_ip].include?(random_ip)
    end

    100.times do
      random_ip = random_ip_from_subnet(@subnet_address_broadcast)
      assert [@subnet_address1_ip, @subnet_address2_ip].include?(random_ip)
    end
  end

  def test_that_random_ip_from_subnet_works_with_rfc3021_addresses
    100.times do
      random_ip = random_ip_from_subnet(@rfc3021_address1)
      assert [@rfc3021_address1_ip, @rfc3021_address2_ip].include?(random_ip)
    end

    100.times do
      random_ip = random_ip_from_subnet(@rfc3021_address2)
      assert [@rfc3021_address1_ip, @rfc3021_address2_ip].include?(random_ip)
    end
  end

  def test_that_random_ip_from_subnet_works_with_host_addresses
    100.times do
      random_ip = random_ip_from_subnet(@host_address)
      assert_equal @host_address_ip, random_ip
    end
  end
end

【讨论】:

我还是会添加一个mask 作为参数,因为内部网通常不遵循他们的 RFC“类” 近 30 年没有一个类这样的东西,完全没有理由担心它们。

以上是关于如何从 IP 范围/子网中获取随机 IP?的主要内容,如果未能解决你的问题,请参考以下文章

Java - 在给定范围内打印随机IP地址

Java - 打印给定范围内的随机 IP 地址

从子网掩码计算 IP 范围

因为路由器是随机分IP的,怎么限制手机的下载速度

如何算有多少个IP地址

如何使用python获取ip和子网掩码地址[关闭]