net-ping 1.4.1-x86-mingw32 → 1.5.3-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +258 -232
- data/MANIFEST +25 -23
- data/README +52 -46
- data/Rakefile +100 -89
- data/doc/ping.txt +274 -249
- data/examples/example_pingexternal.rb +16 -16
- data/examples/example_pinghttp.rb +22 -22
- data/examples/example_pingldap.rb +22 -0
- data/examples/example_pingtcp.rb +16 -16
- data/examples/example_pingudp.rb +12 -12
- data/lib/net/ping.rb +18 -15
- data/lib/net/ping/external.rb +122 -126
- data/lib/net/ping/http.rb +149 -120
- data/lib/net/ping/icmp.rb +180 -168
- data/lib/net/ping/ldap.rb +107 -0
- data/lib/net/ping/ping.rb +89 -88
- data/lib/net/ping/tcp.rb +83 -83
- data/lib/net/ping/udp.rb +119 -119
- data/lib/net/ping/wmi.rb +118 -118
- data/net-ping.gemspec +43 -38
- data/test/test_net_ping.rb +37 -37
- data/test/test_net_ping_external.rb +128 -121
- data/test/test_net_ping_http.rb +182 -176
- data/test/test_net_ping_icmp.rb +120 -116
- data/test/test_net_ping_ldap.rb +200 -0
- data/test/test_net_ping_tcp.rb +108 -108
- data/test/test_net_ping_udp.rb +122 -122
- data/test/test_net_ping_wmi.rb +84 -84
- metadata +59 -26
@@ -1,16 +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?
|
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?
|
@@ -1,22 +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
|
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,22 @@
|
|
1
|
+
########################################################################
|
2
|
+
# example_pingldap.rb
|
3
|
+
#
|
4
|
+
# A short sample program demonstrating an ldap ping. You can run
|
5
|
+
# this program via the example:ldap task. Modify as you see fit.
|
6
|
+
########################################################################
|
7
|
+
require 'net/ping/ldap'
|
8
|
+
|
9
|
+
good = 'ldap://localhost'
|
10
|
+
bad = 'ldap://example.com'
|
11
|
+
|
12
|
+
puts "== Good ping (if you have an ldap server at #{good})"
|
13
|
+
|
14
|
+
p1 = Net::Ping::LDAP.new(good)
|
15
|
+
p p1.ping?
|
16
|
+
|
17
|
+
puts "== Bad ping"
|
18
|
+
|
19
|
+
p2 = Net::Ping::LDAP.new(bad)
|
20
|
+
p p2.ping?
|
21
|
+
p p2.warning
|
22
|
+
p p2.exception
|
data/examples/example_pingtcp.rb
CHANGED
@@ -1,16 +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?
|
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?
|
data/examples/example_pingudp.rb
CHANGED
@@ -1,12 +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?
|
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
CHANGED
@@ -1,15 +1,18 @@
|
|
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
|
-
|
14
|
-
|
15
|
-
|
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
|
+
require File.join(File.dirname(__FILE__), 'ping/ldap')
|
13
|
+
|
14
|
+
RbConfig = Config unless Object.const_defined?(:RbConfig)
|
15
|
+
|
16
|
+
if RbConfig::CONFIG['host_os'] =~ /msdos|mswin|cygwin|mingw|win32|windows/i
|
17
|
+
require File.join(File.dirname(__FILE__), 'ping/wmi')
|
18
|
+
end
|
data/lib/net/ping/external.rb
CHANGED
@@ -1,126 +1,122 @@
|
|
1
|
-
require '
|
2
|
-
require
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
# The
|
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
|
-
when /
|
45
|
-
pstring += "
|
46
|
-
when /
|
47
|
-
pstring += "#{host} 1"
|
48
|
-
when /
|
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
|
-
|
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
|
-
alias ping? ping
|
124
|
-
alias pingecho ping
|
125
|
-
end
|
126
|
-
end
|
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
|
data/lib/net/ping/http.rb
CHANGED
@@ -1,120 +1,149 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'ping')
|
2
|
-
require 'net/http'
|
3
|
-
require 'net/https'
|
4
|
-
require 'uri'
|
5
|
-
|
6
|
-
# Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
|
7
|
-
# Windows because it (ironically) causes blocking problems.
|
8
|
-
unless File::ALT_SEPARATOR
|
9
|
-
require 'resolv-replace'
|
10
|
-
end
|
11
|
-
|
12
|
-
# The Net module serves as a namespace only.
|
13
|
-
module Net
|
14
|
-
|
15
|
-
# The Ping::HTTP class encapsulates methods for HTTP pings.
|
16
|
-
class Ping::HTTP < Ping
|
17
|
-
|
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
|
-
|
24
|
-
# The maximum number of redirects allowed. The default is 5.
|
25
|
-
attr_accessor :redirect_limit
|
26
|
-
|
27
|
-
# The user agent used for the HTTP request. The default is nil.
|
28
|
-
attr_accessor :user_agent
|
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
|
-
|
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
|
-
|
1
|
+
require File.join(File.dirname(__FILE__), 'ping')
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
# Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
|
7
|
+
# Windows because it (ironically) causes blocking problems.
|
8
|
+
unless File::ALT_SEPARATOR
|
9
|
+
require 'resolv-replace'
|
10
|
+
end
|
11
|
+
|
12
|
+
# The Net module serves as a namespace only.
|
13
|
+
module Net
|
14
|
+
|
15
|
+
# The Ping::HTTP class encapsulates methods for HTTP pings.
|
16
|
+
class Ping::HTTP < Ping
|
17
|
+
|
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
|
+
|
24
|
+
# The maximum number of redirects allowed. The default is 5.
|
25
|
+
attr_accessor :redirect_limit
|
26
|
+
|
27
|
+
# The user agent used for the HTTP request. The default is nil.
|
28
|
+
attr_accessor :user_agent
|
29
|
+
|
30
|
+
# OpenSSL certificate verification mode. The default is VERIFY_NONE.
|
31
|
+
attr_accessor :ssl_verify_mode
|
32
|
+
|
33
|
+
# Use GET request instead HEAD. The default is false.
|
34
|
+
attr_accessor :get_request
|
35
|
+
|
36
|
+
# Creates and returns a new Ping::HTTP object. The default port is the
|
37
|
+
# port associated with the URI. The default timeout is 5 seconds.
|
38
|
+
#
|
39
|
+
def initialize(uri=nil, port=nil, timeout=5)
|
40
|
+
@follow_redirect = true
|
41
|
+
@redirect_limit = 5
|
42
|
+
@ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
|
43
|
+
@get_request = false
|
44
|
+
|
45
|
+
port ||= URI.parse(uri).port if uri
|
46
|
+
|
47
|
+
super(uri, port, timeout)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Looks for an HTTP response from the URI passed to the constructor.
|
51
|
+
# If the result is a kind of Net::HTTPSuccess then the ping was
|
52
|
+
# successful and true is returned. Otherwise, false is returned
|
53
|
+
# and the Ping::HTTP#exception method should contain a string
|
54
|
+
# indicating what went wrong.
|
55
|
+
#
|
56
|
+
# If the HTTP#follow_redirect accessor is set to true (which it is
|
57
|
+
# by default) and a redirect occurs during the ping, then the
|
58
|
+
# HTTP#warning attribute is set to the redirect message, but the
|
59
|
+
# return result is still true. If it's set to false then a redirect
|
60
|
+
# response is considered a failed ping.
|
61
|
+
#
|
62
|
+
# If no file or path is specified in the URI, then '/' is assumed.
|
63
|
+
#
|
64
|
+
def ping(host = @host)
|
65
|
+
super(host)
|
66
|
+
bool = false
|
67
|
+
uri = URI.parse(host)
|
68
|
+
|
69
|
+
start_time = Time.now
|
70
|
+
|
71
|
+
response = do_ping(uri)
|
72
|
+
|
73
|
+
if response.is_a?(Net::HTTPSuccess)
|
74
|
+
bool = true
|
75
|
+
elsif redirect?(response) # Check code, HTTPRedirection does not always work
|
76
|
+
if @follow_redirect
|
77
|
+
@warning = response.message
|
78
|
+
rlimit = 0
|
79
|
+
|
80
|
+
while redirect?(response)
|
81
|
+
if rlimit >= redirect_limit
|
82
|
+
@exception = "Redirect limit exceeded"
|
83
|
+
break
|
84
|
+
end
|
85
|
+
redirect = URI.parse(response['location'])
|
86
|
+
redirect = uri + redirect if redirect.relative?
|
87
|
+
response = do_ping(redirect)
|
88
|
+
rlimit += 1
|
89
|
+
end
|
90
|
+
|
91
|
+
if response.is_a?(Net::HTTPSuccess)
|
92
|
+
bool = true
|
93
|
+
else
|
94
|
+
@warning = nil
|
95
|
+
@exception ||= response.message
|
96
|
+
end
|
97
|
+
|
98
|
+
else
|
99
|
+
@exception = response.message
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# There is no duration if the ping failed
|
104
|
+
@duration = Time.now - start_time if bool
|
105
|
+
|
106
|
+
bool
|
107
|
+
end
|
108
|
+
|
109
|
+
alias ping? ping
|
110
|
+
alias pingecho ping
|
111
|
+
alias follow_redirect? follow_redirect
|
112
|
+
alias uri host
|
113
|
+
alias uri= host=
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def redirect?(response)
|
118
|
+
response && response.code.to_i >= 300 && response.code.to_i < 400
|
119
|
+
end
|
120
|
+
|
121
|
+
def do_ping(uri)
|
122
|
+
response = nil
|
123
|
+
begin
|
124
|
+
uri_path = uri.path.empty? ? '/' : uri.path
|
125
|
+
headers = { }
|
126
|
+
headers["User-Agent"] = user_agent unless user_agent.nil?
|
127
|
+
Timeout.timeout(@timeout) do
|
128
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
129
|
+
|
130
|
+
if uri.scheme == 'https'
|
131
|
+
http.use_ssl = true
|
132
|
+
http.verify_mode = @ssl_verify_mode
|
133
|
+
end
|
134
|
+
|
135
|
+
if @get_request == true
|
136
|
+
request = Net::HTTP::Get.new(uri_path)
|
137
|
+
else
|
138
|
+
request = Net::HTTP::Head.new(uri_path)
|
139
|
+
end
|
140
|
+
|
141
|
+
response = http.start{ |h| h.request(request) }
|
142
|
+
end
|
143
|
+
rescue Exception => err
|
144
|
+
@exception = err.message
|
145
|
+
end
|
146
|
+
response
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|