rping 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +10 -0
- data/lib/rping.rb +30 -18
- 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
|
|
data/lib/rping.rb
CHANGED
@@ -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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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