Skip to content
Gavin Schneider edited this page Mar 1, 2016 · 4 revisions

Here's a walk-through of how to use a Reports, as of version 0.1.0 of the gem.

In order to keep verbosity to a minimum, all of the examples assume that the Nexpose module has been included:

include Nexpose

As with all uses of the gem, in order to interact with a Nexpose console you will need an active, valid connection. The following line can be assumed for all code below:

nsc = Connection.new('10.2.0.1', 'nxadmin', 'secret-password')
nsc.login

Get details of the last report run

This can be useful, particularly as a building block for other operations that depend upon a report's status.

last = nsc.last_report(15)
puts "Report ID 15 last run on #{last.generated_on} with a status of #{last.status}."

Create a new template based upon an existing one

template = ReportTemplate.load(nsc, 'audit-report')
template.name = "#{template.name} Copy"
template.id = -1
template.built_in = false
template.show_device_names = true
id = template.save(nsc)
puts "New template saved with ID: #{id}"

Generate a new report from an existing configuration

config = ReportConfig.load(nsc, 15)
summary = config.generate(nsc)
unless summary.status == 'Started'
  puts "Report ID 15 finished on #{summary.generated_on} with a status of #{summary.status}."
else
  puts 'Report ID 15 started.'
end

Copy an existing report configuration

This example copies the existing configuration, but does not auto-generate the report when saved.

config.id = -1
config.name = "#{config.name} Copy"
id = config.save(nsc, false)
puts "Saved report with report ID #{id}."

Delete failed reports from the report history

Simple way of cleaning out old, invalid report attempts.

bad_reports = nsc.report_history(15).select do |summary|
  summary.status == 'Failed'
  || summary.status == 'Aborted'
  || summary.status == 'Unknown'
end
bad_reports.each do |report|
  report.delete(nsc)
end

Get a listing of all PCI-related report templates

pci_templates = nsc.report_templates.select { |tmp| tmp.name =~ /PCI/ }
pci_templates.each { |tmp| puts tmp.id }

Get a list of all report IDs successfully generated since 13 August 2012

reports = nsc.reports.select do |report|
  report.status == 'Generated' && report.generated_on > '20120813T000000000'
end
reports.each { |report| puts report.config_id }

Create a new report from scratch and download

report = ReportConfig.new('CSV Export', 'basic-vulnerability-check-results', 'csv')
report.add_filter('site', 31)
id = report.save(nsc, true)
puts "Report saved with ID #{id}"
until nsc.last_report(id)
  puts 'waiting . . .'
end

last = nsc.last_report(id)
data = nsc.download(last.uri)
puts data.inspect

Generate an Adhoc report

adhoc = AdhocReportConfig.new('audit-report', 'pdf', 31)
data = adhoc.generate(nsc)
File.open('site-31-audit.pdf', 'w') { |file| file.write(data) }