Created
December 14, 2012 03:57
-
-
Save JGallardo/4282602 to your computer and use it in GitHub Desktop.
Ruby script to generate a range of IP Addresses.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IP | |
def intitialize(ip) | |
@ip = ip | |
end | |
def to_s | |
@ip | |
end | |
def==(other) | |
to_s==other.to_s | |
end | |
def succ | |
return @ip if @ip == "255.255.255.255" | |
parts = @ip.split('.').reverse | |
parts.each_with_index do |part,i| | |
if part.to_i < 255 | |
part.succ! | |
break | |
elsif part == "255" | |
part.replace("0") unless i == 3 | |
else | |
raise ArgumentError, "Invalid number #{part} in IP address" | |
end | |
end | |
parts.reverse.join('.') | |
end | |
def succ! | |
@ip.replace(succ) | |
end | |
end | |
print "Input Starting IP Address: " | |
start_ip = gets.strip | |
print "Input Ending IP Address: " | |
end_ip = gets.strip | |
i = IP.new(start_ip) | |
ofile = File.open("ips.txt", "W") | |
ofile.puts i.succ! until i == end_ip | |
ofile.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment