Module: Ronin::Nmap::Converter

Defined in:
lib/ronin/nmap/converter.rb

Overview

Handles converting nmap XML into other formats.

Supports the following formats:

  • JSON
  • CSV

Constant Summary collapse

FILE_FORMATS =

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

Mapping of file extension names to formats.

{
  '.json' => :json,
  '.csv'  => :csv
}

Class Method Summary collapse

Class Method Details

.convert(xml, output = nil, format:) ⇒ String

Converts parsed nmap XML into the desired format.

Parameters:

  • xml (::Nmap::XML)

    The nmap XML to convert.

  • output (IO, nil) (defaults to: nil)

    Optional output to write the converted output to.

  • format (:json, :csv)

    The desired convert to convert the parsed nmap XML to.

Returns:

  • (String)

    The converted nmap XML.



83
84
85
86
87
88
89
90
91
# File 'lib/ronin/nmap/converter.rb', line 83

def self.convert(xml,output=nil, format: )
  if output
    Converters[format].convert(xml,output)
  else
    output = StringIO.new
    convert(xml,output, format: format)
    output.string
  end
end

.convert_file(src, dest, format: infer_format_for(dest)) ⇒ Object

Converts an nmap XML scan file into another format.

Parameters:

  • src (String)

    The input XML file path.

  • dest (String)

    The output file path.



57
58
59
60
61
62
63
64
# File 'lib/ronin/nmap/converter.rb', line 57

def self.convert_file(src,dest, format: infer_format_for(dest))
  xml       = ::Nmap::XML.open(src)
  converter = Converters[format]

  File.open(dest,'w') do |output|
    converter.convert(xml,output)
  end
end

.infer_format_for(path) ⇒ :json, :csv

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.

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

Parameters:

  • path (String)

    The output file name.

Returns:

  • (:json, :csv)

    The conversion format.

Raises:

  • (ArgumentError)

    The format could not be inferred from the path's file extension.



107
108
109
110
111
# File 'lib/ronin/nmap/converter.rb', line 107

def self.infer_format_for(path)
  FILE_FORMATS.fetch(File.extname(path)) do
    raise(ArgumentError,"cannot infer output format from path: #{path.inspect}")
  end
end