Sha256: e1053b82b5c736d6c9388e7c075be29723c5a3525ae8d4931242f7d8822fb760

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module Hooray
  #
  # Main runner
  #
  class Seek
    attr_accessor :network, :ports, :opts

    NET_MASK = 24
    TIMEOUT  = 1

    def initialize(network = nil, *params)
      @scan = {}
      @bots = []
      config_network network
      config_filters params
    end

    def config_network(param)
      if param && !param.empty?
        @network = IPAddr.new(param)
      else
        @network = Hooray::Local.mask
      end
    end

    def config_filters(params)
      return if params.empty?
      numbers, @words = params.flatten.partition { |s| s =~ /\d+/ }
      @ports = numbers.map(&:to_i)
    end
    #
    # Map results to @nodes << Node.new()
    #
    # BUG: sometimes ping returns true
    # When you run list consecutively. Pretty weird!
    # So we need to remove those without mac
    def nodes
      return @nodes if @nodes
      @nodes = sweep.map do |k, v|
        Node.new(ip: k, mac: Hooray::Local.arp_table[k.to_s], ports: v)
      end.reject { |n| n.mac.nil? } # remove those without mac
    end
    alias_method :devices, :nodes

    def ping_class
      return Net::Ping::External unless ports
      if @words && @words.join =~ /tcp|udp|http|wmi/
        Net::Ping.const_get(@words.join.upcase)
      else
        Net::Ping::TCP
        # elsif (serv = Settings.service(@words.join))
        # @ports = serv.values
      end
    end

    def scan_bot(ip)
      (ports || [nil]).each do |port|
        @bots << Thread.new do
          if ping_class.new(ip.to_s, port, TIMEOUT).ping?
            @scan[ip] << port
            print '.'
          end
        end
      end
    end

    #
    # fast -> -sn -PA
    #
    def sweep
      network.to_range.each do |ip|
        @scan[ip] = []
        scan_bot(ip)
      end
      @bots.each(&:join)
      puts
      @scan.reject! { |_k, v| v.empty? }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hooray-0.1.5 lib/hooray/seek.rb