class OpenvasCli::VasResult

Attributes

description[RW]
host[RW]
notes[RW]
overrides[RW]
port[RW]
result_id[RW]
rule_id[RW]
subnet[RW]
task_id[RW]
threat[RW]

Public Class Methods

get_all(options = {}) click to toggle source
# File lib/openvas-cli/vas_result.rb, line 15
def self.get_all(options = {})
  options[:sort_by] ||= :threat
  
  params = {:overrides => 0, :notes => 0}
  
  params[:result_id] = options[:id] if options[:id]
  if options[:task_id]
    params[:task_id] = options[:task_id]
    params[:apply_overrides] = 1 if options[:apply_overrides]
  end

  levels = []    
  if options[:filter]
    options[:filter].each { |ft|
      case ft
      when :high
        levels << "High"
      when :medium
        levels << "Medium"
      when :low
        levels << "Low"
      when :log
        levels << "Log"
      when :debug
        levels << "Debug"
      end
    }
  end
  req = Nokogiri::XML::Builder.new { |xml|
    xml.get_results(params)
  }
  
  results = {}
  
  begin
    xml = connection.send_receive(req.doc)
    xml.xpath("//result").each { |xr|
      id     = extract_value_from("@id", xr)
      threat = extract_value_from("threat", xr)
      if (levels.empty? || levels.include?(threat)) && results.has_key?(id) == false
        res = parse_result_node(xr, options[:task_id])
        results[res.result_id] = res
      end
    }    
  rescue VasExceptions::CommandException => e
  end
  
  ret = results.values

  #Sort Results
  ret.sort!{ |a,b| a.result_id <=> b.result_id }       if options[:sort_by] == :result_id
  ret.sort!{ |a,b| a.host <=> b.host }                 if options[:sort_by] == :host
  ret.sort!{ |a,b| a.rule_id <=> b.rule_id }           if options[:sort_by] == :rule_id
  ret.sort!{ |a,b| a.subnet <=> b.subnet }             if options[:sort_by] == :subnet
  ret.sort!{ |a,b| b.threat_level <=> a.threat_level } if options[:sort_by] == :threat
  
  #Fake Pagination
  if options[:start]
    if options[:count]
      ret = ret[options[:start], options[:count]]
    else
      ret = ret[options[:start], ret.length - options[:start]]
    end
  end
  
  ret
end
get_by_id(id) click to toggle source
# File lib/openvas-cli/vas_result.rb, line 11
def self.get_by_id(id)
  get_all(:id => id)[0]
end
parse_result_node(node, task_id = nil) click to toggle source
# File lib/openvas-cli/vas_result.rb, line 83
def self.parse_result_node(node, task_id = nil)
  res             = VasResult.new
  res.id          = extract_value_from("@id", node)
  res.threat      = extract_value_from("threat", node)
  res.subnet      = extract_value_from("subnet", node)
  res.host        = extract_value_from("host", node)
  res.rule_id     = extract_value_from("nvt/@oid", node)
  res.description = extract_value_from("description", node)
  res.task_id     = task_id if task_id
  
  res
end

Public Instance Methods

threat_level() click to toggle source
# File lib/openvas-cli/vas_result.rb, line 96
def threat_level
  case @threat
  when "High"
    5
  when "Medium"
    4
  when "Low"
    3
  when "Log"
    2
  when "Debug"
    1
  else
    0
  end
end