network_scanner 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/bin/network_scanner +54 -0
- data/lib/network_scanner/version.rb +3 -0
- data/lib/network_scanner.rb +121 -0
- data/network_scanner.gemspec +24 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: df85f302beef579114f5b023815eeac21506567a
|
4
|
+
data.tar.gz: 36ee79cbda3628ae020fd95085ef1f2b5ea69eb7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a41a44d4497ff35480e274e79ccc4d209ef985767fd0322df2b75d40d5dd97e98fde2c9173b8e5ed32abd92a7b248b656db746b46ab8c4e35da82b6325ebd7bb
|
7
|
+
data.tar.gz: 7354e64a16f28de01448fa4cb5b0e630d87fe8491fa8a921d17ba135c02b8a7793074cc689bebd25743f19b073c388bd0b3196fbdd65fb6b0eb68f7b6ea42c52
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Aaron Neyer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# NetworkScanner
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'network_scanner'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install network_scanner
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/network_scanner
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'network_scanner'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.on('--cache FILE', String, 'Caches the IPs on the network in FILE') do |file|
|
9
|
+
options[:cache] = file
|
10
|
+
end
|
11
|
+
|
12
|
+
opts.on('--interface INTERFACE', String, 'Specifys interface to scan') do |interface|
|
13
|
+
options[:interface] = interface
|
14
|
+
end
|
15
|
+
|
16
|
+
opts.on('--portscan PORT', Integer, 'Port to scan for') do |port|
|
17
|
+
options[:port] = port
|
18
|
+
end
|
19
|
+
|
20
|
+
opts.on('--cacheread FILE', String, 'Cache to read ips from') do |file|
|
21
|
+
options[:cacheread] = file
|
22
|
+
end
|
23
|
+
|
24
|
+
opts.on('--poolsize POOLSIZE', Integer, 'Specify thread pool size (default 100)') do |pool|
|
25
|
+
options[:pool] = pool
|
26
|
+
end
|
27
|
+
|
28
|
+
opts.on_tail('--help', 'Show help message') do
|
29
|
+
puts opts
|
30
|
+
exit
|
31
|
+
end
|
32
|
+
end.parse!
|
33
|
+
|
34
|
+
|
35
|
+
network_scanner = NetworkScanner.new
|
36
|
+
if options[:pool]
|
37
|
+
network_scanner.pool_size = options[:pool]
|
38
|
+
end
|
39
|
+
|
40
|
+
if options[:interface]
|
41
|
+
network_scanner.get_ips(options[:interface])
|
42
|
+
end
|
43
|
+
|
44
|
+
if options[:cache]
|
45
|
+
network_scanner.cache(options[:cache])
|
46
|
+
end
|
47
|
+
|
48
|
+
if options[:cacheread]
|
49
|
+
network_scanner.cacheread(options[:cacheread])
|
50
|
+
end
|
51
|
+
|
52
|
+
if options[:port]
|
53
|
+
network_scanner.check_ports(options[:port])
|
54
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require "network_scanner/version"
|
2
|
+
require 'json'
|
3
|
+
require 'socket'
|
4
|
+
require 'timeout'
|
5
|
+
require 'thread/pool'
|
6
|
+
require 'thread'
|
7
|
+
|
8
|
+
module Enumerable
|
9
|
+
def index_by
|
10
|
+
if block_given?
|
11
|
+
Hash[map { |elem| [yield(elem), elem] }]
|
12
|
+
else
|
13
|
+
to_enum :index_by
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module NetworkScanner
|
19
|
+
class << self
|
20
|
+
# Avoids the user having to deal with namespacing. This is probably hacky
|
21
|
+
def new(*args)
|
22
|
+
Scanner.new(*args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Scanner
|
27
|
+
attr_accessor :pool_size
|
28
|
+
|
29
|
+
def initialize(opts = {})
|
30
|
+
@pool_size = 100
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_ips(interface)
|
34
|
+
ifconfig = `ifconfig`.split("\n\n").index_by{|x| x[/\w+/,0]}
|
35
|
+
inet = ifconfig[interface][/inet addr:([^\s]*)/, 1].split('.')
|
36
|
+
broadcast = ifconfig[interface][/Bcast:([^\s]*)/, 1].split('.')
|
37
|
+
mask = ifconfig[interface][/Mask:([^\s]*)/, 1].split('.')
|
38
|
+
|
39
|
+
start_first = inet[0].to_i & mask[0].to_i
|
40
|
+
start_second = inet[1].to_i & mask[1].to_i
|
41
|
+
start_third = inet[2].to_i & mask[2].to_i
|
42
|
+
start_fourth = inet[3].to_i & mask[3].to_i
|
43
|
+
|
44
|
+
first_range = start_first..broadcast[0].to_i
|
45
|
+
second_range = start_second..broadcast[1].to_i
|
46
|
+
third_range = start_third..broadcast[2].to_i
|
47
|
+
fourth_range = start_fourth..broadcast[3].to_i
|
48
|
+
|
49
|
+
@ips = []
|
50
|
+
|
51
|
+
pool = Thread.pool(@pool_size)
|
52
|
+
|
53
|
+
ips_to_check = []
|
54
|
+
|
55
|
+
first_range.each do |first|
|
56
|
+
second_range.each do |second|
|
57
|
+
third_range.each do |third|
|
58
|
+
fourth_range.each do |fourth|
|
59
|
+
ips_to_check << "#{first}.#{second}.#{third}.#{fourth}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
puts "Checking for ips in (#{first_range}).(#{second_range}).(#{third_range}).(#{fourth_range})"
|
66
|
+
ips_to_check.each do |ip|
|
67
|
+
pool.process do
|
68
|
+
@ips << ip if system("ping -c 1 #{ip} >> /dev/null")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
pool.shutdown
|
73
|
+
|
74
|
+
return @ips
|
75
|
+
end
|
76
|
+
|
77
|
+
def port_open?(ip, port)
|
78
|
+
Timeout::timeout(0.5) do
|
79
|
+
s = TCPSocket.new(ip,port)
|
80
|
+
s.close
|
81
|
+
return true
|
82
|
+
end
|
83
|
+
rescue Timeout::Error
|
84
|
+
return false
|
85
|
+
end
|
86
|
+
|
87
|
+
def check_ports(port)
|
88
|
+
queue = Queue.new
|
89
|
+
|
90
|
+
puts "Checking for ports out of a total of #{@ips.length} ips"
|
91
|
+
|
92
|
+
@ips.each do |ip|
|
93
|
+
queue << ip
|
94
|
+
end
|
95
|
+
|
96
|
+
pool = Thread.pool(200)
|
97
|
+
|
98
|
+
until(queue.empty?)
|
99
|
+
pool.process do
|
100
|
+
unless queue.empty?
|
101
|
+
ip = queue.pop
|
102
|
+
puts ip if port_open?(ip, port)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
pool.shutdown unless pool.done?
|
108
|
+
end
|
109
|
+
|
110
|
+
def cache(file)
|
111
|
+
if !@ips
|
112
|
+
raise Exception.new("Must scan ips before caching (Specify an interface)")
|
113
|
+
end
|
114
|
+
File.open(file, 'w'){|f| f.puts(JSON.pretty_generate(@ips))}
|
115
|
+
end
|
116
|
+
|
117
|
+
def cacheread(file)
|
118
|
+
@ips = JSON.parse(File.read(file))
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'network_scanner/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "network_scanner"
|
8
|
+
spec.version = NetworkScanner::VERSION
|
9
|
+
spec.authors = ["Aaron Neyer"]
|
10
|
+
spec.email = ["aaronneyer@gmail.com"]
|
11
|
+
spec.description = %q{Various Network Scanning Utilities}
|
12
|
+
spec.summary = %q{Scans a network for machines that are up and ports that are open}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_dependency 'thread'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: network_scanner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Neyer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thread
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Various Network Scanning Utilities
|
56
|
+
email:
|
57
|
+
- aaronneyer@gmail.com
|
58
|
+
executables:
|
59
|
+
- network_scanner
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/network_scanner
|
69
|
+
- lib/network_scanner.rb
|
70
|
+
- lib/network_scanner/version.rb
|
71
|
+
- network_scanner.gemspec
|
72
|
+
homepage: ''
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Scans a network for machines that are up and ports that are open
|
96
|
+
test_files: []
|