Class: Ncrack::XML

Inherits:
Object
  • Object
show all
Defined in:
lib/ncrack/xml.rb,
lib/ncrack/xml/port.rb,
lib/ncrack/xml/address.rb,
lib/ncrack/xml/service.rb,
lib/ncrack/xml/credentials.rb

Overview

Represents an ncrack XML file or XML data.

Examples

require 'ncrack/xml'

Ncrack::XML.open('ncrack.xml') do |xml|
  xml.each_service do |service|
    puts "#{service.address} #{service.port.number}/#{service.port.name}:"

    service.each_credentials.each do |credentials|
      puts "  #{credentials}"
    end
  end
end

Defined Under Namespace

Classes: Address, Credentials, Port, Service

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, path: nil) {|xml| ... } ⇒ XML

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.

Creates a new XML object.

Parameters:

  • doc (Nokogiri::XML)

    The parsed XML document.

  • path (String, nil) (defaults to: nil)

    The path to the XML file.

Yields:

  • (xml)

    If a block is given, it will be passed the newly created XML parser.

Yield Parameters:

  • xml (XML)

    The newly created XML parser.



55
56
57
58
59
60
# File 'lib/ncrack/xml.rb', line 55

def initialize(doc, path: nil)
  @doc  = doc
  @path = File.expand_path(path) if path

  yield self if block_given?
end

Instance Attribute Details

#docNokogiri::XML::Node (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 parsed XML document.

Returns:

  • (Nokogiri::XML::Node)


30
31
32
# File 'lib/ncrack/xml.rb', line 30

def doc
  @doc
end

#pathString? (readonly)

The path to the XML file.

Returns:

  • (String, nil)


35
36
37
# File 'lib/ncrack/xml.rb', line 35

def path
  @path
end

Class Method Details

.open(path) {|xml| ... } ⇒ XML

Opens an parses an XML file.

Parameters:

  • path (String)

    The path to the XML file.

Yields:

  • (xml)

    If a block is given, it will be passed the newly created XML parser.

Yield Parameters:

  • xml (XML)

    The newly created XML parser.

Returns:

  • (XML)

    The parsed XML.



102
103
104
105
106
# File 'lib/ncrack/xml.rb', line 102

def self.open(path,&block)
  path = File.expand_path(path)

  new(Nokogiri::XML(File.open(path)), path: path, &block)
end

.parse(xml) {|xml| ... } ⇒ XML

Parses the given XML String.

Parameters:

  • xml (String)

    The XML String.

Yields:

  • (xml)

    If a block is given, it will be passed the newly created XML parser.

Yield Parameters:

  • xml (XML)

    The newly created XML parser.

Returns:

  • (XML)

    The parsed XML.



80
81
82
# File 'lib/ncrack/xml.rb', line 80

def self.parse(xml,&block)
  new(Nokogiri::XML(xml),&block)
end

Instance Method Details

#argsString

Additional command-line arguments passed to ncrack.

Returns:

  • (String)

    The value of the args attribute.



124
125
126
# File 'lib/ncrack/xml.rb', line 124

def args
  @args ||= @doc.root['args']
end

#debuggingInteger

The debugging level.

Returns:

  • (Integer)

    The parsed value of the level attribute of the debugging child element.



176
177
178
# File 'lib/ncrack/xml.rb', line 176

def debugging
  @debugging ||= @doc.at_xpath('/ncrackrun/debugging')['level'].to_i
end

#each_service {|service| ... } ⇒ Enumerator

Enumerates over every service.

Yields:

  • (service)

    If a block is given, it will be passed every service object.

Yield Parameters:

  • service (Service)

    A service object.

Returns:

  • (Enumerator)

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



192
193
194
195
196
197
198
# File 'lib/ncrack/xml.rb', line 192

def each_service
  return enum_for(__method__) unless block_given?

  @doc.root.xpath('/ncrackrun/service').each do |node|
    yield Service.new(node)
  end
end

#scannerString

The scanner that produced the XML (aka ncrack).

Returns:

  • (String)

    The value of the scanner attribute.



114
115
116
# File 'lib/ncrack/xml.rb', line 114

def scanner
  @scanner ||= @doc.root['scanner']
end

#serviceService, nik

The first service object.

Returns:



214
215
216
# File 'lib/ncrack/xml.rb', line 214

def service
  each_service.first
end

#servicesArray<Service>

All service object.

Returns:



205
206
207
# File 'lib/ncrack/xml.rb', line 205

def services
  each_service.to_a
end

#startTime

The start time.

Returns:

  • (Time)

    The parsed value of the start attribute.



134
135
136
# File 'lib/ncrack/xml.rb', line 134

def start
  @start ||= Time.at(@doc.root['start'].to_i)
end

#to_sString

Converts the XML to a String.

Returns:

  • (String)

    The path to the XML if #path is set, or the XML if the XML was parsed from a String.



225
226
227
228
229
230
231
# File 'lib/ncrack/xml.rb', line 225

def to_s
  if @path
    @path
  else
    @doc.to_s
  end
end

#verboseInteger

The verbosity level.

Returns:

  • (Integer)

    The parsed value of the level attribute of the verbose child element.



165
166
167
# File 'lib/ncrack/xml.rb', line 165

def verbose
  @verbose ||= @doc.at_xpath('/ncrackrun/verbose')['level'].to_i
end

#versionString

The version of ncrack.

Returns:

  • (String)

    The value of the version attribute.



144
145
146
# File 'lib/ncrack/xml.rb', line 144

def version
  @version ||= @doc.root['version']
end

#xml_output_versionString

The version of the ncrack XML schema.

Returns:

  • (String)

    The value of the xmloutputversion attribute.



154
155
156
# File 'lib/ncrack/xml.rb', line 154

def xml_output_version
  @xml_output_version ||= @doc.root['xmloutputversion']
end