summaryrefslogtreecommitdiff
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
commit7d6b92ccf06dcc202ed344ec3068cda02165142a (patch)
tree0ebb2a11ffdfbd5e71870274ead441f852a042a7
parent3241390ec1c5ac184b7cbdb7df82d05951e46180 (diff)
Split the implementation of the multi-ping tool into a library and a driver.
-rwxr-xr-xbin/multi-ping58
-rw-r--r--lib/custodian/multiping.rb94
2 files changed, 106 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.
-#
diff --git a/lib/custodian/multiping.rb b/lib/custodian/multiping.rb
new file mode 100644
index 0000000..adb246e
--- /dev/null
+++ b/lib/custodian/multiping.rb
@@ -0,0 +1,94 @@
+
+require 'getoptlong'
+require 'socket'
+
+
+
+class MultiPing
+
+ #
+ # The hostname we'll ping, and the resolved address.
+ #
+ attr_reader :hostname, :resolved
+
+ def initialize( hostname )
+ @hostname = hostname
+ @resolved = resolve_hostname( hostname )
+ end
+
+
+ #
+ #
+ #
+ def resolve_hostname( hostname )
+ res = nil
+
+ begin
+ Socket.getaddrinfo(hostname, 'echo').each do |a|
+ res = a[3]
+ end
+ rescue SocketError
+ end
+
+ res
+ end
+
+
+
+ #
+ # Return the resolved address
+ #
+ def address
+ @resolved
+ end
+
+
+
+ #
+ # Does the hostname resolve to an IPv4 address?
+ #
+ def is_ipv4?
+ if ( ( ! @resolved.nil? ) && ( @resolved =~ /^([0-9]+).([0-9]+).([0-9]+).([0-9]+)$/ ) )
+ true
+ else
+ false
+ end
+ end
+
+
+ #
+ # Does the hostname resolve to an IPv6 address?
+ #
+ def is_ipv6?
+ if ( ( ! @resolved.nil? ) && ( @resolved =~ /^2001/ ) )
+ true
+ else
+ false
+ end
+ end
+
+
+
+ #
+ # Run the ping - if it succeeds return "true".
+ #
+ # Return false on error.
+ #
+ def run_ping
+ if ( is_ipv6? )
+ if ( system( "ping6 -c 1 #{@resolved} 2>/dev/null >/dev/null" ) == true )
+ return true
+ end
+ elsif( is_ipv4? )
+ if ( system( "ping -c 1 #{@resolved} 2>/dev/null >/dev/null" ) == true )
+ return true
+ end
+ else
+ puts "ERROR: Resolved to neither an IPv6 or IPv4 address."
+ end
+ return false
+ end
+
+
+end
+