Class: Masscan::OutputFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/masscan/output_file.rb

Overview

Represents an output file.

Example

output_file = Masscan::OutputFile.new('masscan.json')
output_file.each do |record|
  p record
end

Constant Summary collapse

PARSERS =

Mapping of formats to parsers.

API:

  • semipublic

{
  binary: Parsers::Binary,
  list:   Parsers::List,
  json:   Parsers::JSON,
  ndjson: Parsers::JSON,
  # xml:    Parsers::XML,
}
FILE_FORMATS =

Mapping of file extensions to formats

API:

  • semipublic

{
  '.bin' => :binary,
  '.dat' => :binary,

  '.txt' => :list,
  '.list' => :list,

  '.json'   => :json,
  '.ndjson' => :ndjson,

  '.xml' => :xml
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, format: self.class.infer_format(path)) ⇒ OutputFile

Initializes the output file.

Parameters:

  • The path to the output file.

  • (defaults to: self.class.infer_format(path))

    The format of the output file. Defaults to infer_format.

Raises:

  • The output format was not given and it cannot be inferred.



62
63
64
65
66
67
68
69
# File 'lib/masscan/output_file.rb', line 62

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

Instance Attribute Details

#formatSymbol (readonly)

The format of the output file.

Returns:



41
42
43
# File 'lib/masscan/output_file.rb', line 41

def format
  @format
end

#parserParsers::Binary, ... (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parser for the output file format.

Returns:

API:

  • private



48
49
50
# File 'lib/masscan/output_file.rb', line 48

def parser
  @parser
end

#pathString (readonly)

The path to the output file.

Returns:



36
37
38
# File 'lib/masscan/output_file.rb', line 36

def path
  @path
end

Class Method Details

.infer_format(path) ⇒ :binary, ...

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

Parameters:

  • The path to the output file.

Returns:

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

Raises:

  • The output format could not be inferred from the file's name.

API:

  • semipublic



101
102
103
104
105
# File 'lib/masscan/output_file.rb', line 101

def self.infer_format(path)
  FILE_FORMATS.fetch(File.extname(path)) do
    raise(ArgumentError,"could not infer format of #{path}")
  end
end

Instance Method Details

#each {|record| ... } ⇒ Enumerator

Parses the contents of the output file.

Yields:

  • (record)

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

  • (Status, Banner)

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

Returns:

  • If no block is given, an Enumerator will be returned.



119
120
121
122
123
124
125
# File 'lib/masscan/output_file.rb', line 119

def each(&block)
  return enum_for(__method__) unless block

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

#to_sString

Converts the output file to a String.

Returns:

  • The path to the output file.

Since:

  • 0.2.0



135
136
137
# File 'lib/masscan/output_file.rb', line 135

def to_s
  @path
end