lib/ruby-nessus/host.rb in ruby-nessus-0.1.3 vs lib/ruby-nessus/host.rb in ruby-nessus-0.1.4

- old
+ new

@@ -1,7 +1,9 @@ module Nessus class Host + include Enumerable + # Host attr_reader :host # Creates A New Host Object # @param [Object] Host Object @@ -24,29 +26,36 @@ # @return [DateTime] # The Host Scan Start Time # @example # scan.scan_start_time #=> 'Fri Nov 11 23:36:54 1985' def scan_start_time - @host_scan_time = DateTime.strptime(@host.at('startTime').inner_text, fmt='%a %b %d %H:%M:%S %Y') + if @host.at('startTime').inner_text.blank? + return false + else + @host_scan_time = DateTime.strptime(@host.at('startTime').inner_text, fmt='%a %b %d %H:%M:%S %Y') + end end # Return the host scan stop time. # @return [DateTime] # The Host Scan Stop Time # @example # scan.scan_start_time #=> 'Fri Nov 11 23:36:54 1985' def scan_stop_time - @host_scan_time = DateTime.strptime(@host.at('stopTime').inner_text, fmt='%a %b %d %H:%M:%S %Y') + if @host.at('stopTime').inner_text.blank? + return false + else + @host_scan_time = DateTime.strptime(@host.at('stopTime').inner_text, fmt='%a %b %d %H:%M:%S %Y') + end end - + # Return the host run time. # @return [String] # The Host Scan Run Time # @example # scan.scan_run_time #=> '2 hours 5 minutes and 16 seconds' def scan_runtime - if scan_start_time.to_s.empty? | scan_stop_time.to_s.empty?; return "N/A"; end h = ("#{Time.parse(scan_stop_time.to_s).strftime('%H').to_i - Time.parse(scan_start_time.to_s).strftime('%H').to_i}").gsub('-', '') m = ("#{Time.parse(scan_stop_time.to_s).strftime('%M').to_i - Time.parse(scan_start_time.to_s).strftime('%M').to_i}").gsub('-', '') s = ("#{Time.parse(scan_stop_time.to_s).strftime('%S').to_i - Time.parse(scan_start_time.to_s).strftime('%S').to_i}").gsub('-', '') return "#{h} hours #{m} minutes and #{s} seconds" end @@ -95,11 +104,11 @@ # @example # host.open_ports #=> 213 def open_ports @scanned_ports ||= @host.at('num_ports').inner_text.to_i end - + # Returns All Informational Event Objects For A Given Host. # @yield [prog] If a block is given, it will be passed the newly # created Event object. # @yieldparam [EVENT] prog The newly created Event object. # @return [Integer] @@ -117,11 +126,11 @@ @host.xpath("ReportItem").each do |event| next if event.at('severity').inner_text.to_i != 0 @informational_events << Event.new(event) @informational_event_count += 1 end - + end @informational_events.each(&block) return @informational_event_count end @@ -135,13 +144,13 @@ # @example # host.low_severity_events do |low| # puts low.name if low.name # end def low_severity_events(&block) - + @low_severity_count = @host.at('num_lo').inner_text.to_i - + unless @low_severity_events @low_severity_events = [] @host.xpath("ReportItem").each do |event| next if event.at('severity').inner_text.to_i != 1 @@ -163,13 +172,13 @@ # @example # host.medium_severity_events do |medium| # puts medium.name if medium.name # end def medium_severity_events(&block) - + @high_severity_count = @host.at('num_med').inner_text.to_i - + unless @medium_severity_events @medium_severity_events = [] @host.xpath("ReportItem").each do |event| next if event.at('severity').inner_text.to_i != 2 @@ -191,21 +200,21 @@ # @example # host.high_severity_events do |high| # puts high.name if high.name # end def high_severity_events(&block) - + @high_severity_count = @host.at('num_hi').inner_text.to_i - + unless @high_severity_events @high_severity_events = [] @host.xpath("ReportItem").each do |event| next if event.at('severity').inner_text.to_i != 3 @high_severity_events << Event.new(event) end - + end @high_severity_events.each(&block) return @high_severity_count end @@ -230,9 +239,16 @@ # end def events(&block) @host.xpath("ReportItem").each do |event| block.call(Event.new(event)) if block end + end + + # Parses the events of the host. + # @return [Array<String>] + # The events of the host. + def all_events + Enumerator.new(self,:events).to_a end end end