From 7d6b92ccf06dcc202ed344ec3068cda02165142a Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Wed, 14 Nov 2012 17:29:55 +0000 Subject: Split the implementation of the multi-ping tool into a library and a driver. --- bin/multi-ping | 58 ++++++++++++---------------------------------------------- 1 file changed, 12 insertions(+), 46 deletions(-) (limited to 'bin') diff --git a/bin/multi-ping b/bin/multi-ping index ab81d86..03eadca 100755 --- a/bin/multi-ping +++ b/bin/multi-ping @@ -31,7 +31,7 @@ require 'getoptlong' -require 'socket' +require 'custodian/multiping' @@ -93,13 +93,15 @@ end + # # Get the address to ping. # hostname = ARGV.shift + # -# If we have no host then abort +# Abort if we don't have a hostname # if ( hostname.nil? ) puts "Usage: #{$0} hostname" @@ -107,55 +109,19 @@ if ( hostname.nil? ) end -# -# The IP we'll deal with -# -ip = nil - # -# Lookup the IP, catching any exception +# Create the object # -begin - Socket.getaddrinfo(hostname, 'echo').each do |a| - ip = a[3] - end -rescue SocketError - puts "Failed to resolve: #{hostname}" - exit 1 -end +helper = MultiPing.new( hostname ) # -# Was the result an IPv4 address? +# Ping the host, via the helper # -if ( ip =~ /^([0-9]+).([0-9]+).([0-9]+).([0-9]+)$/ ) - - # - # If so invoke "ping" - # - if ( system( "ping -c 1 #{ip} 2>/dev/null >/dev/null" ) == true ) - puts "#{hostname} alive." - exit 0 - else - puts "ping4 failed - #{hostname} [#{ip}]" - exit 1 - end -elsif ( ip =~ /2001/ ) - - # - # Was the result an IPv6 address? - # - if ( system( "ping6 -c 1 -w1 #{ip} 2>/dev/null >/dev/null" ) == true ) - puts "#{hostname} alive." - exit 0 - else - puts "ping6 failed - #{hostname} [#{ip}]" - exit 1 - end +if ( helper.run_ping() ) + puts "#{hostname} - #{helper.address} - is alive." + exit 0 +else + exit 1 end - - -# -# All done. -# -- cgit v1.2.1