Skip to content

Commit b5dcc0e

Browse files
committed
Make several changes.
Some important changes: * Uses optparse to parse argumnets * Prevent file handle leaks
1 parent 6746793 commit b5dcc0e

File tree

1 file changed

+102
-10
lines changed

1 file changed

+102
-10
lines changed

tools/makeiplist.rb

100644100755
Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
#!/usr/bin/env ruby
2+
23
#
4+
# This script takes a list of ranges and converts it to a per line ip list.
5+
# Demonstration:
6+
# echo 192.168.100.0-50 >> rangelist.txt
7+
# echo 192.155-156.0.1 >> rangelist.txt
8+
# echo 192.168.200.0/25 >> rangelist.txt
9+
# ruby tools/makeiplist.rb
310
#
4-
# This script takes a list of ranges and converts it to a per line ip list
11+
# Author:
12+
# mubix
513
#
614

15+
716
msfbase = __FILE__
817
while File.symlink?(msfbase)
918
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
@@ -12,16 +21,99 @@
1221
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', 'lib')))
1322
require 'msfenv'
1423
require 'rex'
24+
require 'optparse'
25+
26+
27+
class OptsConsole
28+
def self.parse(args)
29+
options = {'output' => 'iplist.txt'}
30+
31+
opts = OptionParser.new do |opts|
32+
opts.banner = %Q|This script takes a list of ranges and converts it to a per line ip list.
33+
Usage: #{__FILE__} [options]|
34+
35+
opts.separator ""
36+
opts.separator "Specific options:"
37+
38+
opts.on("-i", '-i <filename>', "Input file") do |v|
39+
options['input'] = v.to_s
40+
end
41+
42+
opts.on("-o", '-o <filename>', "(Optional) Output file. Default: iplist.txt") do |v|
43+
options['output'] = v.to_s
44+
end
1545

46+
opts.separator ""
47+
opts.separator "Common options:"
1648

17-
f = File.open('rangelist.txt', 'r')
18-
w = File.open('iplist.txt', 'a')
49+
opts.on_tail("-h", "--help", "Show this message") do
50+
puts opts
51+
exit
52+
end
53+
end
1954

20-
f.each_line do |range|
21-
ips = Rex::Socket::RangeWalker.new(range)
22-
ips.each do |ip|
23-
w.puts ip
24-
end
55+
begin
56+
opts.parse!(args)
57+
58+
unless ::File.exists?(options['input'])
59+
raise OptionParser::InvalidArgument, "Not found: #{options['input']}"
60+
end
61+
rescue OptionParser::InvalidOption
62+
puts "[*] Invalid option, try -h for usage"
63+
exit
64+
rescue OptionParser::InvalidArgument => e
65+
puts "[*] #{e.message}"
66+
exit
67+
end
68+
69+
if options.empty?
70+
puts "[*] No options specified, try -h for usage"
71+
exit
72+
end
73+
74+
options
75+
end
2576
end
26-
f.close
27-
w.close
77+
78+
79+
#
80+
# Prints IPs
81+
#
82+
def make_list(in_f, out_f)
83+
in_f.each_line do |range|
84+
ips = Rex::Socket::RangeWalker.new(range)
85+
ips.each do |ip|
86+
out_f.puts ip
87+
end
88+
end
89+
end
90+
91+
92+
#
93+
# Returns file handles
94+
#
95+
def load_files(in_f, out_f)
96+
handle_in = ::File.open(in_f, 'r')
97+
98+
# Output file not found, assuming we should create one automatically
99+
::File.open(out_f, 'w') {} unless ::File.exists?(out_f)
100+
101+
handle_out = ::File.open(out_f, 'a')
102+
103+
return handle_in, handle_out
104+
end
105+
106+
107+
options = OptsConsole.parse(ARGV)
108+
in_f, out_f = load_files(options['input'], options['output'])
109+
110+
begin
111+
puts "[*] Generating list at #{options['output']}"
112+
make_list(in_f, out_f)
113+
ensure
114+
# Always makes sure the file descriptors are closed
115+
in_f.close
116+
out_f.close
117+
end
118+
119+
puts "[*] Done."

0 commit comments

Comments
 (0)