class Masscan::OutputFile

Represents an output file.

Constants

PARSERS

Attributes

format[R]

The format of the output file.

@return [Symbol]

parser[R]

The parser for the output file format.

@return [Parsers::Binary, Parsers::JSON, Parsers::List]

path[R]

The path to the output file.

@return [String]

Public Class Methods

infer_format(path) click to toggle source

Infers the format from the output file's extension name.

@param [String] path

The path to the output file.

@return [:binary, :list, :json, :ndjson]

The output format inferred from the file's extension name.

@raise [ArgumentError]

The output format could not be inferred from the file's name.
# File lib/masscan/output_file.rb, line 67
def self.infer_format(path)
  case File.extname(path)
  when '.bin', '.dat'     then :binary
  when '.txt', '.list'    then :list
  when '.json'            then :json
  when '.ndjson'          then :ndjson
  when '.xml'             then :xml
  else
    raise(ArgumentError,"could not infer format of #{path}")
  end
end
new(path, format: self.class.infer_format(path)) click to toggle source

Initializes the output file.

@param [String] path

The path to the output file.

@param [:binary, :list, :json, :ndjson] format

The format of the output file. Defaults to {infer_format}.

@raise [ArgumentError]

The output format was not given and it cannot be inferred.
# File lib/masscan/output_file.rb, line 46
def initialize(path, format: self.class.infer_format(path))
  @path   = path
  @format = format

  @parser = PARSERS.fetch(format) do
    raise(ArgumentError,"unknown format: #{format.inspect}")
  end
end

Public Instance Methods

each(&block) click to toggle source

Parses the contents of the output file.

@yield [record]

If a block is given, it will be passed each parsed record.

@yield [Status, Banner] record

A parsed record, either a {Status} or a {Banner}.

@return [Enumerator]

If no block is given, an Enumerator will be returned.
# File lib/masscan/output_file.rb, line 91
def each(&block)
  return enum_for(__method__) unless block

  @parser.open(@path) do |file|
    @parser.parse(file,&block)
  end
end