network_scanner 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 523886c0cc9e935ee6629722850bf2aac37a2a94
4
- data.tar.gz: a249a6aba70f6c99e6ac6ebb67e6a905f84c9afb
3
+ metadata.gz: b9bdf3586d28cb2c6ba41ce2337d054d1ebcb041
4
+ data.tar.gz: 23164e74226b74b5f02f50dbe5c212778d3db68d
5
5
  SHA512:
6
- metadata.gz: 26091157cbe2b73b7faf985f049f8fe96d3dbe712a4ee487d59eb9a933d60459b6afa12b252c5c30ae8d56c868e6d73cd6d5a815be86f3fd093aeb16cb524af1
7
- data.tar.gz: 618b76d95a7e2558ab2c8a80cc7fccc6457410b5670753320d8c495c703d5e306a7531233f135089cc40bbea8b4bf50a637885412e9ad1283aeb313f655f8b78
6
+ metadata.gz: f23d8cb010bd8f316a86aaa50d2f16b07725344a7c55e387c91e3b0678e29e89a977a194cf3d848a42262d3284c74c625587d879f9779f676f068ef906a0c8ee
7
+ data.tar.gz: d54b2b76b0019752e3a9ef42d4a767d15cd8b6a3b423b3e81f49ce868ab35caf52ac7f0ed45084145c712e12ee177842c28ed65c9fcd28c8224bd36a50d064e8
data/bin/network_scanner CHANGED
@@ -5,42 +5,63 @@ require 'optparse'
5
5
 
6
6
  options = {}
7
7
  OptionParser.new do |opts|
8
- opts.on('--cache FILE', String, 'Caches the IPs up on the network in FILE for quicker scanning') do |file|
9
- options[:cache] = file
8
+ opts.on('-f', '--format FORMAT', String, 'Format to read/write (text/json). Defaults to text') do |format|
9
+ options[:format] = format
10
10
  end
11
11
 
12
- opts.on('--interface INTERFACE', String, 'Specifys interface to scan') do |interface|
12
+ opts.on('-s', '--poolsize POOLSIZE', Integer, 'Specify thread pool size (default 100)') do |pool|
13
+ options[:pool] = pool
14
+ end
15
+
16
+ opts.on('-i', '--interface INTERFACE', String, 'Specifys interface to scan') do |interface|
13
17
  options[:interface] = interface
14
18
  end
15
19
 
16
- opts.on('--range RANGE', String, 'Specify a range to scan(192.168.1.100-192.168.1.200)') do |range|
20
+ opts.on('-r', '--range RANGE', String, 'Specify a range to scan(192.168.1.100-192.168.1.200)') do |range|
17
21
  options[:range] = range
18
22
  end
19
23
 
20
- opts.on('--portscan PORT', Integer, 'Port to scan for') do |port|
21
- options[:port] = port
24
+ opts.on('-c', '--cache FILE', String, 'Cache to read ips from') do |file|
25
+ options[:cache] = file
22
26
  end
23
27
 
24
- opts.on('--cacheread FILE', String, 'Cache to read ips from') do |file|
25
- options[:cacheread] = file
28
+ opts.on('-P', '--ping', 'Checks for all machines that respond to ping') do
29
+ options[:ping] = true
26
30
  end
27
31
 
28
- opts.on('--poolsize POOLSIZE', Integer, 'Specify thread pool size (default 100)') do |pool|
29
- options[:pool] = pool
32
+ opts.on('-p', '--portscan PORT', Integer, 'Port to scan for') do |port|
33
+ options[:port] = port
30
34
  end
31
35
 
32
- opts.on_tail('--help', 'Show help message') do
36
+ opts.on('-o', '--output OUTPUTFILE', String, 'File to output to') do |file|
37
+ options[:output] = file
38
+ end
39
+
40
+ opts.on_tail('-h', '--help', 'Show help message') do
33
41
  puts opts
34
42
  exit
35
43
  end
36
44
  end.parse!
37
45
 
38
-
39
46
  network_scanner = NetworkScanner.new
40
47
  if options[:pool]
48
+ unless options[:pool] > 0
49
+ raise ArgumentError.new('Specify a pool size greater than 0')
50
+ end
41
51
  network_scanner.pool_size = options[:pool]
42
52
  end
43
53
 
54
+ if options[:format]
55
+ unless ['text','json'].include?(options[:format])
56
+ raise ArgumentError.new('Specify either text or json as a format')
57
+ end
58
+ network_scanner.format = options[:format]
59
+ end
60
+
61
+ if options[:output]
62
+ network_scanner.output_name = options[:output]
63
+ end
64
+
44
65
  if options[:interface]
45
66
  network_scanner.get_interface_ips(options[:interface])
46
67
  end
@@ -50,11 +71,11 @@ if options[:range]
50
71
  end
51
72
 
52
73
  if options[:cache]
53
- network_scanner.cache(options[:cache])
74
+ network_scanner.get_cache_ips(options[:cache])
54
75
  end
55
76
 
56
- if options[:cacheread]
57
- network_scanner.cacheread(options[:cacheread])
77
+ if options[:ping]
78
+ network_scanner.check_pings
58
79
  end
59
80
 
60
81
  if options[:port]
@@ -24,10 +24,19 @@ module NetworkScanner
24
24
  end
25
25
 
26
26
  class Scanner
27
- attr_accessor :pool_size
27
+ attr_accessor :pool_size, :format, :output_name
28
28
 
29
29
  def initialize(opts = {})
30
30
  @pool_size = 100
31
+ @format = 'text'
32
+ end
33
+
34
+ def text?
35
+ @format == 'text'
36
+ end
37
+
38
+ def json?
39
+ @format == 'json'
31
40
  end
32
41
 
33
42
  def get_interface_ips(interface)
@@ -59,7 +68,6 @@ module NetworkScanner
59
68
  end
60
69
 
61
70
  puts "Checking ips in (#{first_range}).(#{second_range}).(#{third_range}).(#{fourth_range})"
62
- @ips = @ips_to_check
63
71
  return @ips_to_check
64
72
  end
65
73
 
@@ -83,26 +91,57 @@ module NetworkScanner
83
91
  end
84
92
 
85
93
  puts "Checking ips in (#{first_range}).(#{second_range}).(#{third_range}).(#{fourth_range})"
86
- @ips = @ips_to_check
87
94
  return @ips_to_check
88
95
  end
89
96
 
90
- def get_ips
91
- raise Exception.new("Must specify an interface to get ips to check") unless @ips_to_check
97
+ def get_cache_ips(file)
98
+ @ips_to_check = []
99
+
100
+ if text?
101
+ File.open(file).each_line do |ip|
102
+ @ips_to_check << ip
103
+ end
104
+ elsif json?
105
+ JSON.parse(File.read(file)).each do |ip|
106
+ @ips_to_check << ip
107
+ end
108
+ end
109
+ return @ips_to_check
110
+ end
111
+
112
+ def check_pings
113
+ raise Exception.new("Must specify ips to check(interface/range/cache)") unless @ips_to_check
92
114
 
93
115
  @ips = []
94
116
 
95
117
  pool = Thread.pool(@pool_size)
96
118
 
119
+ if self.output_name
120
+ out = File.open(self.output_name, 'w')
121
+ else
122
+ out = STDOUT
123
+ end
124
+
97
125
  @ips_to_check.each do |ip|
98
126
  pool.process do
99
127
  # For some reason &> isn't working, so pipe stdout and then stderr
100
- @ips << ip if system("ping -c 1 #{ip} >> /dev/null 2> /dev/null")
128
+ if system("ping -c 1 #{ip} >> /dev/null 2> /dev/null")
129
+ @ips << ip
130
+ if text?
131
+ out.print "#{ip}\n"
132
+ end
133
+ end
101
134
  end
102
135
  end
103
136
 
104
137
  pool.shutdown
105
138
 
139
+ if json?
140
+ out.puts(JSON.pretty_generate(@ips))
141
+ end
142
+
143
+ out.close unless out == STDOUT
144
+
106
145
  return @ips
107
146
  end
108
147
 
@@ -117,30 +156,40 @@ module NetworkScanner
117
156
  end
118
157
 
119
158
  def check_ports(port)
120
- raise Exception.new("Must scan ips first (Specify an interface or cacheread)") unless @ips
159
+ raise Exception.new("Must scan ips first (Specify an interface or cacheread)") unless @ips_to_check
121
160
 
122
- puts "Checking for ports out of a total of #{@ips.length} ips"
161
+ puts "Checking for ports out of a total of #{@ips_to_check.length} ips"
162
+
163
+ if self.output_name
164
+ out = File.open(self.output_name, 'w')
165
+ else
166
+ out = STDOUT
167
+ end
123
168
 
124
169
  pool = Thread.pool(@pool_size)
125
170
 
126
- @ips.each do |ip|
171
+ @ips = []
172
+
173
+ @ips_to_check.each do |ip|
127
174
  pool.process do
128
- puts ip if port_open?(ip, port)
175
+ if port_open?(ip, port)
176
+ @ips << ip
177
+ if text?
178
+ out.print "#{ip}\n"
179
+ end
180
+ end
129
181
  end
130
182
  end
131
183
 
132
184
  pool.shutdown
133
- end
134
185
 
135
- def cache(file)
136
- raise Exception.new("Must scan ips first (Specify an interface or cacheread)") unless @ips
137
- get_ips
186
+ if json?
187
+ out.puts(JSON.pretty_generate(@ips))
188
+ end
138
189
 
139
- File.open(file, 'w'){|f| f.puts(JSON.pretty_generate(@ips))}
140
- end
190
+ out.close unless out == STDOUT
141
191
 
142
- def cacheread(file)
143
- @ips = JSON.parse(File.read(file))
192
+ return @ips
144
193
  end
145
194
  end
146
195
  end
@@ -1,3 +1,3 @@
1
1
  module NetworkScanner
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: network_scanner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Neyer