Scripting Example: Exporting scan ranges to CSV

Document created by S Tempest Employee on Apr 4, 2014Last modified by gavin on Jun 30, 2015
Version 4Show Document
  • View in full screen mode

BY BOB

 

The code below:

  • Logs in to Nexpose.
  • Gets list of all sites.
  • Opens a new CSV file and writes the IP ranges for all the sites into it.
  • Logs out of Nexpose

 

For more details on this scenario, see Scripting Scenario: Exporting scan ranges to CSV

 

require 'nexpose'
require 'csv'
@host = 'localhost'
@userid = 'bob'
@password = 'IamSuperCool'   
nsc = Nexpose::Connection.new(@host, @userid, @password)
puts 'Logging into Nexpose'
nsc.login
puts 'Logged into Nexpose'
sites = nsc.list_sites
CSV.open('ip-ranges.csv', "wb") do |csv|
    csv << ["Site ID", "Site Name", "Range Start", "Range End"]
    sites.each do |site|
    site = Nexpose::Site.load(nsc, site.id)
    puts "Getting defined assets for #{site.name}"
        site.assets.each do |asset|
          if asset.respond_to? :from
            puts "IP range #{asset.from} - #{asset.to}"
            csv << ["#{site.id}", "#{site.name}", "#{asset.from}", "#{asset.to}"]
          else
            puts "Host #{asset.host}"
            csv << ["#{site.id}", "#{site.name}", "#{asset.host}"]
          end
        end
      end
    end
puts 'Logging out'
nsc.logout
exit



 

 

Thanks to Gavin Schneider and ospannero

1 person found this helpful

Attachments

    Outcomes