Class: Nmap::XML
- Inherits:
-
Object
- Object
- Nmap::XML
- Includes:
- Enumerable
- Defined in:
- lib/nmap/xml.rb,
lib/nmap/xml/os.rb,
lib/nmap/xml/cpe.rb,
lib/nmap/xml/hop.rb,
lib/nmap/xml/host.rb,
lib/nmap/xml/port.rb,
lib/nmap/xml/scan.rb,
lib/nmap/xml/script.rb,
lib/nmap/xml/status.rb,
lib/nmap/xml/uptime.rb,
lib/nmap/xml/address.rb,
lib/nmap/xml/cpe/url.rb,
lib/nmap/xml/scanner.rb,
lib/nmap/xml/scripts.rb,
lib/nmap/xml/service.rb,
lib/nmap/xml/hostname.rb,
lib/nmap/xml/os_class.rb,
lib/nmap/xml/os_match.rb,
lib/nmap/xml/run_stat.rb,
lib/nmap/xml/sequence.rb,
lib/nmap/xml/prescript.rb,
lib/nmap/xml/scan_task.rb,
lib/nmap/xml/postscript.rb,
lib/nmap/xml/traceroute.rb,
lib/nmap/xml/host_script.rb,
lib/nmap/xml/tcp_sequence.rb,
lib/nmap/xml/ip_id_sequence.rb,
lib/nmap/xml/tcp_ts_sequence.rb
Overview
Represents an Nmap XML file.
Defined Under Namespace
Modules: CPE, Scripts Classes: Address, Hop, Host, HostScript, Hostname, IpIdSequence, OS, OSClass, OSMatch, Port, Postscript, Prescript, RunStat, Scan, ScanTask, Scanner, Script, Sequence, Service, Status, TcpSequence, TcpTsSequence, Traceroute, Uptime
Instance Attribute Summary collapse
-
#doc ⇒ Nokogiri::XML
readonly
private
The parsed XML document.
-
#path ⇒ String?
readonly
Path of the Nmap XML scan file.
Class Method Summary collapse
-
.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(doc, path: nil) {|xml| ... } ⇒ XML
constructor
Creates a new XML object.
-
#postscript ⇒ Postscript
The NSE scripts ran after the scan.
-
#prescript ⇒ Prescript
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.
-
#task(name) ⇒ ScanTask?
Finds the task with the given name.
-
#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(doc, path: nil) {|xml| ... } ⇒ XML
Creates a new XML object.
46 47 48 49 50 51 |
# File 'lib/nmap/xml.rb', line 46 def initialize(doc, path: nil) @doc = doc @path = File.(path) if path yield self if block_given? end |
Instance Attribute Details
#doc ⇒ Nokogiri::XML (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.
24 25 26 |
# File 'lib/nmap/xml.rb', line 24 def doc @doc end |
#path ⇒ String? (readonly)
Path of the Nmap XML scan file
29 30 31 |
# File 'lib/nmap/xml.rb', line 29 def path @path end |
Class Method Details
.open(path) {|xml| ... } ⇒ Object
Creates a new XML object from the file.
85 86 87 88 89 90 |
# File 'lib/nmap/xml.rb', line 85 def self.open(path,&block) path = File.(path) doc = Nokogiri::XML(File.open(path)) new(doc, path: path, &block) end |
.parse(text) {|xml| ... } ⇒ Object
Creates a new XML object from XML text.
67 68 69 |
# File 'lib/nmap/xml.rb', line 67 def self.parse(text,&block) new(Nokogiri::XML(text),&block) end |
Instance Method Details
#debugging ⇒ Integer
Parses the debugging level.
196 197 198 |
# File 'lib/nmap/xml.rb', line 196 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.
374 375 376 |
# File 'lib/nmap/xml.rb', line 374 def down_host each_down_host.first end |
#down_hosts ⇒ Array<Host>
Parses the hosts found to be down during the scan.
363 364 365 |
# File 'lib/nmap/xml.rb', line 363 def down_hosts each_down_host.to_a end |
#each(&block) ⇒ Object
Parses the hosts that were found to be up during the scan.
427 428 429 |
# File 'lib/nmap/xml.rb', line 427 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.
345 346 347 348 349 350 351 352 353 |
# File 'lib/nmap/xml.rb', line 345 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.
299 300 301 302 303 304 305 306 307 |
# File 'lib/nmap/xml.rb', line 299 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.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/nmap/xml.rb', line 153 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.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/nmap/xml.rb', line 214 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.
391 392 393 394 395 396 397 398 399 |
# File 'lib/nmap/xml.rb', line 391 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.
326 327 328 |
# File 'lib/nmap/xml.rb', line 326 def host each_host.first end |
#hosts ⇒ Array<Host>
Parses the hosts in the scan.
315 316 317 |
# File 'lib/nmap/xml.rb', line 315 def hosts each_host.to_a end |
#postscript ⇒ Postscript
The NSE scripts ran after the scan.
280 281 282 283 284 |
# File 'lib/nmap/xml.rb', line 280 def postscript @postscript ||= if (postscript = @doc.at('postscript')) Postscript.new(postscript) end end |
#prescript ⇒ Prescript
The NSE scripts ran before the scan.
266 267 268 269 270 |
# File 'lib/nmap/xml.rb', line 266 def prescript @prescript ||= if (prescript = @doc.at('prescript')) Prescript.new(prescript) end end |
#run_stats ⇒ Array<RunStat>
Parses the essential runstats information.
176 177 178 |
# File 'lib/nmap/xml.rb', line 176 def run_stats each_run_stat.to_a end |
#scan_info ⇒ Array<Scan>
Parses the scan information.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/nmap/xml.rb', line 123 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.
98 99 100 101 102 103 104 105 |
# File 'lib/nmap/xml.rb', line 98 def scanner @scanner ||= Scanner.new( @doc.root['scanner'], @doc.root['version'], @doc.root['args'], Time.at(@doc.root['start'].to_i) ) end |
#task(name) ⇒ ScanTask?
Finds the task with the given name.
254 255 256 |
# File 'lib/nmap/xml.rb', line 254 def task(name) each_task.find { |scan_task| scan_task.name == name } end |
#tasks ⇒ Array<ScanTask>
Parses the tasks of the scan.
239 240 241 |
# File 'lib/nmap/xml.rb', line 239 def tasks each_task.to_a end |
#to_s ⇒ String
Converts the XML parser to a String.
437 438 439 440 441 |
# File 'lib/nmap/xml.rb', line 437 def to_s if @path then @path.to_s else @doc.to_s end end |
#up_host ⇒ Host
Returns the first host found to be up during the scan.
418 419 420 |
# File 'lib/nmap/xml.rb', line 418 def up_host each_up_host.first end |
#up_hosts ⇒ Array<Host>
Parses the hosts found to be up during the scan.
407 408 409 |
# File 'lib/nmap/xml.rb', line 407 def up_hosts each_up_host.to_a end |
#verbose ⇒ Integer
Parses the verbose level.
186 187 188 |
# File 'lib/nmap/xml.rb', line 186 def verbose @verbose ||= @doc.at('verbose/@level').inner_text.to_i end |
#version ⇒ String
Parses the XML scan file version.
113 114 115 |
# File 'lib/nmap/xml.rb', line 113 def version @version ||= @doc.root['xmloutputversion'] end |