1998-01-16 15:13:05 +03:00
|
|
|
#
|
|
|
|
# ping.rb -- check a host for upness
|
|
|
|
#
|
|
|
|
#= SYNOPSIS
|
|
|
|
#
|
|
|
|
# require 'ping'
|
|
|
|
# print "'jimmy' is alive and kicking\n" if Ping.pingecho('jimmy', 10) ;
|
|
|
|
#
|
|
|
|
#= DESCRIPTION
|
|
|
|
#
|
|
|
|
# This module contains routines to test for the reachability of remote hosts.
|
|
|
|
# Currently the only routine implemented is pingecho().
|
|
|
|
#
|
|
|
|
# pingecho() uses a TCP echo (I<not> an ICMP one) to determine if the
|
|
|
|
# remote host is reachable. This is usually adequate to tell that a remote
|
|
|
|
# host is available to rsh(1), ftp(1), or telnet(1) onto.
|
|
|
|
#
|
|
|
|
#== Parameters
|
|
|
|
#
|
|
|
|
# : hostname
|
|
|
|
#
|
|
|
|
# The remote host to check, specified either as a hostname or as an
|
|
|
|
# IP address.
|
|
|
|
#
|
|
|
|
# : timeout
|
|
|
|
#
|
|
|
|
# The timeout in seconds. If not specified it will default to 5 seconds.
|
|
|
|
#
|
1999-01-20 07:59:39 +03:00
|
|
|
# : service
|
|
|
|
#
|
|
|
|
# The service port to connect. The default is "echo".
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
#= WARNING
|
|
|
|
#
|
|
|
|
# pingecho() uses user-level thread to implement the timeout, so it may block
|
|
|
|
# for long period if named does not respond for some reason.
|
|
|
|
#
|
|
|
|
#=end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
require 'timeout'
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
module Ping
|
|
|
|
require "socket"
|
1999-01-20 07:59:39 +03:00
|
|
|
def pingecho(host, timeout=5, service="echo")
|
1998-01-16 15:13:05 +03:00
|
|
|
begin
|
1999-01-20 07:59:39 +03:00
|
|
|
timeout(timeout) do
|
|
|
|
s = TCPsocket.new(host, service)
|
|
|
|
s.close
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
rescue
|
1999-01-20 07:59:39 +03:00
|
|
|
return false
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
return true
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
module_function :pingecho
|
|
|
|
end
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
|
|
|
host = ARGV[0]
|
|
|
|
host ||= "localhost"
|
|
|
|
printf("%s alive? - %s\n", host, Ping::pingecho(host, 5))
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|