Class: Nmap::XML
Overview
Represents an Nmap XML file.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Path of the Nmap XML scan file.
Class Method Summary collapse
-
.load(text, &block) ⇒ Object
deprecated
Deprecated.
Use XML.parse instead.
-
.open(path) {|xml| ... } ⇒ Object
Creates a new XML object from the file.
-
.parse(text) {|xml| ... } ⇒ Object
Creates a new XML object from XML text.
Instance Method Summary collapse
-
#debugging ⇒ Integer
Parses the debugging level.
-
#down_host ⇒ Host
Returns the first host found to be down during the scan.
-
#down_hosts ⇒ Array<Host>
Parses the hosts found to be down during the scan.
-
#each(&block) ⇒ Object
Parses the hosts that were found to be up during the scan.
-
#each_down_host {|host| ... } ⇒ XML, Enumerator
Parses the hosts that were found to be down during the scan.
-
#each_host {|host| ... } ⇒ XML, Enumerator
Parses the hosts in the scan.
-
#each_run_stat {|run_stat| ... } ⇒ Enumerator
Parses the essential runstats information.
-
#each_task {|task| ... } ⇒ Enumerator
Parses the tasks of the scan.
-
#each_up_host {|host| ... } ⇒ XML, Enumerator
Parses the hosts that were found to be up during the scan.
-
#host ⇒ Host
Returns the first host.
-
#hosts ⇒ Array<Host>
Parses the hosts in the scan.
-
#initialize(document) {|xml| ... } ⇒ XML
constructor
Creates a new XML object.
-
#inspect ⇒ String
Inspects the XML file.
-
#postscript ⇒ Postscript
(also: #postscripts)
The NSE scripts ran after the scan.
-
#prescript ⇒ Prescript
(also: #prescripts)
The NSE scripts ran before the scan.
-
#run_stats ⇒ Array<RunStat>
Parses the essential runstats information.
-
#scan_info ⇒ Array<Scan>
Parses the scan information.
-
#scanner ⇒ Scanner
Parses the scanner information.
-
#tasks ⇒ Array<ScanTask>
Parses the tasks of the scan.
-
#to_s ⇒ String
Converts the XML parser to a String.
-
#up_host ⇒ Host
Returns the first host found to be up during the scan.
-
#up_hosts ⇒ Array<Host>
Parses the hosts found to be up during the scan.
-
#verbose ⇒ Integer
Parses the verbose level.
-
#version ⇒ String
Parses the XML scan file version.
Constructor Details
#initialize(document) {|xml| ... } ⇒ XML
Creates a new XML object.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/nmap/xml.rb', line 34 def initialize(document) case document when Nokogiri::XML::Document @doc = document when IO, StringIO @doc = Nokogiri::XML(document) else @path = File.(document) @doc = File.open(@path) { |file| Nokogiri::XML(file) } end yield self if block_given? end |
Instance Attribute Details
#path ⇒ Object (readonly)
Path of the Nmap XML scan file
20 21 22 |
# File 'lib/nmap/xml.rb', line 20 def path @path end |
Class Method Details
.load(text, &block) ⇒ Object
Use parse instead.
71 72 73 |
# File 'lib/nmap/xml.rb', line 71 def self.load(text,&block) parse(text,&block) end |
.open(path) {|xml| ... } ⇒ Object
Creates a new XML object from the file.
89 90 91 |
# File 'lib/nmap/xml.rb', line 89 def self.open(path,&block) new(path,&block) end |
.parse(text) {|xml| ... } ⇒ Object
Creates a new XML object from XML text.
62 63 64 |
# File 'lib/nmap/xml.rb', line 62 def self.parse(text,&block) new(Nokogiri::XML(text),&block) end |
Instance Method Details
#debugging ⇒ Integer
Parses the debugging level.
197 198 199 |
# File 'lib/nmap/xml.rb', line 197 def debugging @debugging ||= @doc.at('debugging/@level').inner_text.to_i end |
#down_host ⇒ Host
Returns the first host found to be down during the scan.
364 365 366 |
# File 'lib/nmap/xml.rb', line 364 def down_host each_down_host.first end |
#down_hosts ⇒ Array<Host>
Parses the hosts found to be down during the scan.
353 354 355 |
# File 'lib/nmap/xml.rb', line 353 def down_hosts each_down_host.to_a end |
#each(&block) ⇒ Object
Parses the hosts that were found to be up during the scan.
417 418 419 |
# File 'lib/nmap/xml.rb', line 417 def each(&block) each_up_host(&block) end |
#each_down_host {|host| ... } ⇒ XML, Enumerator
Parses the hosts that were found to be down during the scan.
335 336 337 338 339 340 341 342 343 |
# File 'lib/nmap/xml.rb', line 335 def each_down_host return enum_for(__method__) unless block_given? @doc.xpath("/nmaprun/host[status[@state='down']]").each do |host| yield Host.new(host) end return self end |
#each_host {|host| ... } ⇒ XML, Enumerator
Parses the hosts in the scan.
289 290 291 292 293 294 295 296 297 |
# File 'lib/nmap/xml.rb', line 289 def each_host return enum_for(__method__) unless block_given? @doc.xpath('/nmaprun/host').each do |host| yield Host.new(host) end return self end |
#each_run_stat {|run_stat| ... } ⇒ Enumerator
Parses the essential runstats information.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/nmap/xml.rb', line 154 def each_run_stat return enum_for(__method__) unless block_given? @doc.xpath('/nmaprun/runstats/finished').each do |run_stat| yield RunStat.new( Time.at(run_stat['time'].to_i), run_stat['elapsed'], run_stat['summary'], run_stat['exit'] ) end return self end |
#each_task {|task| ... } ⇒ Enumerator
Parses the tasks of the scan.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/nmap/xml.rb', line 215 def each_task return enum_for(__method__) unless block_given? @doc.xpath('/nmaprun/taskbegin').each do |task_begin| task_end = task_begin.xpath('following-sibling::taskend').first yield ScanTask.new( task_begin['task'], Time.at(task_begin['time'].to_i), Time.at(task_end['time'].to_i), task_end['extrainfo'] ) end return self end |
#each_up_host {|host| ... } ⇒ XML, Enumerator
Parses the hosts that were found to be up during the scan.
381 382 383 384 385 386 387 388 389 |
# File 'lib/nmap/xml.rb', line 381 def each_up_host return enum_for(__method__) unless block_given? @doc.xpath("/nmaprun/host[status[@state='up']]").each do |host| yield Host.new(host) end return self end |
#host ⇒ Host
Returns the first host.
316 317 318 |
# File 'lib/nmap/xml.rb', line 316 def host each_host.first end |
#hosts ⇒ Array<Host>
Parses the hosts in the scan.
305 306 307 |
# File 'lib/nmap/xml.rb', line 305 def hosts each_host.to_a end |
#inspect ⇒ String
Inspects the XML file.
437 438 439 |
# File 'lib/nmap/xml.rb', line 437 def inspect "#<#{self.class}: #{self}>" end |
#postscript ⇒ Postscript Also known as: postscripts
The NSE scripts ran after the scan.
268 269 270 271 272 |
# File 'lib/nmap/xml.rb', line 268 def postscript @postscript ||= if (postscript = @doc.at('postscript')) Postscript.new(postscript) end end |
#prescript ⇒ Prescript Also known as: prescripts
The NSE scripts ran before the scan.
252 253 254 255 256 |
# File 'lib/nmap/xml.rb', line 252 def prescript @prescript ||= if (prescript = @doc.at('prescript')) Prescript.new(prescript) end end |
#run_stats ⇒ Array<RunStat>
Parses the essential runstats information.
177 178 179 |
# File 'lib/nmap/xml.rb', line 177 def run_stats each_run_stat.to_a end |
#scan_info ⇒ Array<Scan>
Parses the scan information.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/nmap/xml.rb', line 124 def scan_info @doc.xpath('/nmaprun/scaninfo').map do |scaninfo| Scan.new( scaninfo['type'].to_sym, scaninfo['protocol'].to_sym, scaninfo['services'].split(',').map { |ports| if ports.include?('-') Range.new(*(ports.split('-',2))) else ports.to_i end } ) end end |
#scanner ⇒ Scanner
Parses the scanner information.
99 100 101 102 103 104 105 106 |
# File 'lib/nmap/xml.rb', line 99 def scanner @scanner ||= Scanner.new( @doc.root['scanner'], @doc.root['version'], @doc.root['args'], Time.at(@doc.root['start'].to_i) ) end |
#tasks ⇒ Array<ScanTask>
Parses the tasks of the scan.
240 241 242 |
# File 'lib/nmap/xml.rb', line 240 def tasks each_task.to_a end |
#to_s ⇒ String
Converts the XML parser to a String.
427 428 429 |
# File 'lib/nmap/xml.rb', line 427 def to_s @path.to_s end |
#up_host ⇒ Host
Returns the first host found to be up during the scan.
408 409 410 |
# File 'lib/nmap/xml.rb', line 408 def up_host each_up_host.first end |
#up_hosts ⇒ Array<Host>
Parses the hosts found to be up during the scan.
397 398 399 |
# File 'lib/nmap/xml.rb', line 397 def up_hosts each_up_host.to_a end |
#verbose ⇒ Integer
Parses the verbose level.
187 188 189 |
# File 'lib/nmap/xml.rb', line 187 def verbose @verbose ||= @doc.at('verbose/@level').inner_text.to_i end |
#version ⇒ String
Parses the XML scan file version.
114 115 116 |
# File 'lib/nmap/xml.rb', line 114 def version @version ||= @doc.root['xmloutputversion'] end |