Skip to content

Instantly share code, notes, and snippets.

@timogoebel
Created August 3, 2017 11:49
Show Gist options
  • Save timogoebel/cf1c4f57f73b9d52d1857eb0847082bc to your computer and use it in GitHub Desktop.
Save timogoebel/cf1c4f57f73b9d52d1857eb0847082bc to your computer and use it in GitHub Desktop.
Generate a random IP in ruby
#!/usr/bin/env ruby
require 'ipaddr'
family = Socket::AF_INET
subnet_range = IPAddr.new("8.0.0.0/8", 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}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment