class Ncrack::XML
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
Attributes
The path to the XML
file.
@return [String, nil]
Public Class Methods
Creates a new XML
object.
@param [Nokogiri::XML] doc
The parsed XML document.
@param [String, nil] path
The path to the XML file.
@yield [xml]
If a block is given, it will be passed the newly created XML parser.
@yieldparam [XML] xml
The newly created XML parser.
@api private
# 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
Opens an parses an XML
file.
@param [String] path
The path to the XML file.
@yield [xml]
If a block is given, it will be passed the newly created XML parser.
@yieldparam [XML] xml
The newly created XML parser.
@return [XML]
The parsed XML.
@api public
# 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
Parses the given XML
String.
@param [String] xml
The XML String.
@yield [xml]
If a block is given, it will be passed the newly created XML parser.
@yieldparam [XML] xml
The newly created XML parser.
@return [XML]
The parsed XML.
@api public
# File lib/ncrack/xml.rb, line 80 def self.parse(xml,&block) new(Nokogiri::XML(xml),&block) end
Public Instance Methods
Additional command-line arguments passed to ‘ncrack`.
@return [String]
The value of the `args` attribute.
# File lib/ncrack/xml.rb, line 124 def args @args ||= @doc.root['args'] end
The debugging level.
@return [Integer]
The parsed value of the `level` attribute of the `debugging` child element.
# File lib/ncrack/xml.rb, line 176 def debugging @debugging ||= @doc.at_xpath('/ncrackrun/debugging')['level'].to_i end
Enumerates over every service.
@yield [service]
If a block is given, it will be passed every service object.
@yieldparam [Service] service
A service object.
@return [Enumerator]
If no block is given, an Enumerator object will be returned.
# 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
The scanner that produced the XML
(aka ‘ncrack`).
@return [String]
The value of the `scanner` attribute.
# File lib/ncrack/xml.rb, line 114 def scanner @scanner ||= @doc.root['scanner'] end
The first service object.
@return [Service, nik]
# File lib/ncrack/xml.rb, line 214 def service each_service.first end
All service object.
@return [Array<Service>]
# File lib/ncrack/xml.rb, line 205 def services each_service.to_a end
The start time.
@return [Time]
The parsed value of the `start` attribute.
# File lib/ncrack/xml.rb, line 134 def start @start ||= Time.at(@doc.root['start'].to_i) end
Converts the XML
to a String.
@return [String]
The path to the XML if {#path} is set, or the XML if the XML was parsed from a String.
# File lib/ncrack/xml.rb, line 225 def to_s if @path @path else @doc.to_s end end
The verbosity level.
@return [Integer]
The parsed value of the `level` attribute of the `verbose` child element.
# File lib/ncrack/xml.rb, line 165 def verbose @verbose ||= @doc.at_xpath('/ncrackrun/verbose')['level'].to_i end
The version of ‘ncrack`.
@return [String]
The value of the `version` attribute.
# File lib/ncrack/xml.rb, line 144 def version @version ||= @doc.root['version'] end
The version of the ‘ncrack` XML
schema.
@return [String]
The value of the `xmloutputversion` attribute.
# File lib/ncrack/xml.rb, line 154 def xml_output_version @xml_output_version ||= @doc.root['xmloutputversion'] end