CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/ui/console/command_dispatcher/db/common.rb
Views: 11789
1
# -*- coding: binary -*-
2
3
module Msf::Ui::Console::CommandDispatcher::Db::Common
4
5
#
6
# Returns true if the db is connected, prints an error and returns
7
# false if not.
8
#
9
# All commands that require an active database should call this before
10
# doing anything.
11
#
12
def active?
13
unless framework.db.active
14
print_error("Database not connected")
15
return false
16
end
17
true
18
end
19
20
#
21
# Miscellaneous option helpers
22
#
23
24
#
25
# Takes +host_ranges+, an Array of RangeWalkers, and chunks it up into
26
# blocks of 1024.
27
#
28
def each_host_range_chunk(host_ranges, &block)
29
# Chunk it up and do the query in batches. The naive implementation
30
# uses so much memory for a /8 that it's basically unusable (1.6
31
# billion IP addresses take a rather long time to allocate).
32
# Chunking has roughly the same performance for small batches, so
33
# don't worry about it too much.
34
host_ranges.each do |range|
35
if range.nil? or range.length.nil?
36
chunk = nil
37
end_of_range = true
38
else
39
chunk = []
40
end_of_range = false
41
# Set up this chunk of hosts to search for
42
while chunk.length < 1024 and chunk.length < range.length
43
n = range.next_ip
44
if n.nil?
45
end_of_range = true
46
break
47
end
48
chunk << n
49
end
50
end
51
52
# The block will do some
53
yield chunk
54
55
# Restart the loop with the same RangeWalker if we didn't get
56
# to the end of it in this chunk.
57
redo unless end_of_range
58
end
59
end
60
end
61
62