The script asks the user for two IP
addresses . One is the start of the IP range, and the second is the
end of it. Next, a new IP object is created using the defined IP
class called i . The final step before generating the IPs is to
initialize the file the IP addresses will be written to, named ofile
. Now the fun begins.
For each item returned, the results
will be output to ofile. Using the IP class method succ!, an until
loop calls the succ! method until i equals end_ip . Once the two
values are equal, that means the ending IP address has been generated
and the output file is closed.
The script relies on a custom class
called IP, which has four methods: initialize, to_s, succ, and succ!.
The IP class is important because, once an object is created, the IP
address is stored as a class variable for easy tracking. The first
method called, when i is declared, is initialize. This sets @ip to
start_ip . Next, succ! is called to begin creating the range of IPs.
succ! calls succ and utilizes the replace method to overwrite the
contents in @ip whenever succ returns a value . The meat of the IP
class is located in the method succ . If @ip ever increments to the
highest IP address, the script will return 255.255.255.255. IP
addresses can only go up to that value.
Next, the IP address, stored in @ip, is
split apart in reverse order, using the period as a delimiter. The
values are stored in an array called parts. After the IP address is
properly separated, a new code block is called on the array using the
each_with_index method to access two pieces of informationâ??the
index being passed and the value . Within this block, the value in
part is compared against 255, again to prohibit invalid IP addresses.
If the value is equal to 255, then it is reset to zero . The one
exception to the zero reset is if the value of i is equal to 3, since
that is the first octet of the IP. If part is less than 255, the
method succ! is called and the if/else statement breaks.
After each part has been run through
the code block, the IP address is put back together opposite of how
it was taken apart. The script puts each piece back together using
the join method, with periods in between the elements, all in reverse
order . As mentioned previously, the succ! method is called until the
end_ip address is equal to the results of succ!. That's all there is
to perfectly generating an IP address range.
class IP def initialize(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