Sha256: b94390f2f424a8240618d65731da406456ef533c672dd436a925f074d8f45726

Contents?: true

Size: 1.36 KB

Versions: 43

Compression:

Stored size: 1.36 KB

Contents

require 'rex/socket'

module Rex
module Socket

###
#
# This class provides an interface to enumerating a subnet with a supplied
# netmask.
#
###
class SubnetWalker

	#
	# Initializes a subnet walker instance using the supplied subnet
	# information.
	#
	def initialize(subnet, netmask)
		self.subnet  = Socket.resolv_to_dotted(subnet)
		self.netmask = Socket.resolv_to_dotted(netmask)

		reset
	end

	#
	# Resets the subnet walker back to its original state.
	#
	def reset
		self.curr_ip     = self.subnet.split('.')
		self.num_ips     = (1 << (32 - Socket.net2bitmask(self.netmask).to_i))
		self.curr_ip_idx = 0
	end

	#
	# Returns the next IP address.
	#
	def next_ip
		if (curr_ip_idx >= num_ips)
			return nil
		end

		if (curr_ip_idx > 0)
			self.curr_ip[3] = (curr_ip[3].to_i + 1) % 256
			self.curr_ip[2] = (curr_ip[2].to_i + 1) % 256 if (curr_ip[3] == 0)
			self.curr_ip[1] = (curr_ip[1].to_i + 1) % 256 if (curr_ip[2] == 0)
			self.curr_ip[0] = (curr_ip[0].to_i + 1) % 256 if (curr_ip[1] == 0)
		end

		self.curr_ip_idx += 1
	
		self.curr_ip.join('.')
	end

	#
	# The subnet that is being enumerated.
	#
	attr_reader :subnet
	#
	# The netmask of the subnet.
	#
	attr_reader :netmask
	#
	# The total number of IPs within the subnet.
	#
	attr_reader :num_ips

protected

	attr_writer   :subnet, :netmask, :num_ips # :nodoc:
	attr_accessor :curr_ip, :curr_ip_idx # :nodoc:

end

end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
librex-0.0.35 lib/rex/socket/subnet_walker.rb
librex-0.0.34 lib/rex/socket/subnet_walker.rb
librex-0.0.33 lib/rex/socket/subnet_walker.rb
librex-0.0.32 lib/rex/socket/subnet_walker.rb
librex-0.0.31 lib/rex/socket/subnet_walker.rb
librex-0.0.30 lib/rex/socket/subnet_walker.rb
librex-0.0.29 lib/rex/socket/subnet_walker.rb
librex-0.0.28 lib/rex/socket/subnet_walker.rb
librex-0.0.27 lib/rex/socket/subnet_walker.rb
librex-0.0.26 lib/rex/socket/subnet_walker.rb
librex-0.0.25 lib/rex/socket/subnet_walker.rb
librex-0.0.23 lib/rex/socket/subnet_walker.rb
librex-0.0.21 lib/rex/socket/subnet_walker.rb
librex-0.0.19 lib/rex/socket/subnet_walker.rb
librex-0.0.17 lib/rex/socket/subnet_walker.rb
librex-0.0.13 lib/rex/socket/subnet_walker.rb
librex-0.0.12 lib/rex/socket/subnet_walker.rb
librex-0.0.7 lib/rex/socket/subnet_walker.rb
librex-0.0.6 lib/rex/socket/subnet_walker.rb
librex-0.0.5 lib/rex/socket/subnet_walker.rb