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) {|xml| ... } ⇒ Object
Creates a new XML object from XML text.
-
.open(path) {|xml| ... } ⇒ Object
Creates a new XML object from the file.
Instance Method Summary collapse
-
#debugging ⇒ Integer
Parses the debugging level.
-
#each(&block) ⇒ Object
Parses the hosts that were found to be up 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.
-
#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_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) {|xml| ... } ⇒ Object
Creates a new XML object from XML text.
60 61 62 |
# File 'lib/nmap/xml.rb', line 60 def self.load(text,&block) new(Nokogiri::XML(text), &block) end |
.open(path) {|xml| ... } ⇒ Object
Creates a new XML object from the file.
78 79 80 |
# File 'lib/nmap/xml.rb', line 78 def self.open(path,&block) new(path,&block) end |
Instance Method Details
#debugging ⇒ Integer
Parses the debugging level.
186 187 188 |
# File 'lib/nmap/xml.rb', line 186 def debugging @debugging ||= @doc.at('debugging/@level').inner_text.to_i end |
#each(&block) ⇒ Object
Parses the hosts that were found to be up during the scan.
304 305 306 |
# File 'lib/nmap/xml.rb', line 304 def each(&block) each_up_host(&block) end |
#each_host {|host| ... } ⇒ XML, Enumerator
Parses the hosts in the scan.
246 247 248 249 250 251 252 253 254 |
# File 'lib/nmap/xml.rb', line 246 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.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/nmap/xml.rb', line 143 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.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/nmap/xml.rb', line 204 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.
279 280 281 282 283 284 285 286 287 |
# File 'lib/nmap/xml.rb', line 279 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 |
#hosts ⇒ Array<Host>
Parses the hosts in the scan.
262 263 264 |
# File 'lib/nmap/xml.rb', line 262 def hosts each_host.to_a end |
#inspect ⇒ String
Inspects the XML file.
324 325 326 |
# File 'lib/nmap/xml.rb', line 324 def inspect "#<#{self.class}: #{self}>" end |
#run_stats ⇒ Array<RunStat>
Parses the essential runstats information.
166 167 168 |
# File 'lib/nmap/xml.rb', line 166 def run_stats each_run_stat.to_a end |
#scan_info ⇒ Array<Scan>
Parses the scan information.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/nmap/xml.rb', line 113 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.
88 89 90 91 92 93 94 95 |
# File 'lib/nmap/xml.rb', line 88 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.
229 230 231 |
# File 'lib/nmap/xml.rb', line 229 def tasks each_task.to_a end |
#to_s ⇒ String
Converts the XML parser to a String.
314 315 316 |
# File 'lib/nmap/xml.rb', line 314 def to_s @path.to_s end |
#up_hosts ⇒ Array<Host>
Parses the hosts found to be up during the scan.
295 296 297 |
# File 'lib/nmap/xml.rb', line 295 def up_hosts each_up_host.to_a end |
#verbose ⇒ Integer
Parses the verbose level.
176 177 178 |
# File 'lib/nmap/xml.rb', line 176 def verbose @verbose ||= @doc.at('verbose/@level').inner_text.to_i end |
#version ⇒ String
Parses the XML scan file version.
103 104 105 |
# File 'lib/nmap/xml.rb', line 103 def version @version ||= @doc.root['xmloutputversion'] end |