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.
-
#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.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/nmap/xml.rb', line 32 def initialize(document) case document when Nokogiri::XML::Document @doc = document when IO, StringIO @doc = Nokogiri::XML(document) else @path = File.(document) @doc = Nokogiri::XML(open(@path)) end yield self if block_given? end |
Instance Attribute Details
#path ⇒ Object (readonly)
Path of the Nmap XML scan file
18 19 20 |
# File 'lib/nmap/xml.rb', line 18 def path @path end |
Class Method Details
.load(text, &block) ⇒ Object
Use parse instead.
69 70 71 |
# File 'lib/nmap/xml.rb', line 69 def self.load(text,&block) parse(text,&block) end |
.open(path) {|xml| ... } ⇒ Object
Creates a new XML object from the file.
87 88 89 |
# File 'lib/nmap/xml.rb', line 87 def self.open(path,&block) new(path,&block) end |
.parse(text) {|xml| ... } ⇒ Object
Creates a new XML object from XML text.
60 61 62 |
# File 'lib/nmap/xml.rb', line 60 def self.parse(text,&block) new(Nokogiri::XML(text), &block) end |
Instance Method Details
#debugging ⇒ Integer
Parses the debugging level.
195 196 197 |
# File 'lib/nmap/xml.rb', line 195 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.
330 331 332 |
# File 'lib/nmap/xml.rb', line 330 def down_host each_down_host.first end |
#down_hosts ⇒ Array<Host>
Parses the hosts found to be down during the scan.
319 320 321 |
# File 'lib/nmap/xml.rb', line 319 def down_hosts each_down_host.to_a end |
#each(&block) ⇒ Object
Parses the hosts that were found to be up during the scan.
383 384 385 |
# File 'lib/nmap/xml.rb', line 383 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.
301 302 303 304 305 306 307 308 309 |
# File 'lib/nmap/xml.rb', line 301 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.
255 256 257 258 259 260 261 262 263 |
# File 'lib/nmap/xml.rb', line 255 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.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/nmap/xml.rb', line 152 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.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/nmap/xml.rb', line 213 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.
347 348 349 350 351 352 353 354 355 |
# File 'lib/nmap/xml.rb', line 347 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.
282 283 284 |
# File 'lib/nmap/xml.rb', line 282 def host each_host.first end |
#hosts ⇒ Array<Host>
Parses the hosts in the scan.
271 272 273 |
# File 'lib/nmap/xml.rb', line 271 def hosts each_host.to_a end |
#inspect ⇒ String
Inspects the XML file.
403 404 405 |
# File 'lib/nmap/xml.rb', line 403 def inspect "#<#{self.class}: #{self}>" end |
#run_stats ⇒ Array<RunStat>
Parses the essential runstats information.
175 176 177 |
# File 'lib/nmap/xml.rb', line 175 def run_stats each_run_stat.to_a end |
#scan_info ⇒ Array<Scan>
Parses the scan information.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/nmap/xml.rb', line 122 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.
97 98 99 100 101 102 103 104 |
# File 'lib/nmap/xml.rb', line 97 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.
238 239 240 |
# File 'lib/nmap/xml.rb', line 238 def tasks each_task.to_a end |
#to_s ⇒ String
Converts the XML parser to a String.
393 394 395 |
# File 'lib/nmap/xml.rb', line 393 def to_s @path.to_s end |
#up_host ⇒ Host
Returns the first host found to be up during the scan.
374 375 376 |
# File 'lib/nmap/xml.rb', line 374 def up_host each_up_host.first end |
#up_hosts ⇒ Array<Host>
Parses the hosts found to be up during the scan.
363 364 365 |
# File 'lib/nmap/xml.rb', line 363 def up_hosts each_up_host.to_a end |
#verbose ⇒ Integer
Parses the verbose level.
185 186 187 |
# File 'lib/nmap/xml.rb', line 185 def verbose @verbose ||= @doc.at('verbose/@level').inner_text.to_i end |
#version ⇒ String
Parses the XML scan file version.
112 113 114 |
# File 'lib/nmap/xml.rb', line 112 def version @version ||= @doc.root['xmloutputversion'] end |