net-ping 1.6.2-universal-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/CHANGES +287 -0
- data/Gemfile +2 -0
- data/MANIFEST +25 -0
- data/README +54 -0
- data/Rakefile +94 -0
- data/doc/ping.txt +274 -0
- data/examples/example_pingexternal.rb +16 -0
- data/examples/example_pinghttp.rb +22 -0
- data/examples/example_pingtcp.rb +16 -0
- data/examples/example_pingudp.rb +12 -0
- data/lib/net/ping.rb +17 -0
- data/lib/net/ping/external.rb +122 -0
- data/lib/net/ping/helper.rb +33 -0
- data/lib/net/ping/http.rb +159 -0
- data/lib/net/ping/icmp.rb +179 -0
- data/lib/net/ping/ping.rb +89 -0
- data/lib/net/ping/tcp.rb +83 -0
- data/lib/net/ping/udp.rb +119 -0
- data/lib/net/ping/wmi.rb +118 -0
- data/net-ping.gemspec +43 -0
- data/test/test_net_ping.rb +35 -0
- data/test/test_net_ping_external.rb +129 -0
- data/test/test_net_ping_http.rb +210 -0
- data/test/test_net_ping_icmp.rb +139 -0
- data/test/test_net_ping_tcp.rb +105 -0
- data/test/test_net_ping_udp.rb +119 -0
- data/test/test_net_ping_wmi.rb +81 -0
- metadata +145 -0
data/doc/ping.txt
ADDED
@@ -0,0 +1,274 @@
|
|
1
|
+
= Description
|
2
|
+
A simple Ruby interface to the 'ping' command.
|
3
|
+
|
4
|
+
= Synopsis
|
5
|
+
require 'net/ping'
|
6
|
+
include Net
|
7
|
+
|
8
|
+
Ping::TCP.service_check = true
|
9
|
+
|
10
|
+
pt = Net::Ping::TCP.new(host)
|
11
|
+
pu = Net::Ping::UDP.new(host)
|
12
|
+
pe = Net::Ping::External.new(host)
|
13
|
+
ph = Net::Ping::HTTP.new(uri)
|
14
|
+
|
15
|
+
if pt.ping
|
16
|
+
puts "TCP ping successful"
|
17
|
+
else
|
18
|
+
puts "TCP ping unsuccessful: " + pt.exception
|
19
|
+
end
|
20
|
+
|
21
|
+
if pu.ping
|
22
|
+
puts "UDP ping successful"
|
23
|
+
else
|
24
|
+
puts "UDP ping unsuccessful: " + pu.exception
|
25
|
+
end
|
26
|
+
|
27
|
+
if pe.ping
|
28
|
+
puts "External ping successful"
|
29
|
+
else
|
30
|
+
puts "External ping unsuccessful: " + pe.exception
|
31
|
+
end
|
32
|
+
|
33
|
+
if ph.ping?
|
34
|
+
puts "HTTP ping successful"
|
35
|
+
else
|
36
|
+
puts "HTTP ping unsuccessful: " + ph.exception
|
37
|
+
end
|
38
|
+
|
39
|
+
= Ping Classes
|
40
|
+
* Ping::TCP
|
41
|
+
* Ping::UDP
|
42
|
+
* Ping::External
|
43
|
+
* Ping::HTTP
|
44
|
+
* Ping::ICMP
|
45
|
+
* Ping::WMI
|
46
|
+
* Ping::LDAP
|
47
|
+
|
48
|
+
All Ping classes are children of the Ping parent class (which should
|
49
|
+
never be instantiated directly).
|
50
|
+
|
51
|
+
The Ping::ICMP class requires root privileges on UNIX systems.
|
52
|
+
|
53
|
+
The Ping::WMI class only works on MS Windows.
|
54
|
+
|
55
|
+
== Net::Ping
|
56
|
+
Net::Ping.new(host=nil, port=7, timeout=5)
|
57
|
+
Creates and returns a new Ping object. If the host is not specified
|
58
|
+
in the constructor then it must be specified in the ping method.
|
59
|
+
|
60
|
+
== Net::Ping::TCP
|
61
|
+
Ping::TCP.service_check
|
62
|
+
Returns the setting for how ECONNREFUSED is handled. By default, this is
|
63
|
+
set to false, i.e. an ECONNREFUSED error is considered a failed ping.
|
64
|
+
|
65
|
+
Ping::TCP.service_check=(bool)
|
66
|
+
Sets the behavior for how ECONNREFUSED is handled. By default, this is
|
67
|
+
set to false, i.e. an ECONNREFUSED error is considered a failed ping.
|
68
|
+
|
69
|
+
Ping::TCP#ping(host=nil)
|
70
|
+
Attempts to open a connection using TCPSocket with a +host+ specified
|
71
|
+
either here or in the constructor. A successful open means the ping was
|
72
|
+
successful and true is returned. Otherwise, false is returned.
|
73
|
+
|
74
|
+
== Net::Ping::UDP
|
75
|
+
Ping::UDP#ping
|
76
|
+
Attempts to open a connection using UDPSocket and sends the value of
|
77
|
+
Ping::UDP#data as a string across the socket. If the return string matches,
|
78
|
+
then the ping was successful and true is returned. Otherwise, false is
|
79
|
+
returned.
|
80
|
+
|
81
|
+
Ping::UDP#data
|
82
|
+
Returns the string that is sent across the UDP socket.
|
83
|
+
|
84
|
+
Ping::UDP#data=(string)
|
85
|
+
Sets the string that is sent across the UDP socket. The default is "ping".
|
86
|
+
Note that the +string+ cannot be larger than MAX_DATA (64 characters).
|
87
|
+
|
88
|
+
== Net::Ping::External
|
89
|
+
Ping::External#ping
|
90
|
+
Uses the 'open3' module and calls your system's local 'ping' command with
|
91
|
+
various options, depending on platform. If nothing is sent to stderr, the
|
92
|
+
ping was successful and true is returned. Otherwise, false is returned.
|
93
|
+
|
94
|
+
The MS Windows platform requires the 'win32-open3' package.
|
95
|
+
|
96
|
+
== Ping::HTTP
|
97
|
+
Ping::HTTP.new(uri=nil, port=80, timeout=5)
|
98
|
+
Identical to Net::Ping.new except that, instead of a host, the first
|
99
|
+
argument is a URI.
|
100
|
+
|
101
|
+
Ping::HTTP#ping
|
102
|
+
Checks for a response against +uri+. As long as kind of Net::HTTPSuccess
|
103
|
+
response is returned, the ping is successful and true is returned.
|
104
|
+
Otherwise, false is returned and Ping::HTTP#exception is set to the error
|
105
|
+
message.
|
106
|
+
|
107
|
+
Note that redirects are automatically followed unless the
|
108
|
+
Ping::HTTP#follow_redirects method is set to false.
|
109
|
+
|
110
|
+
Ping::HTTP#follow_redirect
|
111
|
+
Indicates whether or not a redirect should be followed in a ping attempt.
|
112
|
+
By default this is set to true.
|
113
|
+
|
114
|
+
Ping::HTTP#follow_redirect=(bool)
|
115
|
+
Sets whether or not a redirect should be followed in a ping attempt. If
|
116
|
+
set to false, then any redirect is considered a failed ping.
|
117
|
+
|
118
|
+
Ping::HTTP#uri
|
119
|
+
An alias for Ping::HTTP#host.
|
120
|
+
|
121
|
+
Ping::HTTP#uri=(uri)
|
122
|
+
An alias for Ping::HTTP#host=.
|
123
|
+
|
124
|
+
== Ping::ICMP
|
125
|
+
Ping::ICMP#duration
|
126
|
+
The time it took to ping the host. Not a precise value but a good estimate.
|
127
|
+
|
128
|
+
== Ping::WMI
|
129
|
+
Ping::WMI#ping(host, options={})
|
130
|
+
Unlike other Ping classes, this method returns a PingStatus struct that
|
131
|
+
contains various bits of information about the ping itself. The PingStatus
|
132
|
+
struct is a wrapper for the Win32_PingStatus WMI class.
|
133
|
+
|
134
|
+
In addition, you can pass options that will be interpreted as WQL parameters.
|
135
|
+
|
136
|
+
Ping::WMI#ping?(host, options={})
|
137
|
+
Returns whether or not the ping succeeded.
|
138
|
+
|
139
|
+
== Ping::LDAP
|
140
|
+
Ping::LDAP.new(uri=nil, timeout=5)
|
141
|
+
Performs a 'ping' to an LDAP server in the form of either an anonymous
|
142
|
+
or an authenticated LDAP bind.
|
143
|
+
Identical to Net::Ping.new except that, instead of a host, the first
|
144
|
+
argument is a URI.
|
145
|
+
The default +timeout+ is 5 seconds.
|
146
|
+
|
147
|
+
+uri+ string is expected to be a full URI with scheme (ldap/ldaps)
|
148
|
+
and optionally the port (else default port is assumed) e.g.
|
149
|
+
ldap://my.ldap.host.com
|
150
|
+
ldap://my.ldap.host.com:1389
|
151
|
+
ldaps://my.ldap.host.com
|
152
|
+
ldaps://my.ldap.host.com:6636
|
153
|
+
|
154
|
+
If a plain hostname is provided as the +uri+, a default port of 389 is assumed
|
155
|
+
|
156
|
+
Ping::LDAP#encryption
|
157
|
+
Set/get the encyption method. By default is nil, but may be set to :simple_tls
|
158
|
+
|
159
|
+
Ping::LDAP#username
|
160
|
+
Ping::LDAP#password
|
161
|
+
set/get the username and password for ping using and authenticated bind.
|
162
|
+
|
163
|
+
= Common Instance Methods
|
164
|
+
Ping#exception
|
165
|
+
Returns the error string that was set if a ping call failed. If an exception
|
166
|
+
is raised, it is caught and stored in this attribute. It is not raised in
|
167
|
+
your code.
|
168
|
+
|
169
|
+
This should be nil if the ping succeeded.
|
170
|
+
|
171
|
+
Ping#host
|
172
|
+
Returns the host name that ping attempts will ping against.
|
173
|
+
|
174
|
+
Ping#host=(hostname)
|
175
|
+
Sets the host name that ping attempts will ping against.
|
176
|
+
|
177
|
+
Ping#port
|
178
|
+
Returns the port number that ping attempts will use.
|
179
|
+
|
180
|
+
Ping#port=(port)
|
181
|
+
Set the port number to open socket connections on. The default is 7 (or
|
182
|
+
whatever your 'echo' port is set to). Note that you can also specify a
|
183
|
+
string, such as "http".
|
184
|
+
|
185
|
+
Ping#timeout
|
186
|
+
Returns the amount of time before the timeout module raises a TimeoutError
|
187
|
+
during connection attempts. The default is 5 seconds.
|
188
|
+
|
189
|
+
Ping#timeout=(time)
|
190
|
+
Sets the amount of time before the timeout module raises a TimeoutError.
|
191
|
+
during connection attempts.
|
192
|
+
|
193
|
+
Ping#warning
|
194
|
+
Returns a warning string that was returned during the ping attempt. This
|
195
|
+
typically occurs only in the Ping::External class, or the Ping::HTTP class
|
196
|
+
if a redirect occurred.
|
197
|
+
|
198
|
+
== Notes
|
199
|
+
If a host is down *IT IS CONSIDERED A FAILED PING*, and the 'no answer from
|
200
|
+
+host+' text is assigned to the 'exception' attribute. You may disagree with
|
201
|
+
this behavior, in which case you need merely check the exception attribute
|
202
|
+
against a regex as a simple workaround.
|
203
|
+
|
204
|
+
== Pre-emptive FAQ
|
205
|
+
Q: "Why don't you return exceptions if a connection fails?"
|
206
|
+
|
207
|
+
A: Because ping is only meant to return one of two things - success or
|
208
|
+
failure. It's very simple. If you want to find out *why* the ping
|
209
|
+
failed, you can check the 'exception' attribute.
|
210
|
+
|
211
|
+
Q: "I know the host is alive, but a TCP or UDP ping tells me otherwise. What
|
212
|
+
gives?"
|
213
|
+
|
214
|
+
A: It's possible that the echo port has been disabled on the remote
|
215
|
+
host for security reasons. Your best best is to specify a different port
|
216
|
+
or to use Ping::ICMP or Ping::External instead.
|
217
|
+
|
218
|
+
In the case of UDP pings, they are often actively refused. It may be
|
219
|
+
more pragmatic to set Ping::UDP.service_check = false.
|
220
|
+
|
221
|
+
Q: "Why does a TCP ping return false when I know it should return true?"
|
222
|
+
|
223
|
+
A: By default ECONNREFUSED errors will return a value of false. This is
|
224
|
+
contrary to what most other folks do for TCP pings. The problem with
|
225
|
+
their philosophy is that you can get false positives if a firewall blocks
|
226
|
+
the route to the host. The problem with my philosophy is that you can
|
227
|
+
get false negatives if there is no firewall (or it's not blocking the
|
228
|
+
route). Given the alternatives I chose the latter.
|
229
|
+
|
230
|
+
You can always change the default behavior by using the +service_check+
|
231
|
+
class method.
|
232
|
+
|
233
|
+
A similar situation is true for UDP pings.
|
234
|
+
|
235
|
+
Q: "Couldn't you use traceroute information to tell for sure?"
|
236
|
+
|
237
|
+
A: I could but I won't so don't bug me about it. It's far more effort than
|
238
|
+
it's worth. If you want something like that, please port the
|
239
|
+
Net::Traceroute Perl module by Daniel Hagerty.
|
240
|
+
|
241
|
+
= Known Bugs
|
242
|
+
You may see a test failure from the test_net_ping_tcp test case. You can
|
243
|
+
ignore these.
|
244
|
+
|
245
|
+
The socket library that ships with the Windows One-Click installer has
|
246
|
+
known issues. This may cause the Ping::ICMP class to fail. In fact, I
|
247
|
+
make an effort to skip those tests if I detect the one-click installer.
|
248
|
+
|
249
|
+
UDP pings may not work with older versions of Ruby 1.9.x.
|
250
|
+
|
251
|
+
Please report any bugs on the project page at
|
252
|
+
http://www.rubyforge.org/projects/shards.
|
253
|
+
|
254
|
+
= Acknowledgements
|
255
|
+
The Ping::ICMP#ping method is based largely on the identical method from
|
256
|
+
the Net::Ping Perl module by Rob Brown. Much of the code was ported by
|
257
|
+
Jos Backus on ruby-talk.
|
258
|
+
|
259
|
+
= Future Plans
|
260
|
+
Add support for syn pings.
|
261
|
+
|
262
|
+
= License
|
263
|
+
Artistic 2.0
|
264
|
+
|
265
|
+
= Copyright
|
266
|
+
(C) 2003-2010 Daniel J. Berger, All Rights Reserved
|
267
|
+
|
268
|
+
= Warranty
|
269
|
+
This package is provided "as is" and without any express or
|
270
|
+
implied warranties, including, without limitation, the implied
|
271
|
+
warranties of merchantability and fitness for a particular purpose.
|
272
|
+
|
273
|
+
= Author
|
274
|
+
Daniel J. Berger
|
@@ -0,0 +1,16 @@
|
|
1
|
+
########################################################################
|
2
|
+
# example_pingexternal.rb
|
3
|
+
#
|
4
|
+
# A short sample program demonstrating an external ping. You can run
|
5
|
+
# this program via the example:external task. Modify as you see fit.
|
6
|
+
########################################################################
|
7
|
+
require 'net/ping'
|
8
|
+
|
9
|
+
good = 'www.rubyforge.org'
|
10
|
+
bad = 'foo.bar.baz'
|
11
|
+
|
12
|
+
p1 = Net::Ping::External.new(good)
|
13
|
+
p p1.ping?
|
14
|
+
|
15
|
+
p2 = Net::Ping::External.new(bad)
|
16
|
+
p p2.ping?
|
@@ -0,0 +1,22 @@
|
|
1
|
+
########################################################################
|
2
|
+
# example_pinghttp.rb
|
3
|
+
#
|
4
|
+
# A short sample program demonstrating an http ping. You can run
|
5
|
+
# this program via the example:http task. Modify as you see fit.
|
6
|
+
########################################################################
|
7
|
+
require 'net/ping'
|
8
|
+
|
9
|
+
good = 'http://www.google.com/index.html'
|
10
|
+
bad = 'http://www.ruby-lang.org/index.html'
|
11
|
+
|
12
|
+
puts "== Good ping, no redirect"
|
13
|
+
|
14
|
+
p1 = Net::Ping::HTTP.new(good)
|
15
|
+
p p1.ping?
|
16
|
+
|
17
|
+
puts "== Bad ping"
|
18
|
+
|
19
|
+
p2 = Net::Ping::HTTP.new(bad)
|
20
|
+
p p2.ping?
|
21
|
+
p p2.warning
|
22
|
+
p p2.exception
|
@@ -0,0 +1,16 @@
|
|
1
|
+
########################################################################
|
2
|
+
# example_pingtcp.rb
|
3
|
+
#
|
4
|
+
# A short sample program demonstrating a tcp ping. You can run
|
5
|
+
# this program via the example:tcp task. Modify as you see fit.
|
6
|
+
########################################################################
|
7
|
+
require 'net/ping'
|
8
|
+
|
9
|
+
good = 'www.google.com'
|
10
|
+
bad = 'foo.bar.baz'
|
11
|
+
|
12
|
+
p1 = Net::Ping::TCP.new(good, 'http')
|
13
|
+
p p1.ping?
|
14
|
+
|
15
|
+
p2 = Net::Ping::TCP.new(bad)
|
16
|
+
p p2.ping?
|
@@ -0,0 +1,12 @@
|
|
1
|
+
########################################################################
|
2
|
+
# example_pingudp.rb
|
3
|
+
#
|
4
|
+
# A short sample program demonstrating a UDP ping. You can run
|
5
|
+
# this program via the example:udp task. Modify as you see fit.
|
6
|
+
########################################################################
|
7
|
+
require 'net/ping'
|
8
|
+
|
9
|
+
host = 'www.google.com'
|
10
|
+
|
11
|
+
u = Net::Ping::UDP.new(host)
|
12
|
+
p u.ping?
|
data/lib/net/ping.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# By doing a "require 'net/ping'" you are requiring every subclass. If you
|
2
|
+
# want to require a specific ping type only, do "require 'net/ping/tcp'",
|
3
|
+
# for example.
|
4
|
+
#
|
5
|
+
require 'rbconfig'
|
6
|
+
|
7
|
+
require File.join(File.dirname(__FILE__), 'ping/tcp')
|
8
|
+
require File.join(File.dirname(__FILE__), 'ping/udp')
|
9
|
+
require File.join(File.dirname(__FILE__), 'ping/icmp')
|
10
|
+
require File.join(File.dirname(__FILE__), 'ping/external')
|
11
|
+
require File.join(File.dirname(__FILE__), 'ping/http')
|
12
|
+
|
13
|
+
RbConfig = Config unless Object.const_defined?(:RbConfig)
|
14
|
+
|
15
|
+
if File::ALT_SEPARATOR
|
16
|
+
require File.join(File.dirname(__FILE__), 'ping/wmi')
|
17
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
require 'rbconfig'
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), 'ping')
|
5
|
+
|
6
|
+
if File::ALT_SEPARATOR && RUBY_VERSION.to_f < 1.9 && RUBY_PLATFORM != 'java'
|
7
|
+
require 'win32/open3'
|
8
|
+
else
|
9
|
+
require 'open3'
|
10
|
+
end
|
11
|
+
|
12
|
+
# The Net module serves as a namespace only.
|
13
|
+
module Net
|
14
|
+
|
15
|
+
# The Ping::External class encapsulates methods for external (system) pings.
|
16
|
+
class Ping::External < Ping
|
17
|
+
|
18
|
+
if File::ALT_SEPARATOR
|
19
|
+
extend FFI::Library
|
20
|
+
ffi_lib 'kernel32'
|
21
|
+
|
22
|
+
attach_function :SetConsoleCP, [:uint], :bool
|
23
|
+
attach_function :GetConsoleCP, [], :uint
|
24
|
+
end
|
25
|
+
|
26
|
+
# Pings the host using your system's ping utility and checks for any
|
27
|
+
# errors or warnings. Returns true if successful, or false if not.
|
28
|
+
#
|
29
|
+
# If the ping failed then the Ping::External#exception method should
|
30
|
+
# contain a string indicating what went wrong. If the ping succeeded then
|
31
|
+
# the Ping::External#warning method may or may not contain a value.
|
32
|
+
#
|
33
|
+
def ping(host = @host)
|
34
|
+
super(host)
|
35
|
+
|
36
|
+
stdin = stdout = stderr = nil
|
37
|
+
pstring = "ping "
|
38
|
+
bool = false
|
39
|
+
orig_cp = nil
|
40
|
+
|
41
|
+
case RbConfig::CONFIG['host_os']
|
42
|
+
when /linux|bsd|osx|mach|darwin/i
|
43
|
+
pstring += "-c 1 #{host}"
|
44
|
+
when /solaris|sunos/i
|
45
|
+
pstring += "#{host} 1"
|
46
|
+
when /hpux/i
|
47
|
+
pstring += "#{host} -n 1"
|
48
|
+
when /win32|windows|msdos|mswin|cygwin|mingw/i
|
49
|
+
orig_cp = GetConsoleCP()
|
50
|
+
SetConsoleCP(437) if orig_cp != 437 # United States
|
51
|
+
pstring += "-n 1 #{host}"
|
52
|
+
else
|
53
|
+
pstring += "#{host}"
|
54
|
+
end
|
55
|
+
|
56
|
+
start_time = Time.now
|
57
|
+
|
58
|
+
begin
|
59
|
+
err = nil
|
60
|
+
|
61
|
+
Timeout.timeout(@timeout){
|
62
|
+
stdin, stdout, stderr = Open3.popen3(pstring)
|
63
|
+
err = stderr.gets # Can't chomp yet, might be nil
|
64
|
+
}
|
65
|
+
|
66
|
+
stdin.close
|
67
|
+
stderr.close
|
68
|
+
|
69
|
+
if File::ALT_SEPARATOR && GetConsoleCP() != orig_cp
|
70
|
+
SetConsoleCP(orig_cp)
|
71
|
+
end
|
72
|
+
|
73
|
+
unless err.nil?
|
74
|
+
if err =~ /warning/i
|
75
|
+
@warning = err.chomp
|
76
|
+
bool = true
|
77
|
+
else
|
78
|
+
@exception = err.chomp
|
79
|
+
end
|
80
|
+
# The "no answer" response goes to stdout, not stderr, so check it
|
81
|
+
else
|
82
|
+
lines = stdout.readlines
|
83
|
+
stdout.close
|
84
|
+
if lines.nil? || lines.empty?
|
85
|
+
bool = true
|
86
|
+
else
|
87
|
+
regexp = /
|
88
|
+
no\ answer|
|
89
|
+
host\ unreachable|
|
90
|
+
could\ not\ find\ host|
|
91
|
+
request\ timed\ out|
|
92
|
+
100%\ packet\ loss
|
93
|
+
/ix
|
94
|
+
|
95
|
+
lines.each{ |line|
|
96
|
+
if regexp.match(line)
|
97
|
+
@exception = line.chomp
|
98
|
+
break
|
99
|
+
end
|
100
|
+
}
|
101
|
+
|
102
|
+
bool = true unless @exception
|
103
|
+
end
|
104
|
+
end
|
105
|
+
rescue Exception => error
|
106
|
+
@exception = error.message
|
107
|
+
ensure
|
108
|
+
stdin.close if stdin && !stdin.closed?
|
109
|
+
stdout.close if stdout && !stdout.closed?
|
110
|
+
stderr.close if stderr && !stderr.closed?
|
111
|
+
end
|
112
|
+
|
113
|
+
# There is no duration if the ping failed
|
114
|
+
@duration = Time.now - start_time if bool
|
115
|
+
|
116
|
+
bool
|
117
|
+
end
|
118
|
+
|
119
|
+
alias ping? ping
|
120
|
+
alias pingecho ping
|
121
|
+
end
|
122
|
+
end
|