net-ping 1.3.2 → 1.3.3
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.
- data/CHANGES +11 -0
- data/README +4 -6
- data/Rakefile +11 -24
- data/doc/ping.txt +1 -3
- data/lib/net/ping.rb +4 -2
- data/lib/net/ping/external.rb +106 -99
- data/lib/net/ping/http.rb +74 -71
- data/lib/net/ping/ping.rb +3 -3
- data/lib/net/ping/tcp.rb +66 -66
- data/lib/net/ping/udp.rb +94 -94
- data/net-ping.gemspec +28 -26
- data/test/test_net_ping.rb +11 -3
- data/test/test_net_ping_external.rb +63 -68
- data/test/test_net_ping_http.rb +103 -74
- data/test/test_net_ping_icmp.rb +0 -4
- data/test/test_net_ping_tcp.rb +79 -83
- data/test/test_net_ping_udp.rb +108 -88
- data/test/test_net_ping_wmi.rb +0 -4
- metadata +22 -11
data/CHANGES
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
== 1.3.3 - 21-Jun-2010
|
2
|
+
* Bug fixes for JRuby on MS Windows. The code now explicitly checks for JRuby
|
3
|
+
in a few places to ensure it doesn't try to load unsupported libraries
|
4
|
+
on MS Windows. Thanks go to Rob Schultz for the spot and some patches.
|
5
|
+
* The Net::Ping::HTTP class will no longer fail because a root URI
|
6
|
+
is missing a trailing slash. If the URI#path is empty, it now defaults
|
7
|
+
to the root path.
|
8
|
+
* The Rakefile tasks and naming were refactored.
|
9
|
+
* Some tests were refactored to take advantage of test-unit 2.x features,
|
10
|
+
as well as make them better and more descriptive in general.
|
11
|
+
|
1
12
|
== 1.3.2 - 21-Sep-2009
|
2
13
|
* Doing a 'require "net/ping"' was not automatically loading the
|
3
14
|
Net::Ping::WMI class on MS Windows. This has been fixed. Thanks go to
|
data/README
CHANGED
@@ -4,17 +4,13 @@
|
|
4
4
|
== Prerequisites
|
5
5
|
* Ruby 1.8.0 or later
|
6
6
|
* The win32-open3 (1.8.x) and windows-pr libraries are required on
|
7
|
-
MS Windows when using the Net::Ping::External class
|
7
|
+
MS Windows when using the Net::Ping::External class unless you're
|
8
|
+
using JRuby.
|
8
9
|
* Windows 2000 or later is required to use the Ping::WMI class.
|
9
10
|
|
10
11
|
== Installation
|
11
|
-
=== Remote Installation
|
12
12
|
gem install net-ping
|
13
13
|
|
14
|
-
=== Local Installation
|
15
|
-
rake test (optional)
|
16
|
-
rake install (standard) OR rake gem_install (gems)
|
17
|
-
|
18
14
|
== Notes
|
19
15
|
Please read the documentation under the 'doc' directory. Especially pay
|
20
16
|
attention to the documentation pertaining to ECONNREFUSED and TCP pings.
|
@@ -40,6 +36,8 @@
|
|
40
36
|
|
41
37
|
== Known Issues
|
42
38
|
Older versions of Ruby 1.9.x may not work with UDP pings.
|
39
|
+
As of JRuby 1.5.1, UDP pings will return false positives because of an
|
40
|
+
incorrect error class being raised. See JRuby-4896.
|
43
41
|
|
44
42
|
== License
|
45
43
|
Artistic 2.0
|
data/Rakefile
CHANGED
@@ -2,31 +2,18 @@ require 'rake'
|
|
2
2
|
require 'rake/testtask'
|
3
3
|
include Config
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
Dir.mkdir(dest2) unless File.exists?(dest2)
|
12
|
-
|
13
|
-
FileUtils.cp('lib/net/ping.rb', dest1, :verbose => true)
|
14
|
-
|
15
|
-
Dir['lib/net/ping/*.rb'].each{ |file|
|
16
|
-
FileUtils.cp(file, dest2, :verbose => true)
|
17
|
-
}
|
18
|
-
end
|
19
|
-
|
20
|
-
desc 'Create and install the net-ping gem'
|
21
|
-
task :gem_install => [:gem] do
|
22
|
-
gem_file = Dir["*.gem"].first
|
23
|
-
sh "gem install #{gem_file}"
|
24
|
-
end
|
5
|
+
namespace 'gem' do
|
6
|
+
desc 'Create the net-ping gem'
|
7
|
+
task :create do
|
8
|
+
spec = eval(IO.read('net-ping.gemspec'))
|
9
|
+
Gem::Builder.new(spec).build
|
10
|
+
end
|
25
11
|
|
26
|
-
desc '
|
27
|
-
task :
|
28
|
-
|
29
|
-
|
12
|
+
desc 'Install the net-ping gem'
|
13
|
+
task :install => [:create] do
|
14
|
+
gem_file = Dir["*.gem"].first
|
15
|
+
sh "gem install #{gem_file}"
|
16
|
+
end
|
30
17
|
end
|
31
18
|
|
32
19
|
namespace 'example' do
|
data/doc/ping.txt
CHANGED
@@ -238,7 +238,7 @@ Ping#warning
|
|
238
238
|
Artistic 2.0
|
239
239
|
|
240
240
|
= Copyright
|
241
|
-
(C) 2003-
|
241
|
+
(C) 2003-2010 Daniel J. Berger, All Rights Reserved
|
242
242
|
|
243
243
|
= Warranty
|
244
244
|
This package is provided "as is" and without any express or
|
@@ -247,5 +247,3 @@ Ping#warning
|
|
247
247
|
|
248
248
|
= Author
|
249
249
|
Daniel J. Berger
|
250
|
-
djberg96 at gmail dot com
|
251
|
-
imperator on IRC (irc.freenode.net)
|
data/lib/net/ping.rb
CHANGED
@@ -10,6 +10,8 @@ require File.join(File.dirname(__FILE__), 'ping/icmp')
|
|
10
10
|
require File.join(File.dirname(__FILE__), 'ping/external')
|
11
11
|
require File.join(File.dirname(__FILE__), 'ping/http')
|
12
12
|
|
13
|
-
if Config::CONFIG['host_os'] =~ /
|
14
|
-
|
13
|
+
if Config::CONFIG['host_os'] =~ /msdos|mswin|cygwin|mingw|win32/i &&
|
14
|
+
JAVA_PLATFORM != 'java'
|
15
|
+
then
|
16
|
+
require File.join(File.dirname(__FILE__), 'ping/wmi')
|
15
17
|
end
|
data/lib/net/ping/external.rb
CHANGED
@@ -1,119 +1,126 @@
|
|
1
1
|
require 'rbconfig'
|
2
2
|
require File.join(File.dirname(__FILE__), 'ping')
|
3
3
|
|
4
|
-
if Config::CONFIG['host_os'] =~ /mswin|win32|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
if Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i &&
|
5
|
+
RUBY_PLATFORM != 'java'
|
6
|
+
then
|
7
|
+
if RUBY_VERSION.to_f < 1.9
|
8
|
+
require 'win32/open3'
|
9
|
+
end
|
10
|
+
require 'windows/console'
|
9
11
|
else
|
10
|
-
|
12
|
+
require 'open3'
|
11
13
|
end
|
12
14
|
|
13
15
|
# The Net module serves as a namespace only.
|
14
16
|
module Net
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
+
# The Ping::External class encapsulates methods for external (system) pings.
|
19
|
+
class Ping::External < Ping
|
20
|
+
|
21
|
+
CWINDOWS = Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i &&
|
22
|
+
RUBY_PLATFORM != 'java'
|
18
23
|
|
19
|
-
|
20
|
-
|
21
|
-
|
24
|
+
if CWINDOWS
|
25
|
+
include Windows::Console
|
26
|
+
end
|
22
27
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
# Pings the host using your system's ping utility and checks for any
|
29
|
+
# errors or warnings. Returns true if successful, or false if not.
|
30
|
+
#
|
31
|
+
# If the ping failed then the Ping::External#exception method should
|
32
|
+
# contain a string indicating what went wrong. If the ping succeeded then
|
33
|
+
# the Ping::External#warning method may or may not contain a value.
|
34
|
+
#
|
35
|
+
def ping(host = @host)
|
36
|
+
super(host)
|
32
37
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
38
|
+
input, output, error = ""
|
39
|
+
pstring = "ping "
|
40
|
+
bool = false
|
41
|
+
orig_cp = nil
|
42
|
+
|
43
|
+
case Config::CONFIG['host_os']
|
44
|
+
when /linux|bsd|osx|mach|darwin/i
|
45
|
+
pstring += "-c 1 #{host}"
|
46
|
+
when /solaris|sunos/i
|
47
|
+
pstring += "#{host} 1"
|
48
|
+
when /hpux/i
|
49
|
+
pstring += "#{host} -n 1"
|
50
|
+
when /win32|windows|msdos|mswin|cygwin|mingw/i
|
51
|
+
if RUBY_PLATFORM != 'java'
|
52
|
+
orig_cp = GetConsoleCP()
|
53
|
+
SetConsoleCP(437) if orig_cp != 437 # United States
|
54
|
+
end
|
55
|
+
pstring += "-n 1 #{host}"
|
56
|
+
else
|
57
|
+
pstring += "#{host}"
|
58
|
+
end
|
59
|
+
|
60
|
+
start_time = Time.now
|
61
|
+
|
62
|
+
begin
|
63
|
+
err = nil
|
57
64
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
65
|
+
Timeout.timeout(@timeout){
|
66
|
+
input, output, error = Open3.popen3(pstring)
|
67
|
+
err = error.gets # Can't chomp yet, might be nil
|
68
|
+
}
|
62
69
|
|
63
|
-
|
64
|
-
|
70
|
+
input.close
|
71
|
+
error.close
|
65
72
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
100%\ packet\ loss
|
92
|
-
/ix
|
93
|
-
lines.each{ |line|
|
94
|
-
if regexp.match(line)
|
95
|
-
@exception = line.chomp
|
96
|
-
break
|
97
|
-
end
|
98
|
-
}
|
99
|
-
bool = true unless @exception
|
100
|
-
end
|
101
|
-
end
|
102
|
-
rescue Exception => err
|
103
|
-
@exception = err.message
|
104
|
-
ensure
|
105
|
-
input.close if input && !input.closed?
|
106
|
-
error.close if error && !error.closed?
|
107
|
-
output.close if output && !output.closed?
|
108
|
-
end
|
73
|
+
if CWINDOWS && GetConsoleCP() != orig_cp
|
74
|
+
SetConsoleCP(orig_cp)
|
75
|
+
end
|
76
|
+
|
77
|
+
unless err.nil?
|
78
|
+
if err =~ /warning/i
|
79
|
+
@warning = err.chomp
|
80
|
+
bool = true
|
81
|
+
else
|
82
|
+
@exception = err.chomp
|
83
|
+
end
|
84
|
+
# The "no answer" response goes to stdout, not stderr, so check it
|
85
|
+
else
|
86
|
+
lines = output.readlines
|
87
|
+
output.close
|
88
|
+
if lines.nil? || lines.empty?
|
89
|
+
bool = true
|
90
|
+
else
|
91
|
+
regexp = /
|
92
|
+
no\ answer|
|
93
|
+
host\ unreachable|
|
94
|
+
could\ not\ find\ host|
|
95
|
+
request\ timed\ out|
|
96
|
+
100%\ packet\ loss
|
97
|
+
/ix
|
109
98
|
|
110
|
-
|
111
|
-
|
99
|
+
lines.each{ |line|
|
100
|
+
if regexp.match(line)
|
101
|
+
@exception = line.chomp
|
102
|
+
break
|
103
|
+
end
|
104
|
+
}
|
112
105
|
|
113
|
-
|
106
|
+
bool = true unless @exception
|
107
|
+
end
|
108
|
+
end
|
109
|
+
rescue Exception => error
|
110
|
+
@exception = error.message
|
111
|
+
ensure
|
112
|
+
input.close if input && !input.closed?
|
113
|
+
error.close if error && !error.closed?
|
114
|
+
output.close if output && !output.closed?
|
114
115
|
end
|
115
116
|
|
116
|
-
|
117
|
-
|
118
|
-
|
117
|
+
# There is no duration if the ping failed
|
118
|
+
@duration = Time.now - start_time if bool
|
119
|
+
|
120
|
+
bool
|
121
|
+
end
|
122
|
+
|
123
|
+
alias ping? ping
|
124
|
+
alias pingecho ping
|
125
|
+
end
|
119
126
|
end
|
data/lib/net/ping/http.rb
CHANGED
@@ -5,91 +5,94 @@ require 'rbconfig'
|
|
5
5
|
|
6
6
|
# Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
|
7
7
|
# Windows because it (ironically) causes blocking problems.
|
8
|
-
unless Config::CONFIG['host_os'] =~ /mswin|win32|
|
9
|
-
|
8
|
+
unless Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw/i
|
9
|
+
require 'resolv-replace'
|
10
10
|
end
|
11
11
|
|
12
12
|
# The Net module serves as a namespace only.
|
13
13
|
module Net
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
# The Ping::HTTP class encapsulates methods for HTTP pings.
|
16
|
+
class Ping::HTTP < Ping
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
# By default an http ping will follow a redirect and give you the result
|
19
|
+
# of the final URI. If this value is set to false, then it will not
|
20
|
+
# follow a redirect and will return false immediately on a redirect.
|
21
|
+
#
|
22
|
+
attr_accessor :follow_redirect
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
# Creates and returns a new Ping::HTTP object. Note that the default
|
25
|
+
# port for Ping::HTTP is 80.
|
26
|
+
#
|
27
|
+
def initialize(uri=nil, port=80, timeout=5)
|
28
|
+
@follow_redirect = true
|
29
|
+
super(uri, port, timeout)
|
30
|
+
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
32
|
+
# Looks for an HTTP response from the URI passed to the constructor.
|
33
|
+
# If the result is a kind of Net::HTTPSuccess then the ping was
|
34
|
+
# successful and true is returned. Otherwise, false is returned
|
35
|
+
# and the Ping::HTTP#exception method should contain a string
|
36
|
+
# indicating what went wrong.
|
37
|
+
#
|
38
|
+
# If the HTTP#follow_redirect accessor is set to true (which it is
|
39
|
+
# by default) and a redirect occurs during the ping, then the
|
40
|
+
# HTTP#warning attribute is set to the redirect message, but the
|
41
|
+
# return result is still true. If it's set to false then a redirect
|
42
|
+
# response is considered a failed ping.
|
43
|
+
#
|
44
|
+
# If no file or path is specified in the URI, then '/' is assumed.
|
45
|
+
#
|
46
|
+
def ping(host = @host)
|
47
|
+
super(host)
|
48
|
+
bool = false
|
49
|
+
uri = URI.parse(host)
|
48
50
|
|
49
|
-
|
51
|
+
start_time = Time.now
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
53
|
+
begin
|
54
|
+
response = nil
|
55
|
+
uri_path = uri.path.empty? ? '/' : uri.path
|
56
|
+
Timeout.timeout(@timeout){
|
57
|
+
response = Net::HTTP.get_response(uri.host, uri_path, @port)
|
58
|
+
}
|
59
|
+
rescue Exception => err
|
60
|
+
@exception = err.message
|
61
|
+
else
|
62
|
+
if response.is_a?(Net::HTTPSuccess)
|
63
|
+
bool = true
|
64
|
+
else
|
65
|
+
if @follow_redirect
|
66
|
+
@warning = response.message
|
67
|
+
|
68
|
+
while response.is_a?(Net::HTTPRedirection)
|
69
|
+
redirect = URI.parse(response['location'])
|
70
|
+
redirect = uri + redirect if redirect.relative?
|
71
|
+
response = Net::HTTP.get_response(redirect.host, redirect.path, @port)
|
72
|
+
end
|
73
|
+
|
59
74
|
if response.is_a?(Net::HTTPSuccess)
|
60
|
-
|
75
|
+
bool = true
|
61
76
|
else
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
while response.is_a?(Net::HTTPRedirection)
|
66
|
-
redirect = URI.parse(response['location'])
|
67
|
-
redirect = uri + redirect if redirect.relative?
|
68
|
-
response = Net::HTTP.get_response(redirect.host, redirect.path, @port)
|
69
|
-
end
|
70
|
-
|
71
|
-
if response.is_a?(Net::HTTPSuccess)
|
72
|
-
bool = true
|
73
|
-
else
|
74
|
-
@warning = nil
|
75
|
-
@exception = response.message
|
76
|
-
end
|
77
|
-
else
|
78
|
-
@exception = response.message
|
79
|
-
end
|
77
|
+
@warning = nil
|
78
|
+
@exception = response.message
|
80
79
|
end
|
81
|
-
|
80
|
+
else
|
81
|
+
@exception = response.message
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
82
85
|
|
83
|
-
|
84
|
-
|
86
|
+
# There is no duration if the ping failed
|
87
|
+
@duration = Time.now - start_time if bool
|
85
88
|
|
86
|
-
|
87
|
-
|
89
|
+
bool
|
90
|
+
end
|
88
91
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
92
|
+
alias ping? ping
|
93
|
+
alias pingecho ping
|
94
|
+
alias follow_redirect? follow_redirect
|
95
|
+
alias uri host
|
96
|
+
alias uri= host=
|
97
|
+
end
|
95
98
|
end
|