Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from marcuscarey/patch-1
Browse files Browse the repository at this point in the history
Add support for passing in XML strings to Nessus::Parse.new
  • Loading branch information
djcas9 committed Sep 11, 2013
2 parents 7c128d7 + ff911eb commit 51323a4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 41 deletions.
67 changes: 34 additions & 33 deletions examples/example.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
#!/usr/bin/env ruby
$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))

#!/usr/bin/env ruby
require 'rubygems'
require 'nessus'

# Ruby-Nessus Example
#
# This proc is called by scan.each_host in each variation of Nessus::Parse
def print_info(host)
puts host.ip
puts host.hostname
puts host.os_name
puts host.runtime

Nessus::Parse.new('example_v1.nessus') do |scan|

scan.each_host do |host|
puts host.ip
puts host.hostname
puts host.os_name
puts host.runtime

#puts host.mac_addr
# puts host.event_percentage_for('icmp', true)
# puts host.ports.inspect
#
# puts "\n"
#
host.each_event do |event|

puts "=> #{event.name}" if event.name
# puts event.synopsis if event.synopsis
# puts "\n"
# puts event.output
# puts "\n"
# puts event.patch_publication_date.pretty if event.patch_publication_data
# puts event.see_also unless event.see_also.empty?
# puts event.synopsis if event.synopsis
# puts event.solution if event.solution

end
#
# puts host.mac_addr
# puts host.event_percentage_for('icmp', true)
# puts host.ports.inspect

host.each_event do |event|
puts "=> #{event.name}" if event.name
# puts event.synopsis if event.synopsis
# puts "\n"
# puts event.output
# puts "\n"


# puts event.patch_publication_date.pretty if event.patch_publication_data
# puts event.see_also unless event.see_also.empty?
# puts event.synopsis if event.synopsis
# puts event.solution if event.solution
end


end

# From a file:
puts '+ Using a Nessus XML file:'
Nessus::Parse.new('example_v1.nessus') do |scan|
scan.each_host(&method(:print_info))
end

puts

# From a string:
puts '+ Using an XML string:'
Nessus::Parse.new(nil, { :xml => File.read('example_v1.nessus') }) do |scan|
scan.each_host(&method(:print_info))
end
6 changes: 3 additions & 3 deletions lib/nessus/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ module Nessus

class Parse

def initialize(file, options={}, &block)
@file = File.open(file)
def initialize(file = nil, options = {}, &block)
doc = file ? File.read(file) : options[:xml]
@xml = Nokogiri::XML.parse(doc)
@version = options[:version]
@xml = Nokogiri::XML.parse(@file.read)

if @version
case @version
Expand Down
14 changes: 9 additions & 5 deletions spec/helpers/xml.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Helpers
v1 = File.join(File.dirname(__FILE__),'example_v1.nessus')
v2 = File.join(File.dirname(__FILE__),'example_v2.nessus')
DOT_NESSUS_V1 = Nokogiri::XML.parse(File.open(v1).read)
DOT_NESSUS_V2 = Nokogiri::XML.parse(File.open(v2).read)
end
DOT_NESSUS_V1_PATH = File.join(File.dirname(__FILE__),'example_v1.nessus')
DOT_NESSUS_V2_PATH = File.join(File.dirname(__FILE__),'example_v2.nessus')

DOT_NESSUS_V1_DOC = File.read(DOT_NESSUS_V1_PATH)
DOT_NESSUS_V2_DOC = File.read(DOT_NESSUS_V2_PATH)

DOT_NESSUS_V1 = Nokogiri::XML.parse(DOT_NESSUS_V1_DOC)
DOT_NESSUS_V2 = Nokogiri::XML.parse(DOT_NESSUS_V2_DOC)
end
22 changes: 22 additions & 0 deletions spec/ruby-nessus/parse_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require_relative '../spec_helper'
require_relative '../helpers/xml'

describe 'Nessus::Parse' do
it 'should parse a valid v1 .nessus file' do
lambda { Nessus::Parse.new(Helpers::DOT_NESSUS_V1_PATH) }.should_not raise_error
end

it 'should parse a valid v2 .nessus file' do
lambda { Nessus::Parse.new(Helpers::DOT_NESSUS_V2_PATH) }.should_not raise_error
end

it 'should parse a valid v1 .nessus string' do
options = { :xml => Helpers::DOT_NESSUS_V1_DOC }
lambda { Nessus::Parse.new(nil, options) }.should_not raise_error
end

it 'should parse a valid v2 .nessus string' do
options = { :xml => Helpers::DOT_NESSUS_V2_DOC }
lambda { Nessus::Parse.new(nil, options) }.should_not raise_error
end
end

0 comments on commit 51323a4

Please sign in to comment.