Module: Msf::Ui::Console::CommandDispatcher::Db::Common

Included in:
Msf::Ui::Console::CommandDispatcher::Db
Defined in:
lib/msf/ui/console/command_dispatcher/db/common.rb

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns true if the db is connected, prints an error and returns false if not.

All commands that require an active database should call this before doing anything.

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/msf/ui/console/command_dispatcher/db/common.rb', line 12

def active?
  unless framework.db.active
    print_error("Database not connected")
    return false
  end
  true
end

#each_host_range_chunk(host_ranges, &block) ⇒ Object

Takes host_ranges, an Array of RangeWalkers, and chunks it up into blocks of 1024.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/msf/ui/console/command_dispatcher/db/common.rb', line 28

def each_host_range_chunk(host_ranges, &block)
  # Chunk it up and do the query in batches. The naive implementation
  # uses so much memory for a /8 that it's basically unusable (1.6
  # billion IP addresses take a rather long time to allocate).
  # Chunking has roughly the same performance for small batches, so
  # don't worry about it too much.
  host_ranges.each do |range|
    if range.nil? or range.length.nil?
      chunk = nil
      end_of_range = true
    else
      chunk = []
      end_of_range = false
      # Set up this chunk of hosts to search for
      while chunk.length < 1024 and chunk.length < range.length
        n = range.next_ip
        if n.nil?
          end_of_range = true
          break
        end
        chunk << n
      end
    end

    # The block will do some
    yield chunk

    # Restart the loop with the same RangeWalker if we didn't get
    # to the end of it in this chunk.
    redo unless end_of_range
  end
end