network_scanner 0.0.1 → 0.0.2

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: df85f302beef579114f5b023815eeac21506567a
4
- data.tar.gz: 36ee79cbda3628ae020fd95085ef1f2b5ea69eb7
3
+ metadata.gz: 523886c0cc9e935ee6629722850bf2aac37a2a94
4
+ data.tar.gz: a249a6aba70f6c99e6ac6ebb67e6a905f84c9afb
5
5
  SHA512:
6
- metadata.gz: a41a44d4497ff35480e274e79ccc4d209ef985767fd0322df2b75d40d5dd97e98fde2c9173b8e5ed32abd92a7b248b656db746b46ab8c4e35da82b6325ebd7bb
7
- data.tar.gz: 7354e64a16f28de01448fa4cb5b0e630d87fe8491fa8a921d17ba135c02b8a7793074cc689bebd25743f19b073c388bd0b3196fbdd65fb6b0eb68f7b6ea42c52
6
+ metadata.gz: 26091157cbe2b73b7faf985f049f8fe96d3dbe712a4ee487d59eb9a933d60459b6afa12b252c5c30ae8d56c868e6d73cd6d5a815be86f3fd093aeb16cb524af1
7
+ data.tar.gz: 618b76d95a7e2558ab2c8a80cc7fccc6457410b5670753320d8c495c703d5e306a7531233f135089cc40bbea8b4bf50a637885412e9ad1283aeb313f655f8b78
data/bin/network_scanner CHANGED
@@ -5,7 +5,7 @@ require 'optparse'
5
5
 
6
6
  options = {}
7
7
  OptionParser.new do |opts|
8
- opts.on('--cache FILE', String, 'Caches the IPs on the network in FILE') do |file|
8
+ opts.on('--cache FILE', String, 'Caches the IPs up on the network in FILE for quicker scanning') do |file|
9
9
  options[:cache] = file
10
10
  end
11
11
 
@@ -13,6 +13,10 @@ OptionParser.new do |opts|
13
13
  options[:interface] = interface
14
14
  end
15
15
 
16
+ opts.on('--range RANGE', String, 'Specify a range to scan(192.168.1.100-192.168.1.200)') do |range|
17
+ options[:range] = range
18
+ end
19
+
16
20
  opts.on('--portscan PORT', Integer, 'Port to scan for') do |port|
17
21
  options[:port] = port
18
22
  end
@@ -38,7 +42,11 @@ if options[:pool]
38
42
  end
39
43
 
40
44
  if options[:interface]
41
- network_scanner.get_ips(options[:interface])
45
+ network_scanner.get_interface_ips(options[:interface])
46
+ end
47
+
48
+ if options[:range]
49
+ network_scanner.get_range_ips(options[:range])
42
50
  end
43
51
 
44
52
  if options[:cache]
@@ -1,3 +1,3 @@
1
1
  module NetworkScanner
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -17,7 +17,7 @@ end
17
17
 
18
18
  module NetworkScanner
19
19
  class << self
20
- # Avoids the user having to deal with namespacing. This is probably hacky
20
+ # Prevents the user from having to deal with namespacing. This is probably hacky
21
21
  def new(*args)
22
22
  Scanner.new(*args)
23
23
  end
@@ -30,7 +30,7 @@ module NetworkScanner
30
30
  @pool_size = 100
31
31
  end
32
32
 
33
- def get_ips(interface)
33
+ def get_interface_ips(interface)
34
34
  ifconfig = `ifconfig`.split("\n\n").index_by{|x| x[/\w+/,0]}
35
35
  inet = ifconfig[interface][/inet addr:([^\s]*)/, 1].split('.')
36
36
  broadcast = ifconfig[interface][/Bcast:([^\s]*)/, 1].split('.')
@@ -45,27 +45,59 @@ module NetworkScanner
45
45
  second_range = start_second..broadcast[1].to_i
46
46
  third_range = start_third..broadcast[2].to_i
47
47
  fourth_range = start_fourth..broadcast[3].to_i
48
+
49
+ @ips_to_check = []
48
50
 
49
- @ips = []
51
+ first_range.each do |first|
52
+ second_range.each do |second|
53
+ third_range.each do |third|
54
+ fourth_range.each do |fourth|
55
+ @ips_to_check << "#{first}.#{second}.#{third}.#{fourth}"
56
+ end
57
+ end
58
+ end
59
+ end
50
60
 
51
- pool = Thread.pool(@pool_size)
61
+ puts "Checking ips in (#{first_range}).(#{second_range}).(#{third_range}).(#{fourth_range})"
62
+ @ips = @ips_to_check
63
+ return @ips_to_check
64
+ end
52
65
 
53
- ips_to_check = []
66
+ def get_range_ips(range)
67
+ start, finish = range.split('-', 2).map{|ip| ip.split('.')}
68
+ first_range = start[0]..finish[0]
69
+ second_range = start[1]..finish[1]
70
+ third_range = start[2]..finish[2]
71
+ fourth_range = start[3]..finish[3]
72
+
73
+ @ips_to_check = []
54
74
 
55
75
  first_range.each do |first|
56
76
  second_range.each do |second|
57
77
  third_range.each do |third|
58
78
  fourth_range.each do |fourth|
59
- ips_to_check << "#{first}.#{second}.#{third}.#{fourth}"
79
+ @ips_to_check << "#{first}.#{second}.#{third}.#{fourth}"
60
80
  end
61
81
  end
62
82
  end
63
83
  end
64
84
 
65
- puts "Checking for ips in (#{first_range}).(#{second_range}).(#{third_range}).(#{fourth_range})"
66
- ips_to_check.each do |ip|
85
+ puts "Checking ips in (#{first_range}).(#{second_range}).(#{third_range}).(#{fourth_range})"
86
+ @ips = @ips_to_check
87
+ return @ips_to_check
88
+ end
89
+
90
+ def get_ips
91
+ raise Exception.new("Must specify an interface to get ips to check") unless @ips_to_check
92
+
93
+ @ips = []
94
+
95
+ pool = Thread.pool(@pool_size)
96
+
97
+ @ips_to_check.each do |ip|
67
98
  pool.process do
68
- @ips << ip if system("ping -c 1 #{ip} >> /dev/null")
99
+ # 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")
69
101
  end
70
102
  end
71
103
 
@@ -85,32 +117,25 @@ module NetworkScanner
85
117
  end
86
118
 
87
119
  def check_ports(port)
88
- queue = Queue.new
120
+ raise Exception.new("Must scan ips first (Specify an interface or cacheread)") unless @ips
89
121
 
90
122
  puts "Checking for ports out of a total of #{@ips.length} ips"
91
123
 
92
- @ips.each do |ip|
93
- queue << ip
94
- end
95
-
96
- pool = Thread.pool(200)
124
+ pool = Thread.pool(@pool_size)
97
125
 
98
- until(queue.empty?)
126
+ @ips.each do |ip|
99
127
  pool.process do
100
- unless queue.empty?
101
- ip = queue.pop
102
- puts ip if port_open?(ip, port)
103
- end
128
+ puts ip if port_open?(ip, port)
104
129
  end
105
130
  end
106
131
 
107
- pool.shutdown unless pool.done?
132
+ pool.shutdown
108
133
  end
109
134
 
110
135
  def cache(file)
111
- if !@ips
112
- raise Exception.new("Must scan ips before caching (Specify an interface)")
113
- end
136
+ raise Exception.new("Must scan ips first (Specify an interface or cacheread)") unless @ips
137
+ get_ips
138
+
114
139
  File.open(file, 'w'){|f| f.puts(JSON.pretty_generate(@ips))}
115
140
  end
116
141
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: network_scanner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Neyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-23 00:00:00.000000000 Z
11
+ date: 2013-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler