summaryrefslogtreecommitdiff
path: root/bin/multi-ping
diff options
context:
space:
mode:
authorSteve Kemp <steve@steve.org.uk>2012-11-14 17:29:55 +0000
committerSteve Kemp <steve@steve.org.uk>2012-11-14 17:29:55 +0000
commit4b3abea480a7cc5e3ff559ba5987ecdbafbe1c82 (patch)
tree0ebb2a11ffdfbd5e71870274ead441f852a042a7 /bin/multi-ping
parent72843f543522bd5cb1ffd92a1a1c98b5ba71317e (diff)
Split the implementation of the multi-ping tool into a library and a driver.
Diffstat (limited to 'bin/multi-ping')
-rwxr-xr-xbin/multi-ping58
1 files changed, 12 insertions, 46 deletions
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.
-#