-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsite-restore.rb
77 lines (63 loc) · 2.11 KB
/
site-restore.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
76
77
require "nexpose"
require "io/console"
require "yaml"
# Nexpose Site Restore tool
#
# Restores the full configuration of Nexpose sites from yaml files created by the site-backup.rb tool.
# This includes assigned assets, scan templates, schedules, etc.
#
# Example: ruby site-restore.rb <Nexpose console host> <site1.yml> <site2.yml> ...
# You will be prompted to log in, then it will go through each .yml file you specified and check for an existing site with that name.
# If a site is found with that name, it will first confirm the overwite action, then restore the config to that existing site.
# If no sites with that name are found, it will ask to create and restore to a new site.
# Need this since Ruby 2.x tries to use proxies.
ENV['http_proxy'] = nil
ENV['https_proxy'] = nil
include Nexpose
# Create connection and login
puts "Nexpose Login"
puts "-------------"
print "Username: "
username = $stdin.gets.chomp
print "Password: "
password = STDIN.noecho(&:gets).chomp
puts
server = ARGV[0]
ARGV.shift
@nsc = Connection.new(server, username, password)
@nsc.login
ARGV.each do |rFile|
sitename = rFile.sub('.yml', '').gsub('%', '/').sub('.\\', '')
puts "Checking for site: "+sitename+"..."
namesIds = {}
@nsc.sites.each {|site| namesIds[site.name] = site.id}
site = ""
if namesIds.keys.include?(sitename)
site = Site.load(@nsc, namesIds[sitename])
puts "Found existing site: "+sitename+" ("+site.id.to_s+")"
print "Restore from file and overwrite this site? (y/n): "
yesno = $stdin.gets.chomp
if yesno != 'y'
puts "Cancelling..."
exit
end
else
puts "No site named "+sitename+" found."
print "Create it and restore to it? (y/n): "
yesno = $stdin.gets.chomp
if yesno != 'y'
puts "Cancelling"
exit
end
end
puts "Loading restore file..."
restoreSite = YAML.load(File.read(rFile))
if site == ""
restoreSite.id = -1
else
restoreSite.id = site.id
end
puts "Restoring site: "+sitename+"..."
restoreSite.save(@nsc)
puts "Done"
end