rping 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +10 -0
  2. data/lib/rping.rb +30 -18
  3. metadata +3 -3
data/README CHANGED
@@ -37,6 +37,16 @@ https://bitbucket.org/winebarrel/rping
37
37
  p reply
38
38
  #=> {:time=>0.0, :src=>"127.0.0.1", :dest=>"127.0.0.1", :ttl=>128, :size=>36, :seq=>1}
39
39
  end
40
+
41
+ dests = (1..255).map {|i| "127.0.0.#{i}" }
42
+ p RPing.multi_ping(dests, :timeout => 0.3)
43
+ #=> {"127.0.0.1" => [{:time=>0.0, :dest=>"127.0.0.1", :src=>"127.0.0.1", :ttl=>128, :size=>36, :seq=>1}],
44
+ # "127.0.0.2" => [nil],
45
+ # "127.0.0.3" => [nil],
46
+ # ...
47
+
48
+ pp RPing.multi_ping(dest, :timeout => 0.3)
49
+
40
50
 
41
51
  == Reference Documents
42
52
 
@@ -1,5 +1,4 @@
1
1
  require 'socket'
2
- require 'timeout'
3
2
 
4
3
  class RPing
5
4
  MAX_LEN = 64 * 1024
@@ -16,6 +15,21 @@ class RPing
16
15
  self.new(options).ping(dest, &block)
17
16
  end
18
17
 
18
+ def self.multi_ping(dests, options = {})
19
+ ths = []
20
+ reply_h = {}
21
+
22
+ dests.each do |dest|
23
+ ths << Thread.start {
24
+ reply_h[dest] = RPing.ping(dest, options)
25
+ }
26
+ end
27
+
28
+ ths.each {|th| th.join }
29
+
30
+ return reply_h
31
+ end
32
+
19
33
  def ping(addr, &block)
20
34
  unless addr =~ /\A\d{3}\.\d{3}\.\d{3}\.\d{3}\Z/
21
35
  addr = IPSocket.getaddress(addr)
@@ -65,23 +79,21 @@ class RPing
65
79
  reply = nil
66
80
 
67
81
  begin
68
- timeout(@timeout) do
69
- if select([sock], nil, nil, @timeout)
70
- msg = sock.recv(MAX_LEN)
71
- recv_time = Time.now.to_f
72
- ip, icmp = unpack_echo_reply(msg)
73
-
74
- # icmp[0] == 0: Type == Echo Reply
75
- if icmp[0] == 0 and icmp[3] == @icmp_id
76
- reply = {
77
- :dest => ip[8].bytes.to_a.join('.'),
78
- :src => ip[9].bytes.to_a.join('.'),
79
- :size => msg.length,
80
- :ttl => ip[5],
81
- :seq => icmp[4],
82
- :time => (recv_time - icmp[5]) * 1000,
83
- }
84
- end
82
+ if select([sock], nil, nil, @timeout)
83
+ msg = sock.recv(MAX_LEN)
84
+ recv_time = Time.now.to_f
85
+ ip, icmp = unpack_echo_reply(msg)
86
+
87
+ # icmp[0] == 0: Type == Echo Reply
88
+ if icmp[0] == 0 and icmp[3] == @icmp_id
89
+ reply = {
90
+ :dest => ip[8].bytes.to_a.join('.'),
91
+ :src => ip[9].bytes.to_a.join('.'),
92
+ :size => msg.length,
93
+ :ttl => ip[5],
94
+ :seq => icmp[4],
95
+ :time => (recv_time - icmp[5]) * 1000,
96
+ }
85
97
  end
86
98
  end
87
99
  rescue Timeout::Error
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rping
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - winebarrel