ipscanner 0.1.3 → 0.1.4

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.
Files changed (2) hide show
  1. data/lib/ipscanner.rb +18 -3
  2. metadata +4 -4
data/lib/ipscanner.rb CHANGED
@@ -2,20 +2,20 @@
2
2
 
3
3
  # file: ipscanner.rb
4
4
 
5
- require 'net/ping'
5
+ require 'socket'
6
6
 
7
7
  class IPScanner
8
8
 
9
9
  def initialize(ip_base='192.168.1.')
10
10
  @ip_base = ip_base
11
11
  @a = []
12
- threads = (1..254).map{|i| Thread.new {ip = ip_base + i.to_s; @a << i if Net::Ping::TCP.new(ip, 'http').ping?}}
12
+ threads = (1..254).map{|i| Thread.new {ip = ip_base + i.to_s; @a << i if pingecho(ip, 1)}}
13
13
  threads.join
14
14
  end
15
15
 
16
16
  def self.run(ip_base='192.168.1.')
17
17
  a = []
18
- threads = (1..254).map{|i| Thread.new {ip = ip_base + i.to_s; a << i if Net::Ping::TCP.new(ip, 'http').ping?}}
18
+ threads = (1..254).map{|i| Thread.new {ip = ip_base + i.to_s; a << i if pingecho(ip_base + i.to_s, 1) }}
19
19
  threads.join
20
20
  a.sort.map{|x| ip_base + x.to_s}
21
21
  end
@@ -25,4 +25,19 @@ class IPScanner
25
25
  def to_a()
26
26
  @a.sort.map{|x| @ip_base + x.to_s}
27
27
  end
28
+
29
+ def self.pingecho(host, timeout=5, service="echo")
30
+ begin
31
+ timeout(timeout) do
32
+ s = TCPSocket.new(host, service)
33
+ s.close
34
+ end
35
+ rescue Errno::ECONNREFUSED
36
+ return true
37
+ rescue Timeout::Error, StandardError
38
+ return false
39
+ end
40
+ return true
41
+ end
42
+
28
43
  end
metadata CHANGED
@@ -2,15 +2,15 @@
2
2
  name: ipscanner
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
- authors: []
8
-
7
+ authors:
8
+ - James Robertson
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-17 00:00:00 +01:00
13
+ date: 2011-12-09 00:00:00 +00:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency