knoc-knoc 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +17 -0
- data/LICENSE +20 -0
- data/README.markdown +31 -0
- data/Rakefile +9 -0
- data/VERSION.yml +5 -0
- data/knoc-knoc.gemspec +25 -0
- data/lib/knoc-knoc.rb +42 -0
- data/lib/knoc-knoc/host.rb +32 -0
- data/lib/knoc-knoc/version.rb +3 -0
- data/lib/knoc-knoc/watchman.rb +83 -0
- data/lib/knoc-knoc/watchman_live.rb +31 -0
- data/lib/knoc-knoc/watchman_monitor.rb +32 -0
- metadata +96 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 lex148
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# knoc-knoc, a gem to tell you who is there
|
2
|
+
|
3
|
+
KnocKnoc will scan your network and tell you who is on it.
|
4
|
+
It will tell you when someone new arrives and when someone leaves
|
5
|
+
|
6
|
+
##About KnocKnoc
|
7
|
+
|
8
|
+
KnocKnoc has two modes live and monitor.
|
9
|
+
|
10
|
+
Monitor mode scans your network in the background. if you ask KnocKnoc whos there in monitor mode, it will only tell you about who it knows about right then.
|
11
|
+
|
12
|
+
Live mode only scans when you ask about the network. when in live mode you are guaranteed a fill list of people on your network, however you will have to wait for it.
|
13
|
+
|
14
|
+
###Switching Modes
|
15
|
+
|
16
|
+
- KnocKnoc::mode = KnocKnoc::WatchmanMonitor
|
17
|
+
- KnocKnoc::mode = KnocKnoc::WatchmanLive
|
18
|
+
|
19
|
+
|
20
|
+
##Using KnocKnoc
|
21
|
+
|
22
|
+
To use KnocKnoc you ask it who is there
|
23
|
+
|
24
|
+
require 'knoc-knoc'
|
25
|
+
|
26
|
+
- KnocKnoc::whos_there => [list of people on your network]
|
27
|
+
- KnocKnoc::whos_new => [list of the new people on your network]
|
28
|
+
- KnocKnoc::whos_left => [list of the people that have left your network]
|
29
|
+
|
30
|
+
|
31
|
+
|
data/Rakefile
ADDED
data/knoc-knoc.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
#require "knoc-knoc/version"
|
4
|
+
require File.expand_path("../lib/knoc-knoc/version", __FILE__)
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "knoc-knoc"
|
8
|
+
s.version = KnocKnoc::VERSION
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.authors = ["Lex Childs"]
|
11
|
+
s.email = ["lex@childs.me"]
|
12
|
+
s.homepage = %q{http://github.com/lex148/knoc-knoc}
|
13
|
+
s.summary = %q{A library to tell you who is there.}
|
14
|
+
|
15
|
+
|
16
|
+
s.rubyforge_project = "knoc-knoc"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
s.add_dependency 'net-ping', ">= 1.3.7"
|
24
|
+
|
25
|
+
end
|
data/lib/knoc-knoc.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'net/ping'
|
4
|
+
require 'socket'
|
5
|
+
|
6
|
+
require File.dirname(__FILE__) + '/knoc-knoc/host'
|
7
|
+
require File.dirname(__FILE__) + '/knoc-knoc/watchman'
|
8
|
+
require File.dirname(__FILE__) + '/knoc-knoc/watchman_live'
|
9
|
+
require File.dirname(__FILE__) + '/knoc-knoc/watchman_monitor'
|
10
|
+
|
11
|
+
module KnocKnoc
|
12
|
+
module_function
|
13
|
+
|
14
|
+
def mode
|
15
|
+
@watchman.class
|
16
|
+
end
|
17
|
+
|
18
|
+
def mode= value
|
19
|
+
if value.class == Class
|
20
|
+
@watchman.cleanup
|
21
|
+
@watchman = value.new
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
#set the default mode
|
26
|
+
@watchman = WatchmanMonitor.new
|
27
|
+
#@watchman = WatchmanLive.new
|
28
|
+
|
29
|
+
def whos_there
|
30
|
+
@watchman.whos_there
|
31
|
+
end
|
32
|
+
|
33
|
+
def whos_new
|
34
|
+
@watchman.whos_new
|
35
|
+
end
|
36
|
+
|
37
|
+
def whos_left
|
38
|
+
@watchman.whos_left
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module KnocKnoc
|
2
|
+
|
3
|
+
class Host
|
4
|
+
|
5
|
+
#@ip, @hostname, @mac = nil, nil, nil
|
6
|
+
attr_reader :ip, :hostname, :mac
|
7
|
+
|
8
|
+
def initialize(address)
|
9
|
+
@ip = address
|
10
|
+
@hostname = get_hostname(address)
|
11
|
+
@mac = get_mac(address)
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def get_hostname ip
|
18
|
+
Socket::getaddrinfo(ip,nil)[0][2]
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_mac ip
|
22
|
+
mac = /([a-zA-Z0-9]+:)+([a-zA-Z0-9]+)/.match(`arp #{ip}`).to_s
|
23
|
+
mac = mac.split /:/
|
24
|
+
mac = mac.map{ |p| '%02s' % p }
|
25
|
+
mac = mac.map{ |p| p.gsub(' ', '0' ) }
|
26
|
+
mac = mac.join(':')
|
27
|
+
mac.upcase
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module KnocKnoc
|
2
|
+
|
3
|
+
class Watchman
|
4
|
+
|
5
|
+
attr_reader :whos_there, :whos_left, :whos_new
|
6
|
+
|
7
|
+
def self.local_ip
|
8
|
+
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
|
9
|
+
UDPSocket.open do |s|
|
10
|
+
s.connect '172.0.0.1', 1
|
11
|
+
s.addr.last
|
12
|
+
end
|
13
|
+
ensure
|
14
|
+
Socket.do_not_reverse_lookup = orig
|
15
|
+
end
|
16
|
+
|
17
|
+
SUBNET = /([0-9]+\.){3}/.match(self.local_ip).to_s
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@whos_there, @whos_new, @whos_left = [], [], []
|
21
|
+
end
|
22
|
+
|
23
|
+
def whos_new
|
24
|
+
list = @whos_new
|
25
|
+
@whos_new = []
|
26
|
+
list
|
27
|
+
end
|
28
|
+
|
29
|
+
def whos_left
|
30
|
+
list = @whos_left
|
31
|
+
@whos_left = []
|
32
|
+
list
|
33
|
+
end
|
34
|
+
|
35
|
+
def refresh_lists
|
36
|
+
current_ips = self.class.ping_all
|
37
|
+
whos_there_ips = @whos_there.map{|h| h.ip }
|
38
|
+
new_ips = current_ips - whos_there_ips
|
39
|
+
left_ips = whos_there_ips - current_ips
|
40
|
+
self.update_new_ips new_ips
|
41
|
+
self.update_whos_left left_ips
|
42
|
+
end
|
43
|
+
|
44
|
+
def update_new_ips new_ips
|
45
|
+
new_ips.each do |ip|
|
46
|
+
host = Host.new(ip)
|
47
|
+
@whos_there.push host
|
48
|
+
@whos_new.push host
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def update_whos_left left_ips
|
53
|
+
left_ips.each do |ip|
|
54
|
+
host = @whos_there.find{ |x| x.ip == ip }
|
55
|
+
@whos_there = @whos_there - [host]
|
56
|
+
@whos_left.push host
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.ping_all
|
61
|
+
threads, list = [], []
|
62
|
+
(1..254).select do |num|
|
63
|
+
threads << Thread.new(num) do |n|
|
64
|
+
ip = SUBNET + n.to_s
|
65
|
+
if Net::Ping::External.new(ip, 1).ping?
|
66
|
+
#if ping ip
|
67
|
+
list.push( ip )
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
threads.each { |th| th.join() }
|
72
|
+
list
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.ping ip
|
76
|
+
!!(/1 received/.match(`ping #{ip} -c 1 -w 1`))
|
77
|
+
end
|
78
|
+
|
79
|
+
def cleanup
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module KnocKnoc
|
2
|
+
|
3
|
+
class WatchmanLive < Watchman
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
super
|
7
|
+
Thread.new() do
|
8
|
+
refresh_lists
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def whos_new
|
13
|
+
refresh_lists
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def whos_left
|
18
|
+
refresh_lists
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
def whos_there
|
23
|
+
refresh_lists
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
def cleanup
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module KnocKnoc
|
2
|
+
|
3
|
+
class WatchmanMonitor < Watchman
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
super
|
7
|
+
@running = true
|
8
|
+
#run the refresh_lists once before
|
9
|
+
#starting the set interval
|
10
|
+
Thread.new() do
|
11
|
+
self.refresh_lists
|
12
|
+
self.repeat_every(30) do
|
13
|
+
self.refresh_lists
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def repeat_every(interval, &block)
|
19
|
+
while @running
|
20
|
+
start_time = Time.now
|
21
|
+
Thread.new(&block).join
|
22
|
+
elapsed = Time.now - start_time
|
23
|
+
sleep([interval - elapsed, 0].max)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def cleanup
|
28
|
+
@running = false
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: knoc-knoc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Lex Childs
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-15 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: net-ping
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 21
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 3
|
33
|
+
- 7
|
34
|
+
version: 1.3.7
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description:
|
38
|
+
email:
|
39
|
+
- lex@childs.me
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- LICENSE
|
51
|
+
- README.markdown
|
52
|
+
- Rakefile
|
53
|
+
- VERSION.yml
|
54
|
+
- knoc-knoc.gemspec
|
55
|
+
- lib/knoc-knoc.rb
|
56
|
+
- lib/knoc-knoc/host.rb
|
57
|
+
- lib/knoc-knoc/version.rb
|
58
|
+
- lib/knoc-knoc/watchman.rb
|
59
|
+
- lib/knoc-knoc/watchman_live.rb
|
60
|
+
- lib/knoc-knoc/watchman_monitor.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: http://github.com/lex148/knoc-knoc
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project: knoc-knoc
|
91
|
+
rubygems_version: 1.3.7
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: A library to tell you who is there.
|
95
|
+
test_files: []
|
96
|
+
|