Module: ScanDB::Runner

Defined in:
lib/scandb/runner.rb

Class Method Summary collapse

Class Method Details

.command_line(args) ⇒ Object

The command-line runner.



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/scandb/runner.rb', line 35

def Runner.command_line(args)
  options = OpenStruct.new
  options.log = {}

  opts = OptionParser.new do |opts|
    opts.banner = 'usage: scandb [-v] [-l FILE] [-d URI] [--import-nmap FILE | -L | -p PORT | -s NAME]'

    opts.on('-l','--log FILE','The FILE to use for logging Database activity.','Defaults to ~/.scandb/database.log') do |file|
      options.log[:path] = file
    end

    opts.on('--log-level LEVEL','Specifies the log-level.','Defaults to info') do |level|
      options.log[:level] = level.to_sym
    end

    opts.on('--log-stdout','Send log messages to stdout') do
      options.log[:stream] = STDOUT
    end

    opts.on('--disable-log','Alias for --log-level off') do
      options.log[:level] = :off
    end

    opts.on('-d','--database URI','The URI for the Database.') do |uri|
      options.database = uri
    end

    opts.on('--import-nmap FILE','Import a Nmap XML scan file') do |file|
      options.import = :nmap
      options.import_file = file
    end

    opts.on('-L','--list-hosts','List all hosts within ScanDB') do
      options.list_hosts = true
    end

    opts.on('-p','--with-port PORT','List hosts with the specified open PORT') do |port|
      options.with_port = port.to_i
    end

    opts.on('--with-open-ports','List hosts with open ports') do
      options.with_port_status = :open
    end

    opts.on('--with-filtered-ports','List hosts with filtered ports') do
      options.with_port_status = :filtered
    end

    opts.on('--with-closed-ports','List hosts with closed ports') do
      options.with_port_status = :closed
    end

    opts.on('-s','--with-service NAME','List hosts with the specified service') do |name|
      options.with_service = name
    end

    opts.on('--export-yaml FILE','Exports hosts as a YAML file') do |path|
      options.export = :yaml
      options.export_path = path
    end

    opts.on('--export-xml FILE','Exports hosts as a XML file') do |path|
      options.export = :xml
      options.export_path = path
    end

    opts.on('-v','--verbose','Increase verbosity of output') do
      options.verbose = true
    end

    opts.on('-V','--version','Print ScanDB version and exit') do
      puts ScanDB::Version
      exit
    end

    opts.on('-h','--help','This cruft') do
      puts opts
      exit
    end
  end

  opts.parse!(args)

  unless options.log.empty?
    Database.setup_log(options.log)
  end

  Database.setup(options.database || Database.config)

  if options.import
    case options.import
    when :nmap then
      hosts = Nmap.import_xml(options.import_file) do |host|
        if options.verbose
          puts ">>> Imported #{host.ip}"
        end
      end

      case hosts
      when 0
        puts "No hosts where imported."
      when 1
        puts "Imported #{hosts} host."
      else
        puts "Imported #{hosts} hosts."
      end
    end
  else
    if options.with_port
      hosts = Port.all(:number => options.with_port).scanned(:status => :open).host
    elsif options.with_service
      hosts = Service.all(:name.like => "%#{options.with_service}%").scanned(:status => :open).host
    else
      hosts = Host.all
    end

    if options.export
      File.open(options.export_path,'w') do |output|
        case options.export
        when :yaml
          output.write(hosts.to_yaml)
        when :xml
          output.write(hosts.to_xml)
        end
      end
    else
      hosts.each do |host|
        if options.verbose
          print "[ #{host} ]\n\n"

          unless host.names.empty?
            puts '  Host names:'

            host.names.each do |name|
              puts "    #{name}"
            end

            print "\n"
          end

          unless host.os_class_guesses.empty?
            puts '  OS Classes:'

            host.os_class_guesses.each do |guess|
              puts "    #{guess}"
            end

            print "\n"
          end

          unless host.os_match_guesses.empty?
            puts '  OS Matches:'

            host.os_match_guesses.each do |guess|
              puts "    #{guess}"
            end

            print "\n"
          end

          unless host.scanned_ports.empty?
            puts "  Scanned Ports:"

            host.scanned_ports.each do |scanned_port|
              puts "  #{scanned_port}"
            end

            print "\n"
          end
        else
          puts host
        end
      end
    end
  end

  return true
end