2006-08-12 08:31:38 +00:00
|
|
|
require 'rex/socket'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Socket
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class provides an interface to enumerating an IP range
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class RangeWalker
|
|
|
|
|
|
|
|
#
|
|
|
|
# Initializes a walker instance using the supplied range
|
|
|
|
#
|
2006-08-13 00:08:40 +00:00
|
|
|
def initialize(ranges)
|
2006-08-12 08:31:38 +00:00
|
|
|
|
2006-08-13 00:08:40 +00:00
|
|
|
self.ranges = []
|
2006-08-12 08:31:38 +00:00
|
|
|
|
2006-08-13 00:08:40 +00:00
|
|
|
ranges.split(',').each do |range|
|
2008-09-26 05:39:06 +00:00
|
|
|
tmp = range.split('-')
|
|
|
|
tmp[1] ||= tmp[0]
|
|
|
|
|
|
|
|
if(tmp[0] == tmp[1] and tmp[0] =~ /\//)
|
|
|
|
tmp = Rex::Socket.cidr_crack(tmp[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
addr_a, addr_b = tmp
|
|
|
|
addr_a, scope = tmp[0].split("%")
|
|
|
|
addr_b, scope = tmp[1].split("%") if not scope
|
2006-08-13 00:08:40 +00:00
|
|
|
|
2008-09-26 05:39:06 +00:00
|
|
|
addr_a = Rex::Socket.addr_atoi(addr_a)
|
|
|
|
addr_b = Rex::Socket.addr_atoi(addr_b)
|
2006-08-13 00:08:40 +00:00
|
|
|
|
2008-09-26 05:39:06 +00:00
|
|
|
if (addr_b < addr_a)
|
|
|
|
addr_t = addr_a
|
|
|
|
addr_a = addr_b
|
|
|
|
addr_b = addr_t
|
2006-08-13 00:08:40 +00:00
|
|
|
end
|
2008-09-26 05:39:06 +00:00
|
|
|
|
|
|
|
self.ranges << [addr_a,addr_b,scope]
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
reset
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Resets the subnet walker back to its original state.
|
|
|
|
#
|
|
|
|
def reset
|
2006-08-13 00:08:40 +00:00
|
|
|
self.curr_range = 0
|
|
|
|
self.curr_ip = self.ranges[0][0]
|
2008-08-23 07:05:38 +00:00
|
|
|
self.curr_scope = self.ranges[0][2]
|
2006-08-13 00:08:40 +00:00
|
|
|
self.num_ips = 0
|
|
|
|
self.ranges.each {|r| self.num_ips += r[1]-r[0] + 1 }
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the next IP address.
|
|
|
|
#
|
|
|
|
def next_ip
|
2006-08-13 00:08:40 +00:00
|
|
|
if (self.curr_ip > self.ranges[self.curr_range][1])
|
2008-09-26 05:39:06 +00:00
|
|
|
|
2006-08-13 00:08:40 +00:00
|
|
|
if (self.curr_range == self.ranges.length - 1)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
self.curr_range += 1
|
|
|
|
self.curr_ip = self.ranges[self.curr_range][0]
|
2008-09-26 05:39:06 +00:00
|
|
|
self.curr_scope = self.ranges[self.curr_range][2]
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
addr = Rex::Socket.addr_itoa(self.curr_ip)
|
|
|
|
self.curr_ip += 1
|
2008-08-23 07:05:38 +00:00
|
|
|
|
2009-03-08 07:55:47 +00:00
|
|
|
addr << "%#{self.curr_scope}" if self.curr_scope
|
2008-09-26 05:39:06 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
return addr
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# The total number of IPs within the range
|
|
|
|
#
|
|
|
|
attr_reader :num_ips
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
attr_writer :num_ips # :nodoc:
|
2008-08-23 07:05:38 +00:00
|
|
|
attr_accessor :addr_start, :addr_stop, :curr_ip, :curr_range, :ranges, :curr_scope # :nodoc:
|
2006-08-12 08:31:38 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-03-08 07:55:47 +00:00
|
|
|
end
|