|
1 | 1 | #!/usr/bin/env ruby
|
| 2 | + |
2 | 3 | #
|
| 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 |
3 | 10 | #
|
4 |
| -# This script takes a list of ranges and converts it to a per line ip list |
| 11 | +# Author: |
| 12 | +# mubix |
5 | 13 | #
|
6 | 14 |
|
| 15 | + |
7 | 16 | msfbase = __FILE__
|
8 | 17 | while File.symlink?(msfbase)
|
9 | 18 | msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
|
|
12 | 21 | $:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', 'lib')))
|
13 | 22 | require 'msfenv'
|
14 | 23 | 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 |
15 | 45 |
|
| 46 | + opts.separator "" |
| 47 | + opts.separator "Common options:" |
16 | 48 |
|
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 |
19 | 54 |
|
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 |
25 | 76 | 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