Skip to content

Instantly share code, notes, and snippets.

@BuffaloWill
Last active June 21, 2020 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BuffaloWill/2e266c416f5ac0fd53fe427c03ed3835 to your computer and use it in GitHub Desktop.
Save BuffaloWill/2e266c416f5ac0fd53fe427c03ed3835 to your computer and use it in GitHub Desktop.
Generic IP List Generator
irb --simple-prompt --noecho
require 'ipaddr'
# RFC 1918
# 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
#IPAddr.new("10.0.0.0/8").to_range.to_a.each{ |ip| puts ip }
#IPAddr.new("172.16.0.0/12").to_range.to_a.each{ |ip| puts ip }
#IPAddr.new("192.168.0.0/16").to_range.to_a.each{ |ip| puts ip }
# prints up to NUM ips from the range
def random(num,range)
0.upto(num) do |n|
r = rand(0..range.size()-1)
puts range[r]
end
end
# example print to only print common gateway ips from a range
IPAddr.new("192.168.0.0/16").to_range.to_a.each do |ip|
partial = ip.to_s.split(".")
if partial[3] == "1"
puts ip
end
end
# example print to only print common gateway ips from a range
IPAddr.new("10.0.0.0/8").to_range.to_a.each do |ip|
partial = ip.to_s.split(".")
if partial[3] == "1"
puts ip
end
end
# example print to only print common gateway ips from a range
IPAddr.new("172.16.0.0/12").to_range.to_a.each do |ip|
partial = ip.to_s.split(".")
if partial[3] == "1"
puts ip
end
end
# example print to 300 random ips from a range
random(300, IPAddr.new("192.168.0.0/16").to_range.to_a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment