net-ping 1.7.1-universal-mingw32 → 1.7.2-universal-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/CHANGES +309 -298
- data/Gemfile +2 -2
- data/MANIFEST +0 -1
- data/README +62 -57
- data/Rakefile +94 -94
- data/doc/ping.txt +246 -246
- data/lib/net/ping.rb +17 -17
- data/lib/net/ping/external.rb +74 -122
- data/lib/net/ping/http.rb +171 -159
- data/lib/net/ping/icmp.rb +178 -179
- data/lib/net/ping/ping.rb +89 -89
- data/lib/net/ping/tcp.rb +102 -102
- data/net-ping.gemspec +40 -46
- data/test/test_net_ping.rb +35 -35
- data/test/test_net_ping_external.rb +129 -129
- data/test/test_net_ping_http.rb +230 -210
- data/test/test_net_ping_icmp.rb +139 -139
- data/test/test_net_ping_tcp.rb +105 -105
- data/test/test_net_ping_udp.rb +119 -119
- data/test/test_net_ping_wmi.rb +81 -81
- metadata +20 -33
- data/lib/net/ping/helper.rb +0 -33
data/lib/net/ping.rb
CHANGED
@@ -1,17 +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
|
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
|
data/lib/net/ping/external.rb
CHANGED
@@ -1,122 +1,74 @@
|
|
1
|
-
require '
|
2
|
-
require 'rbconfig'
|
3
|
-
|
4
|
-
require File.join(File.dirname(__FILE__), 'ping')
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
#
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
1
|
+
require 'open3'
|
2
|
+
require 'rbconfig'
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), 'ping')
|
5
|
+
|
6
|
+
# The Net module serves as a namespace only.
|
7
|
+
module Net
|
8
|
+
|
9
|
+
# The Ping::External class encapsulates methods for external (system) pings.
|
10
|
+
class Ping::External < Ping
|
11
|
+
# Pings the host using your system's ping utility and checks for any
|
12
|
+
# errors or warnings. Returns true if successful, or false if not.
|
13
|
+
#
|
14
|
+
# If the ping failed then the Ping::External#exception method should
|
15
|
+
# contain a string indicating what went wrong. If the ping succeeded then
|
16
|
+
# the Ping::External#warning method may or may not contain a value.
|
17
|
+
#
|
18
|
+
def ping(host = @host)
|
19
|
+
super(host)
|
20
|
+
|
21
|
+
pcmd = ['ping']
|
22
|
+
bool = false
|
23
|
+
|
24
|
+
case RbConfig::CONFIG['host_os']
|
25
|
+
when /linux|bsd|osx|mach|darwin/i
|
26
|
+
pcmd += ['-c1', host]
|
27
|
+
when /solaris|sunos/i
|
28
|
+
pcmd += [host, '1']
|
29
|
+
when /hpux/i
|
30
|
+
pcmd += [host, '-n1']
|
31
|
+
when /win32|windows|msdos|mswin|cygwin|mingw/i
|
32
|
+
pcmd += ['-n', '1', host]
|
33
|
+
else
|
34
|
+
pcmd += [host]
|
35
|
+
end
|
36
|
+
|
37
|
+
start_time = Time.now
|
38
|
+
|
39
|
+
begin
|
40
|
+
err = nil
|
41
|
+
|
42
|
+
Timeout.timeout(@timeout){
|
43
|
+
Open3.popen3(*pcmd) do |stdin, stdout, stderr, thread|
|
44
|
+
err = stderr.gets # Can't chomp yet, might be nil
|
45
|
+
|
46
|
+
case thread.value.exitstatus
|
47
|
+
when 0
|
48
|
+
bool = true # Success, at least one response.
|
49
|
+
if err & err =~ /warning/i
|
50
|
+
@warning = err.chomp
|
51
|
+
end
|
52
|
+
when 2
|
53
|
+
bool = false # Transmission successful, no response.
|
54
|
+
@exception = err.chomp
|
55
|
+
else
|
56
|
+
bool = false # An error occurred
|
57
|
+
@exception = err.chomp
|
58
|
+
end
|
59
|
+
end
|
60
|
+
}
|
61
|
+
rescue Exception => error
|
62
|
+
@exception = error.message
|
63
|
+
end
|
64
|
+
|
65
|
+
# There is no duration if the ping failed
|
66
|
+
@duration = Time.now - start_time if bool
|
67
|
+
|
68
|
+
bool
|
69
|
+
end
|
70
|
+
|
71
|
+
alias ping? ping
|
72
|
+
alias pingecho ping
|
73
|
+
end
|
74
|
+
end
|
data/lib/net/ping/http.rb
CHANGED
@@ -1,159 +1,171 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'ping')
|
2
|
-
require 'net/http'
|
3
|
-
require 'net/https'
|
4
|
-
require 'uri'
|
5
|
-
require 'open-uri'
|
6
|
-
|
7
|
-
# Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
|
8
|
-
# Windows because it (ironically) causes blocking problems.
|
9
|
-
unless File::ALT_SEPARATOR or RUBY_VERSION >= "1.9.3"
|
10
|
-
require 'resolv-replace'
|
11
|
-
end
|
12
|
-
|
13
|
-
# The Net module serves as a namespace only.
|
14
|
-
module Net
|
15
|
-
|
16
|
-
# The Ping::HTTP class encapsulates methods for HTTP pings.
|
17
|
-
class Ping::HTTP < Ping
|
18
|
-
|
19
|
-
# By default an http ping will follow a redirect and give you the result
|
20
|
-
# of the final URI. If this value is set to false, then it will not
|
21
|
-
# follow a redirect and will return false immediately on a redirect.
|
22
|
-
#
|
23
|
-
attr_accessor :follow_redirect
|
24
|
-
|
25
|
-
# The maximum number of redirects allowed. The default is 5.
|
26
|
-
attr_accessor :redirect_limit
|
27
|
-
|
28
|
-
# The user agent used for the HTTP request. The default is nil.
|
29
|
-
attr_accessor :user_agent
|
30
|
-
|
31
|
-
# OpenSSL certificate verification mode. The default is VERIFY_NONE.
|
32
|
-
attr_accessor :ssl_verify_mode
|
33
|
-
|
34
|
-
# Use GET request instead HEAD. The default is false.
|
35
|
-
attr_accessor :get_request
|
36
|
-
|
37
|
-
# was this ping proxied?
|
38
|
-
attr_accessor :proxied
|
39
|
-
|
40
|
-
#
|
41
|
-
#
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
#
|
60
|
-
# If the
|
61
|
-
#
|
62
|
-
# HTTP#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
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
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
1
|
+
require File.join(File.dirname(__FILE__), 'ping')
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'uri'
|
5
|
+
require 'open-uri'
|
6
|
+
|
7
|
+
# Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
|
8
|
+
# Windows because it (ironically) causes blocking problems.
|
9
|
+
unless File::ALT_SEPARATOR or RUBY_VERSION >= "1.9.3"
|
10
|
+
require 'resolv-replace'
|
11
|
+
end
|
12
|
+
|
13
|
+
# The Net module serves as a namespace only.
|
14
|
+
module Net
|
15
|
+
|
16
|
+
# The Ping::HTTP class encapsulates methods for HTTP pings.
|
17
|
+
class Ping::HTTP < Ping
|
18
|
+
|
19
|
+
# By default an http ping will follow a redirect and give you the result
|
20
|
+
# of the final URI. If this value is set to false, then it will not
|
21
|
+
# follow a redirect and will return false immediately on a redirect.
|
22
|
+
#
|
23
|
+
attr_accessor :follow_redirect
|
24
|
+
|
25
|
+
# The maximum number of redirects allowed. The default is 5.
|
26
|
+
attr_accessor :redirect_limit
|
27
|
+
|
28
|
+
# The user agent used for the HTTP request. The default is nil.
|
29
|
+
attr_accessor :user_agent
|
30
|
+
|
31
|
+
# OpenSSL certificate verification mode. The default is VERIFY_NONE.
|
32
|
+
attr_accessor :ssl_verify_mode
|
33
|
+
|
34
|
+
# Use GET request instead HEAD. The default is false.
|
35
|
+
attr_accessor :get_request
|
36
|
+
|
37
|
+
# was this ping proxied?
|
38
|
+
attr_accessor :proxied
|
39
|
+
|
40
|
+
# For unsuccessful requests that return a server error, it is
|
41
|
+
# useful to know the HTTP status code of the response.
|
42
|
+
attr_reader :code
|
43
|
+
|
44
|
+
# Creates and returns a new Ping::HTTP object. The default port is the
|
45
|
+
# port associated with the URI. The default timeout is 5 seconds.
|
46
|
+
#
|
47
|
+
def initialize(uri=nil, port=nil, timeout=5)
|
48
|
+
@follow_redirect = true
|
49
|
+
@redirect_limit = 5
|
50
|
+
@ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
|
51
|
+
@get_request = false
|
52
|
+
@code = nil
|
53
|
+
|
54
|
+
port ||= URI.parse(uri).port if uri
|
55
|
+
|
56
|
+
super(uri, port, timeout)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Looks for an HTTP response from the URI passed to the constructor.
|
60
|
+
# If the result is a kind of Net::HTTPSuccess then the ping was
|
61
|
+
# successful and true is returned. Otherwise, false is returned
|
62
|
+
# and the Ping::HTTP#exception method should contain a string
|
63
|
+
# indicating what went wrong.
|
64
|
+
#
|
65
|
+
# If the HTTP#follow_redirect accessor is set to true (which it is
|
66
|
+
# by default) and a redirect occurs during the ping, then the
|
67
|
+
# HTTP#warning attribute is set to the redirect message, but the
|
68
|
+
# return result is still true. If it's set to false then a redirect
|
69
|
+
# response is considered a failed ping.
|
70
|
+
#
|
71
|
+
# If no file or path is specified in the URI, then '/' is assumed.
|
72
|
+
# If no scheme is present in the URI, then 'http' is assumed.
|
73
|
+
#
|
74
|
+
def ping(host = @host)
|
75
|
+
super(host)
|
76
|
+
bool = false
|
77
|
+
|
78
|
+
# See https://bugs.ruby-lang.org/issues/8645
|
79
|
+
host = "http://#{host}" unless host.include?("http")
|
80
|
+
|
81
|
+
uri = URI.parse(host)
|
82
|
+
|
83
|
+
# A port provided here overrides anything provided in constructor
|
84
|
+
port = URI.split(host)[3] || @port
|
85
|
+
port = port.to_i
|
86
|
+
|
87
|
+
start_time = Time.now
|
88
|
+
|
89
|
+
response = do_ping(uri, port)
|
90
|
+
|
91
|
+
if response.is_a?(Net::HTTPSuccess)
|
92
|
+
bool = true
|
93
|
+
elsif redirect?(response) # Check code, HTTPRedirection does not always work
|
94
|
+
if @follow_redirect
|
95
|
+
@warning = response.message
|
96
|
+
rlimit = 0
|
97
|
+
|
98
|
+
while redirect?(response)
|
99
|
+
if rlimit >= redirect_limit
|
100
|
+
@exception = "Redirect limit exceeded"
|
101
|
+
break
|
102
|
+
end
|
103
|
+
redirect = URI.parse(response['location'])
|
104
|
+
redirect = uri + redirect if redirect.relative?
|
105
|
+
response = do_ping(redirect, port)
|
106
|
+
rlimit += 1
|
107
|
+
end
|
108
|
+
|
109
|
+
if response.is_a?(Net::HTTPSuccess)
|
110
|
+
bool = true
|
111
|
+
else
|
112
|
+
@warning = nil
|
113
|
+
@exception ||= response.message
|
114
|
+
end
|
115
|
+
|
116
|
+
else
|
117
|
+
@exception = response.message
|
118
|
+
end
|
119
|
+
else
|
120
|
+
@exception ||= response.message
|
121
|
+
end
|
122
|
+
|
123
|
+
# There is no duration if the ping failed
|
124
|
+
@duration = Time.now - start_time if bool
|
125
|
+
|
126
|
+
bool
|
127
|
+
end
|
128
|
+
|
129
|
+
alias ping? ping
|
130
|
+
alias pingecho ping
|
131
|
+
alias follow_redirect? follow_redirect
|
132
|
+
alias uri host
|
133
|
+
alias uri= host=
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
def redirect?(response)
|
138
|
+
response && response.code.to_i >= 300 && response.code.to_i < 400
|
139
|
+
end
|
140
|
+
|
141
|
+
def do_ping(uri, port)
|
142
|
+
response = nil
|
143
|
+
proxy = uri.find_proxy || URI.parse("")
|
144
|
+
begin
|
145
|
+
uri_path = uri.path.empty? ? '/' : uri.path
|
146
|
+
headers = { }
|
147
|
+
headers["User-Agent"] = user_agent unless user_agent.nil?
|
148
|
+
Timeout.timeout(@timeout) do
|
149
|
+
http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new(uri.host, port)
|
150
|
+
@proxied = http.proxy?
|
151
|
+
if @get_request == true
|
152
|
+
request = Net::HTTP::Get.new(uri_path)
|
153
|
+
else
|
154
|
+
request = Net::HTTP::Head.new(uri_path)
|
155
|
+
end
|
156
|
+
|
157
|
+
if uri.scheme == 'https'
|
158
|
+
http.use_ssl = true
|
159
|
+
http.verify_mode = @ssl_verify_mode
|
160
|
+
end
|
161
|
+
|
162
|
+
response = http.start { |h| h.request(request) }
|
163
|
+
end
|
164
|
+
rescue Exception => err
|
165
|
+
@exception = err.message
|
166
|
+
end
|
167
|
+
@code = response.code if response
|
168
|
+
response
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|