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.
@@ -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
@@ -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?
@@ -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
- if Config::CONFIG['host_os'] =~ /msdos|mswin|cygwin|mingw|win32|windows/i
14
- require File.join(File.dirname(__FILE__), 'ping/wmi')
15
- 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
+ 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
@@ -1,126 +1,122 @@
1
- require 'rbconfig'
2
- require File.join(File.dirname(__FILE__), 'ping')
3
-
4
- if Config::CONFIG['host_os'] =~ /mswin|win32|msdos|cygwin|mingw|windows/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'
11
- else
12
- require 'open3'
13
- end
14
-
15
- # The Net module serves as a namespace only.
16
- module Net
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|windows/i &&
22
- RUBY_PLATFORM != 'java'
23
-
24
- if CWINDOWS
25
- include Windows::Console
26
- end
27
-
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)
37
-
38
- stdin, stdout, stderr = ""
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
64
-
65
- Timeout.timeout(@timeout){
66
- stdin, stdout, stderr = Open3.popen3(pstring)
67
- err = stderr.gets # Can't chomp yet, might be nil
68
- }
69
-
70
- stdin.close
71
- stderr.close
72
-
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 = stdout.readlines
87
- stdout.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
98
-
99
- lines.each{ |line|
100
- if regexp.match(line)
101
- @exception = line.chomp
102
- break
103
- end
104
- }
105
-
106
- bool = true unless @exception
107
- end
108
- end
109
- rescue Exception => error
110
- @exception = error.message
111
- ensure
112
- stdin.close if stdin && !stdin.closed?
113
- stdout.close if stdout && !stdout.closed?
114
- stderr.close if stderr && !stderr.closed?
115
- end
116
-
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
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
- # Creates and returns a new Ping::HTTP object. The default port is the
31
- # port associated with the URI. The default timeout is 5 seconds.
32
- #
33
- def initialize(uri=nil, port=nil, timeout=5)
34
- @follow_redirect = true
35
- @redirect_limit = 5
36
-
37
- port ||= URI.parse(uri).port if uri
38
-
39
- super(uri, port, timeout)
40
- end
41
-
42
- # Looks for an HTTP response from the URI passed to the constructor.
43
- # If the result is a kind of Net::HTTPSuccess then the ping was
44
- # successful and true is returned. Otherwise, false is returned
45
- # and the Ping::HTTP#exception method should contain a string
46
- # indicating what went wrong.
47
- #
48
- # If the HTTP#follow_redirect accessor is set to true (which it is
49
- # by default) and a redirect occurs during the ping, then the
50
- # HTTP#warning attribute is set to the redirect message, but the
51
- # return result is still true. If it's set to false then a redirect
52
- # response is considered a failed ping.
53
- #
54
- # If no file or path is specified in the URI, then '/' is assumed.
55
- #
56
- def ping(host = @host)
57
- super(host)
58
- bool = false
59
- uri = URI.parse(host)
60
-
61
- start_time = Time.now
62
-
63
- begin
64
- response = nil
65
- uri_path = uri.path.empty? ? '/' : uri.path
66
- headers = { }
67
- headers["User-Agent"] = user_agent unless user_agent.nil?
68
- Timeout.timeout(@timeout) do
69
- http = Net::HTTP.new(uri.host, uri.port)
70
- http.use_ssl = (uri.scheme == 'https')
71
- request = Net::HTTP::Get.new(uri_path)
72
- response = http.start{ |h| h.request(request) }
73
- end
74
- rescue Exception => err
75
- @exception = err.message
76
- else
77
- if response.is_a?(Net::HTTPSuccess)
78
- bool = true
79
- else
80
- if @follow_redirect
81
- @warning = response.message
82
- rlimit = 0
83
-
84
- # Any response code in the 300 range is a redirect
85
- while response.code.to_i >= 300 && response.code.to_i < 400
86
- if rlimit >= redirect_limit
87
- @exception = "Redirect limit exceeded"
88
- break
89
- end
90
- redirect = URI.parse(response['location'])
91
- redirect = uri + redirect if redirect.relative?
92
- response = Net::HTTP.get_response(redirect.host, redirect.path, @port)
93
- rlimit += 1
94
- end
95
-
96
- if response.is_a?(Net::HTTPSuccess)
97
- bool = true
98
- else
99
- @warning = nil
100
- @exception ||= response.message
101
- end
102
- else
103
- @exception = response.message
104
- end
105
- end
106
- end
107
-
108
- # There is no duration if the ping failed
109
- @duration = Time.now - start_time if bool
110
-
111
- bool
112
- end
113
-
114
- alias ping? ping
115
- alias pingecho ping
116
- alias follow_redirect? follow_redirect
117
- alias uri host
118
- alias uri= host=
119
- end
120
- end
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