diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-11-09 09:11:52 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-11-09 09:11:52 +0000 |
commit | ee5ab9cc136baf18756abc6bd5d136a0231f973e (patch) | |
tree | abdc351cbffd7b67f63f41ea08f544819af0a28b /lib/ping.rb | |
parent | dc8da69e7cbc54f020ef1182fd28e57bbaa40fc3 (diff) |
1.1c7
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@316 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/ping.rb')
-rw-r--r-- | lib/ping.rb | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/lib/ping.rb b/lib/ping.rb index d742a50f99..96919c004b 100644 --- a/lib/ping.rb +++ b/lib/ping.rb @@ -33,23 +33,26 @@ # #=end +require 'timeout' + module Ping require "socket" def pingecho(host, timeout=5) begin - x = Thread.current - y = Thread.start { - sleep timeout - x.raise RuntimeError if x.status - } - s = TCPsocket.new(host, "echo") - s.close - return TRUE + timeout(timeout) do + s = TCPsocket.new(host, "echo") + s.close + end rescue - return FALSE; - ensure - Thread.kill y if y.status + return false end + return true end module_function "pingecho" end + +if $0 == __FILE__ + host = ARGV[0] + host ||= "localhost" + printf("%s alive? - %s\n", host, Ping.pingecho(host, 5)) +end |