在 Ruby 中生成随机 IP 地址的最短方法是什么?

发布于 2024-09-01 13:07:49 字数 21 浏览 8 评论 0原文

正如标题所说,出于测试目的。

Just like the title says, wanted for testing purposes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

等风来 2024-09-08 13:07:49

您可以使用 IPAddr

require 'ipaddr'
ipv4 = IPAddr.new(rand(2**32),Socket::AF_INET)
ipv6 = IPAddr.new(rand(2**128),Socket::AF_INET6)

You could use IPAddr

require 'ipaddr'
ipv4 = IPAddr.new(rand(2**32),Socket::AF_INET)
ipv6 = IPAddr.new(rand(2**128),Socket::AF_INET6)
为你拒绝所有暧昧 2024-09-08 13:07:49

如果您想要一个真正随机的 IP 地址,Array.new(4){rand(256)}.join('.') 即可实现

If you want a truly random IP address, Array.new(4){rand(256)}.join('.') does it

两个我 2024-09-08 13:07:49

我建议使用 Faker

https://github.com/stympy/faker#fakerinternet

Faker::Internet.ip_v4_address #=> "24.29.18.175"

I would suggest using Faker

https://github.com/stympy/faker#fakerinternet

Faker::Internet.ip_v4_address #=> "24.29.18.175"
逆流 2024-09-08 13:07:49
ip = "%d.%d.%d.%d" % [rand(256), rand(256), rand(256), rand(256)]
ip = "%d.%d.%d.%d" % [rand(256), rand(256), rand(256), rand(256)]
梦明 2024-09-08 13:07:49

我之前用过这个来生成随机IP,然后用Resolv验证它

  ip = "#{rand(99)}.#{rand(100)}.#{rand(10)}.#{rand(255)}"
  begin
    if ip
      host = Resolv.new.getname(ip) 
      puts "#{c} #{real_ip.length} #{ip} #{host}" 
    end
  rescue Exception => e
    puts "FAKE #{ip}"
  end

I've used this before to generate a random ip then validate it with Resolv

  ip = "#{rand(99)}.#{rand(100)}.#{rand(10)}.#{rand(255)}"
  begin
    if ip
      host = Resolv.new.getname(ip) 
      puts "#{c} #{real_ip.length} #{ip} #{host}" 
    end
  rescue Exception => e
    puts "FAKE #{ip}"
  end
巷子口的你 2024-09-08 13:07:49

我打算在 Lass 的评论中添加一些代码,以获得任何安全的RFC1918 地址,但声誉还不够。

因此,如果我们采用以下数学结果:

10*2**24 = 167772160
172*2**24 + 16*2**16 = 2886729728
192*2**24 + 168*2**16 = 3232235520

并将它们填充到一个数组中,该数组填充到 IPAddr.new 方法中

IPAddr.new([167772160, 2886729728, 3232235520].sample + rand(2**16), Socket::AF_INET)

大多数 RF1918 空间都会生成。不幸的是,这总是会产生 10.0.XX 地址,但如果有人正在寻找某种看起来不太糟糕的单行格式的地址变化,那么这应该适合测试。

与 Faker 相比,此类方法或 Lass 方法的一个主要好处是,如果代码出于某种原因尝试访问随机地址,则可以降低意外占用某人互联网资源的风险。

I was going to tack on to Lass's comment with some code to get any safe RFC1918 address, but not enough reputation yet.

So, if we take the following math results:

10*2**24 = 167772160
172*2**24 + 16*2**16 = 2886729728
192*2**24 + 168*2**16 = 3232235520

and, stuff them into an array which is stuffed in the IPAddr.new method

IPAddr.new([167772160, 2886729728, 3232235520].sample + rand(2**16), Socket::AF_INET)

Most any RF1918 space is generated. Unfortunately, this will always produce 10.0.X.X addresses, but that should be fine for testing if one was looking for some address variety in a not too terrible looking one-liner format.

A major benefit of something like this or Lass's method over Faker is there is reduced risk for accidental taxing of someone's Internet resources if the code attempts to reach out to the random address for some reason.

会发光的星星闪亮亮i 2024-09-08 13:07:49

要在合理的网络范围(例如 10.0.0.0/24 或 2001:db8::/64)内生成有效 IP 地址,请执行以下操作:

require 'ipaddr'
ipv4 = IPAddr.new("10.0.0.0/24", Socket::AF_INET) | IPAddr.new(rand(2**(32-24)),Socket::AF_INET)
ipv6 = IPAddr.new("2001:db8:cafe:babe::/64", Socket::AF_INET6) | IPAddr.new(rand(2**(128-64)),Socket::AF_INET6)

这可以推广到任何子网:

subnet = "192.168.100.0"
prefix = 24
ipv4 = IPAddr.new("#{subnet}/#{prefix}", Socket::AF_INET) | IPAddr.new(rand(2**(32-prefix)),Socket::AF_INET)

To generate valid IP addresses within a reasonable network range (e.g. 10.0.0.0/24 or 2001:db8::/64), do this:

require 'ipaddr'
ipv4 = IPAddr.new("10.0.0.0/24", Socket::AF_INET) | IPAddr.new(rand(2**(32-24)),Socket::AF_INET)
ipv6 = IPAddr.new("2001:db8:cafe:babe::/64", Socket::AF_INET6) | IPAddr.new(rand(2**(128-64)),Socket::AF_INET6)

This can be generalized to any subnet:

subnet = "192.168.100.0"
prefix = 24
ipv4 = IPAddr.new("#{subnet}/#{prefix}", Socket::AF_INET) | IPAddr.new(rand(2**(32-prefix)),Socket::AF_INET)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文