-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset_lookup_to_csv.rb
79 lines (66 loc) · 1.69 KB
/
asset_lookup_to_csv.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#Takes a text file of hostnames and creates a CSV file with headers "asset", "IP", "site"
require 'highline/import'
require 'nexpose'
require 'csv'
require 'Resolv'
include Nexpose
filename = ARGV[0]
if filename.nil?
puts ARGV.to_s
puts "USAGE: ruby Nexpose_VMP_asset_lookup.rb <text file of hostnames>"
exit
end
hostnames = []
puts "Processing hosts from #{filename}:"
File.open(filename).readlines.each do |line|
hostnames << line.strip
end
default_host = 'nexpose'
host = ask('Enter hostname for Nexpose console: ') { |q| q.default = default_host }
default_name = 'nxadmin'
userid = ask('Enter your Nexpose username: ') { |q| q.default = default_name }
password = ask('Enter your Nexpose password: ') { |q| q.echo = '*' }
nsc = Connection.new(host, userid, password)
puts 'Logging into Nexpose'
nsc.login
puts 'Logged into Nexpose'
#create hash map of all assets
all_assets = nsc.assets.reduce({}) do |hash, dev|
$stderr.puts("Duplicate asset: #{dev.address}") if @debug and hash.member? dev.address
hash[dev.address] = dev
hash
end
sites = nsc.sites.reduce({}) do |hash, site|
hash[site.id] = site
hash
end
file = "nexpose_asset_lookup.csv"
CSV.open(file, 'wb') do |csv|
csv << ["hostname", "IP", "site"]
end
csv_data = []
hostnames.each do |hostname|
csv_row = []
hostname.downcase!
csv_row.push(hostname)
puts hostname
begin
ip = Resolv.getaddress hostname
csv_row.push(ip)
rescue
csv_row.push(' ')
next
end
begin
site = sites[all_assets[ip].site_id].name
puts site
csv_row.push(site)
rescue
csv_row.push(' ')
next
end
csv_data.push(csv_row)
end
CSV.open(file, "a+") do |csv|
csv_data.each { |row| csv << row}
end