net-ping 1.3.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- 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/lib/net/ping/ping.rb
CHANGED
@@ -10,12 +10,12 @@ module Net
|
|
10
10
|
#
|
11
11
|
class Ping
|
12
12
|
# The version of the net-ping library.
|
13
|
-
VERSION = '1.3.
|
13
|
+
VERSION = '1.3.3'
|
14
14
|
|
15
|
-
# The host to ping.
|
15
|
+
# The host to ping. In the case of Ping::HTTP, this is the URI.
|
16
16
|
attr_accessor :host
|
17
17
|
|
18
|
-
# The port to ping.
|
18
|
+
# The port to ping. This is set to the echo port (7) by default. The
|
19
19
|
# Ping::HTTP class defaults to port 80.
|
20
20
|
#
|
21
21
|
attr_accessor :port
|
data/lib/net/ping/tcp.rb
CHANGED
@@ -3,81 +3,81 @@ require File.join(File.dirname(__FILE__), 'ping')
|
|
3
3
|
# The Net module serves as a namespace only.
|
4
4
|
module Net
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
# With a TCP ping simply try to open a connection. If we are successful,
|
7
|
+
# assume success. In either case close the connection to be polite.
|
8
|
+
#
|
9
|
+
class Ping::TCP < Ping
|
10
|
+
@@service_check = false
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
# Returns whether or not Errno::ECONNREFUSED is considered a successful
|
13
|
+
# ping. The default is false.
|
14
|
+
#
|
15
|
+
def self.service_check
|
16
|
+
@@service_check
|
17
|
+
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
@@service_check = bool
|
19
|
+
# Sets whether or not an Errno::ECONNREFUSED should be considered a
|
20
|
+
# successful ping.
|
21
|
+
#
|
22
|
+
def self.service_check=(bool)
|
23
|
+
unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
|
24
|
+
raise ArgumentError, 'argument must be true or false'
|
27
25
|
end
|
26
|
+
@@service_check = bool
|
27
|
+
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
29
|
+
# This method attempts to ping a host and port using a TCPSocket with
|
30
|
+
# the host, port and timeout values passed in the constructor. Returns
|
31
|
+
# true if successful, or false otherwise.
|
32
|
+
#
|
33
|
+
# Note that, by default, an Errno::ECONNREFUSED return result will be
|
34
|
+
# considered a failed ping. See the documentation for the
|
35
|
+
# Ping::TCP.service_check= method if you wish to change this behavior.
|
36
|
+
#
|
37
|
+
def ping(host=@host)
|
38
|
+
super(host)
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
bool = false
|
41
|
+
tcp = nil
|
42
|
+
start_time = Time.now
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
@exception = err
|
56
|
-
else
|
57
|
-
bool = true
|
58
|
-
end
|
59
|
-
}
|
60
|
-
rescue Timeout::Error => err
|
44
|
+
begin
|
45
|
+
Timeout.timeout(@timeout){
|
46
|
+
begin
|
47
|
+
tcp = TCPSocket.new(host, @port)
|
48
|
+
rescue Errno::ECONNREFUSED => err
|
49
|
+
if @@service_check
|
50
|
+
bool = true
|
51
|
+
else
|
52
|
+
@exception = err
|
53
|
+
end
|
54
|
+
rescue Exception => err
|
61
55
|
@exception = err
|
62
|
-
|
63
|
-
|
64
|
-
|
56
|
+
else
|
57
|
+
bool = true
|
58
|
+
end
|
59
|
+
}
|
60
|
+
rescue Timeout::Error => err
|
61
|
+
@exception = err
|
62
|
+
ensure
|
63
|
+
tcp.close if tcp
|
64
|
+
end
|
65
65
|
|
66
|
-
|
67
|
-
|
66
|
+
# There is no duration if the ping failed
|
67
|
+
@duration = Time.now - start_time if bool
|
68
68
|
|
69
|
-
|
70
|
-
|
69
|
+
bool
|
70
|
+
end
|
71
71
|
|
72
|
-
|
73
|
-
|
72
|
+
alias ping? ping
|
73
|
+
alias pingecho ping
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
75
|
+
# Class method aliases. DEPRECATED.
|
76
|
+
class << self
|
77
|
+
alias econnrefused service_check
|
78
|
+
alias econnrefused= service_check=
|
79
|
+
alias ecr service_check
|
80
|
+
alias ecr= service_check=
|
81
|
+
end
|
82
|
+
end
|
83
83
|
end
|
data/lib/net/ping/udp.rb
CHANGED
@@ -3,117 +3,117 @@ require File.join(File.dirname(__FILE__), 'ping')
|
|
3
3
|
# The Net module serves as a namespace only.
|
4
4
|
module Net
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
# The Ping::UDP class encapsulates methods for UDP pings.
|
7
|
+
class Ping::UDP < Ping
|
8
|
+
@@service_check = true
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
# Returns whether or not the connect behavior should enforce remote
|
11
|
+
# service availability as well as reachability. The default is true.
|
12
|
+
#
|
13
|
+
def self.service_check
|
14
|
+
@@service_check
|
15
|
+
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
28
|
-
@@service_check = bool
|
17
|
+
# Set whether or not the connect behavior should enforce remote
|
18
|
+
# service availability as well as reachability. If set to false
|
19
|
+
# then Errno::ECONNREFUSED or Errno::ECONNRESET will be considered
|
20
|
+
# a successful ping, meaning no actual data handshaking is required.
|
21
|
+
# By default, if either of those errors occurs it is considered a failed
|
22
|
+
# ping.
|
23
|
+
#
|
24
|
+
def self.service_check=(bool)
|
25
|
+
unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
|
26
|
+
raise ArgumentError, 'argument must be true or false'
|
29
27
|
end
|
28
|
+
@@service_check = bool
|
29
|
+
end
|
30
30
|
|
31
|
-
|
32
|
-
|
31
|
+
# The maximum data size that can be sent in a UDP ping.
|
32
|
+
MAX_DATA = 64
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
# The data to send to the remote host. By default this is 'ping'.
|
35
|
+
# This should be MAX_DATA size characters or less.
|
36
|
+
#
|
37
|
+
attr_reader :data
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
# Creates and returns a new Ping::UDP object. This is effectively
|
40
|
+
# identical to its superclass constructor.
|
41
|
+
#
|
42
|
+
def initialize(host=nil, port=nil, timeout=5)
|
43
|
+
@data = 'ping'
|
44
44
|
|
45
|
-
|
45
|
+
super(host, port, timeout)
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
@bind_host = nil
|
48
|
+
@bind_port = nil
|
49
|
+
end
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
51
|
+
# Sets the data string sent to the remote host. This value cannot have
|
52
|
+
# a size greater than MAX_DATA.
|
53
|
+
#
|
54
|
+
def data=(string)
|
55
|
+
if string.size > MAX_DATA
|
56
|
+
err = "cannot set data string larger than #{MAX_DATA} characters"
|
57
|
+
raise ArgumentError, err
|
58
|
+
end
|
59
59
|
|
60
|
-
|
61
|
-
|
60
|
+
@data = string
|
61
|
+
end
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
63
|
+
# Associates the local end of the UDP connection with the given +host+
|
64
|
+
# and +port+. This is essentially a wrapper for UDPSocket#bind.
|
65
|
+
#
|
66
|
+
def bind(host, port)
|
67
|
+
@bind_host = host
|
68
|
+
@bind_port = port
|
69
|
+
end
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
71
|
+
# Sends a simple text string to the host and checks the return string. If
|
72
|
+
# the string sent and the string returned are a match then the ping was
|
73
|
+
# successful and true is returned. Otherwise, false is returned.
|
74
|
+
#
|
75
|
+
def ping(host = @host)
|
76
|
+
super(host)
|
77
77
|
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
bool = false
|
79
|
+
udp = UDPSocket.open
|
80
|
+
array = []
|
81
81
|
|
82
|
-
|
83
|
-
|
84
|
-
|
82
|
+
if @bind_host
|
83
|
+
udp.bind(@bind_host, @bind_port)
|
84
|
+
end
|
85
85
|
|
86
|
-
|
86
|
+
start_time = Time.now
|
87
87
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
88
|
+
begin
|
89
|
+
Timeout.timeout(@timeout){
|
90
|
+
udp.connect(host, @port)
|
91
|
+
udp.send(@data, 0)
|
92
|
+
array = udp.recvfrom(MAX_DATA)
|
93
|
+
}
|
94
|
+
rescue Errno::ECONNREFUSED, Errno::ECONNRESET => err
|
95
|
+
if @@service_check
|
96
|
+
@exception = err
|
97
|
+
else
|
98
|
+
bool = true
|
99
|
+
end
|
100
|
+
rescue Exception => err
|
101
|
+
@exception = err
|
102
|
+
else
|
103
|
+
if array[0] == @data
|
104
|
+
bool = true
|
105
|
+
end
|
106
|
+
ensure
|
107
|
+
udp.close if udp
|
108
|
+
end
|
109
109
|
|
110
|
-
|
111
|
-
|
110
|
+
# There is no duration if the ping failed
|
111
|
+
@duration = Time.now - start_time if bool
|
112
112
|
|
113
|
-
|
114
|
-
|
113
|
+
bool
|
114
|
+
end
|
115
115
|
|
116
|
-
|
117
|
-
|
118
|
-
|
116
|
+
alias ping? ping
|
117
|
+
alias pingecho ping
|
118
|
+
end
|
119
119
|
end
|
data/net-ping.gemspec
CHANGED
@@ -2,35 +2,37 @@ require 'rubygems'
|
|
2
2
|
require 'rbconfig'
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
5
|
+
gem.name = 'net-ping'
|
6
|
+
gem.version = '1.3.3'
|
7
|
+
gem.license = 'Artistic 2.0'
|
8
|
+
gem.author = 'Daniel J. Berger'
|
9
|
+
gem.email = 'djberg96@gmail.com'
|
10
|
+
gem.homepage = 'http://www.rubyforge.org/projects/shards'
|
11
|
+
gem.summary = 'A ping interface for Ruby.'
|
12
|
+
gem.test_file = 'test/test_net_ping.rb'
|
13
|
+
gem.has_rdoc = true
|
14
|
+
gem.files = Dir['**/*'].reject{ |f| f.include?('git') }
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
gem.rubyforge_project = 'shards'
|
17
|
+
gem.extra_rdoc_files = ['README', 'CHANGES', 'doc/ping.txt']
|
18
18
|
|
19
|
-
|
19
|
+
gem.add_development_dependency('test-unit', '>= 2.0.9')
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
# These dependencies are for Net::Ping::External
|
22
|
+
if Config::CONFIG['host_os'] =~ /mswin|dos|win32|cygwin|mingw/i &&
|
23
|
+
RUBY_PLATFORM != 'java'
|
24
|
+
then
|
25
|
+
gem.platform = Gem::Platform::CURRENT
|
26
|
+
gem.add_dependency('windows-pr', '>= 1.0.8')
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
if RUBY_VERSION.to_f < 1.9
|
29
|
+
gem.add_dependency('win32-open3', '>= 0.3.1')
|
30
|
+
end
|
31
|
+
end
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
gem.description = <<-EOF
|
34
|
+
The net-ping library provides a ping interface for Ruby. It includes
|
35
|
+
separate TCP, HTTP, ICMP, UDP, WMI (for Windows) and external ping
|
36
|
+
classes.
|
37
|
+
EOF
|
36
38
|
end
|
data/test/test_net_ping.rb
CHANGED
@@ -10,9 +10,17 @@ require 'test_net_ping_tcp'
|
|
10
10
|
require 'test_net_ping_udp'
|
11
11
|
|
12
12
|
if Process.euid == 0
|
13
|
-
|
13
|
+
require 'test_net_ping_icmp'
|
14
14
|
end
|
15
15
|
|
16
|
-
if Config::CONFIG['host_os'] =~ /mswin|win32|dos|cygwin|mingw/i
|
17
|
-
|
16
|
+
if Config::CONFIG['host_os'] =~ /mswin|win32|dos|cygwin|mingw/i &&
|
17
|
+
RUBY_PLATFORM != 'java'
|
18
|
+
then
|
19
|
+
require 'test_net_ping_wmi'
|
20
|
+
end
|
21
|
+
|
22
|
+
class TC_Net_Ping < Test::Unit::TestCase
|
23
|
+
def test_net_ping_version
|
24
|
+
assert_equal('1.3.3', Net::Ping::VERSION)
|
25
|
+
end
|
18
26
|
end
|