def ping(host = @host)
super(host)
stdin, stdout, stderr = ""
pstring = "ping "
bool = false
orig_cp = nil
case Config::CONFIG['host_os']
when /linux|bsd|osx|mach|darwin/i
pstring += "-c 1 #{host}"
when /solaris|sunos/i
pstring += "#{host} 1"
when /hpux/i
pstring += "#{host} -n 1"
when /win32|windows|msdos|mswin|cygwin|mingw/i
if RUBY_PLATFORM != 'java'
orig_cp = GetConsoleCP()
SetConsoleCP(437) if orig_cp != 437
end
pstring += "-n 1 #{host}"
else
pstring += "#{host}"
end
start_time = Time.now
begin
err = nil
Timeout.timeout(@timeout){
stdin, stdout, stderr = Open3.popen3(pstring)
err = stderr.gets
}
stdin.close
stderr.close
if CWINDOWS && GetConsoleCP() != orig_cp
SetConsoleCP(orig_cp)
end
unless err.nil?
if err =~ /warning/i
@warning = err.chomp
bool = true
else
@exception = err.chomp
end
else
lines = stdout.readlines
stdout.close
if lines.nil? || lines.empty?
bool = true
else
regexp = /
no\ answer|
host\ unreachable|
could\ not\ find\ host|
request\ timed\ out|
100%\ packet\ loss
/ix
lines.each{ |line|
if regexp.match(line)
@exception = line.chomp
break
end
}
bool = true unless @exception
end
end
rescue Exception => error
@exception = error.message
ensure
stdin.close if stdin && !stdin.closed?
stdout.close if stdout && !stdout.closed?
stderr.close if stderr && !stderr.closed?
end
@duration = Time.now - start_time if bool
bool
end