-
Notifications
You must be signed in to change notification settings - Fork 1
/
util-cleanup.rb
executable file
·199 lines (154 loc) · 5.64 KB
/
util-cleanup.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env ruby
#
# util-cleanup.rb
#
# utility to move nmap XML files containing 0 hosts to a backup directory
# and out of the logs directory. Speeds query processes of both fathom.rb
# and fp-list.rb
#
# Part of the Fathom suite written by Tom Sellers <fathom_at_fadedcode.net>
#
# Requires:
# Ruby (1.9.1 recommended)
#
# Kris Katterjohn's Ruby Nmap::Parser
# http://rubynmap.sourceforge.net/
#
# License
# Copyright (c) 2015 Tom Sellers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
PROG_VERSION = '0.98.01'
require 'nmap/parser'
require 'optparse'
require 'fileutils'
require 'ipaddr'
$ip_filter = nil
$backup = './logs/backup'
$params = ParseArgs.parse(ARGV)
if $params['purge']
puts "Are you sure you wish to delete everything in the #{$backup} directory? (y/n)"
verify = gets
exit if verify.nil?
verify.chomp!
exit if verify != 'y' && verify != 'Y'
FileUtils.rm Dir.glob($backup + '/*'), :force => true
exit
end
listing = Dir.glob('./logs/*.xml')
listing.each { |file|
begin
parser = Nmap::Parser.parsefile(file)
rescue
if $error_message
$error_message = $error_message + "\r\nError parsing #{file}."
else
$error_message = "Error parsing #{file}."
end # $error_message
else
if parser.hosts('up').count == 0
filepath = file.gsub('.xml', '.*')
puts "Moving #{filepath}, #{parser.hosts('up').count} hosts in this file group."
FileUtils.mv Dir.glob(filepath), $backup
elsif $ip_filter
parser.hosts('up') do |host|
if $ip_filter.include?(IPAddr.new(host.ip4_addr))
filepath = file.gsub('.xml', '.*')
puts "Moving #{filepath} due to IP Address selection.\n\t#{parser.hosts('up').count} hosts in this file group."
FileUtils.mv Dir.glob(filepath), $backup
end
end
end
end # begin
}
if $error_message
puts
puts
puts '##############################################################################'
puts 'Errors during operation:'
puts $error_message
puts '##############################################################################'
end # $error_message
# ----- Method Definitions -----
BEGIN {
class ParseArgs
def self.parse(args)
options = {}
legal_option = nil
opts = OptionParser.new do |opts|
opts.banner = 'Usage: util-cleanup.rb [options]'
opts.separator ''
opts.separator 'Options:'
opts.on('--archive', "Move data files containing 0 hosts to backup directory (#{$backup})") do
legal_option = true
end
opts.on('--purge', "Delete all files in the backup directory (#{$backup})") do
options['purge'] = true
legal_option = true
end
opts.separator ''
opts.separator 'Selection options:'
opts.on('--ip-filter <ip_address>', 'Select files containing certain IP Addresses',
'Acceptable formats are as a single IP address (xxx.xxx.xxx.xxx)',
'or in IP/CIDR notation (xxx.xxx.xxx.xxx/xx)',
'or in IP/netmask notation (xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx)') do |address_filter|
# validate ip filter
# check for format xxx.xxx.xxx.xxx
valid_filter = true if address_filter.to_s =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
# check for format xxx.xxx.xxx.xxx/xx
valid_filter = true if address_filter.to_s =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}$/
# check for format xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx (netmask)
valid_filter = true if address_filter.to_s =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
if valid_filter
$ip_filter = IPAddr.new address_filter.to_s
legal_option = true
else
puts
puts 'Error: Option passed for network address does not appear valid. Use -h to see valid formats.'
puts
exit
end # if valid_filter
end
opts.separator ''
opts.on('-v', '--version', 'Show version information') do
puts
puts "\tutil-cleanup #{PROG_VERSION} by Tom Sellers"
puts
puts "\tSupporting software versions:"
puts "\t\tRuby version: #{RUBY_VERSION}"
puts "\t\tNmap::Parser version: #{Nmap::Parser::Version} #{Nmap::Parser::Stage}"
exit
end
end
begin
opts.parse!(args)
rescue OptionParser::InvalidOption
puts 'Invalid option, try -h for usage'
exit
end
if !legal_option
puts 'Error: No search criteria selected...'
puts
puts opts
exit
end
options
end # self.parse(args)
end # ParseArgs
}