summaryrefslogtreecommitdiff
path: root/lib/custodian/multiping.rb
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 /lib/custodian/multiping.rb
parent3241390ec1c5ac184b7cbdb7df82d05951e46180 (diff)
Split the implementation of the multi-ping tool into a library and a driver.
Diffstat (limited to 'lib/custodian/multiping.rb')
-rw-r--r--lib/custodian/multiping.rb94
1 files changed, 94 insertions, 0 deletions
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
+