Sha256: 4861390658360ddd48b475eadb5420ab94b01b4083acfd738de471d93fd3e711

Contents?: true

Size: 1.45 KB

Versions: 75

Compression:

Stored size: 1.45 KB

Contents

# -*- coding: binary -*-
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

75 entries across 75 versions & 4 rubygems

Version Path
rex-socket-0.1.59 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.58 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.57 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.56 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.55 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.54 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.53 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.52 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.51 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.50 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.49 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.48 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.47 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.46 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.45 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.44 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.43 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.42 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.41 lib/rex/socket/subnet_walker.rb
rex-socket-0.1.40 lib/rex/socket/subnet_walker.rb