summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Kemp <steve@steve.org.uk>2012-11-22 06:42:07 +0000
committerSteve Kemp <steve@steve.org.uk>2012-11-22 06:42:07 +0000
commit80b250e226472b3d7d71c194ea1cd2b02f52885c (patch)
tree7536362b6aa9c1f6c90c75d4b56b6533e73d7080
parent5c92238fa95976cc6445eea3f964a8602bd939c8 (diff)
Renamed.
-rwxr-xr-xbin/multi-ping4
-rw-r--r--lib/custodian/dnsutil.rb72
-rw-r--r--lib/custodian/util/dns.rb79
-rw-r--r--lib/custodian/util/ping.rb88
4 files changed, 169 insertions, 74 deletions
diff --git a/bin/multi-ping b/bin/multi-ping
index 06232e4..3e4cbf4 100755
--- a/bin/multi-ping
+++ b/bin/multi-ping
@@ -33,7 +33,7 @@
require 'getoptlong'
-require 'custodian/multiping'
+require 'custodian/util/ping'
@@ -115,7 +115,7 @@ end
#
# Create the object
#
-helper = MultiPing.new( hostname )
+helper = Custodian::Util::Ping.new( hostname )
#
diff --git a/lib/custodian/dnsutil.rb b/lib/custodian/dnsutil.rb
deleted file mode 100644
index e42d5e3..0000000
--- a/lib/custodian/dnsutil.rb
+++ /dev/null
@@ -1,72 +0,0 @@
-
-
-require 'ipaddr'
-require 'socket'
-require 'timeout'
-
-
-
-#
-# This class is responsible for doing forward/reverse DNS lookups
-#
-class DNSUtil
-
-
- #
- # Return the reverse DNS for the specified IP address, nil on failure.
- #
- def DNSUtil.ip_to_hostname( ip )
- resolved = nil
- begin
- timeout( 4 ) do
- begin
- Socket.getaddrinfo(ip, 'echo').each do |a|
- resolved = a[2] if ( a )
- end
- rescue SocketError
- resolved = nil
- end
- end
- rescue Timeout::Error => e
- resolved = nil
- end
- resolved
- end
-
-
- #
- # Convert a hostname to an IP address, return nil on failure.
- #
- def DNSUtil.hostname_to_ip( hostname )
-
- resolved = nil
-
- begin
- timeout( 4 ) do
- begin
- Socket.getaddrinfo(hostname, 'echo').each do |a|
- resolved = a[3] if ( a )
- end
- rescue SocketError
- resolved = nil
- end
- end
- rescue Timeout::Error => e
- resolved = nil
- end
- resolved
- end
-
-end
-
-
-if __FILE__ == $0 then
-
- %w( ssh.steve.org.uk ipv6.steve.org.uk ).each do |name|
- puts "Hostname test: #{name} #{DNSUtil.hostname_to_ip(name) }"
- end
-
- %w( 80.68.85.46 80.68.85.48 2001:41c8:125:46::22 ).each do |name|
- puts "Hostname test: #{name} #{DNSUtil.ip_to_hostname(name) }"
- end
-end
diff --git a/lib/custodian/util/dns.rb b/lib/custodian/util/dns.rb
new file mode 100644
index 0000000..901dae0
--- /dev/null
+++ b/lib/custodian/util/dns.rb
@@ -0,0 +1,79 @@
+
+
+require 'ipaddr'
+require 'socket'
+require 'timeout'
+
+
+
+#
+# This class is responsible for doing forward/reverse DNS lookups
+#
+module Custodian
+ module Util
+ class DNS
+
+
+ #
+ # Return the reverse DNS for the specified IP address, nil on failure.
+ #
+ def DNS.ip_to_hostname( ip )
+ resolved = nil
+ begin
+ timeout( 4 ) do
+ begin
+ Socket.getaddrinfo(ip, 'echo').each do |a|
+ resolved = a[2] if ( a )
+ end
+ rescue SocketError
+ resolved = nil
+ end
+ end
+ rescue Timeout::Error => e
+ resolved = nil
+ end
+ resolved
+ end
+
+
+ #
+ # Convert a hostname to an IP address, return nil on failure.
+ #
+ def DNS.hostname_to_ip( hostname )
+
+ resolved = nil
+
+ begin
+ timeout( 4 ) do
+ begin
+ Socket.getaddrinfo(hostname, 'echo').each do |a|
+ resolved = a[3] if ( a )
+ end
+ rescue SocketError
+ resolved = nil
+ end
+ end
+ rescue Timeout::Error => e
+ resolved = nil
+ end
+ resolved
+ end
+
+ end
+
+ end
+end
+
+
+
+
+if __FILE__ == $0 then
+
+ %w( ssh.steve.org.uk ipv6.steve.org.uk ).each do |name|
+ puts "Hostname test: #{name} #{Custodian::Util::DNS.hostname_to_ip(name) }"
+ end
+
+ %w( 80.68.85.46 80.68.85.48 2001:41c8:125:46::22 ).each do |name|
+ puts "Hostname test: #{name} #{Custodian::Util::DNS.ip_to_hostname(name) }"
+ end
+end
diff --git a/lib/custodian/util/ping.rb b/lib/custodian/util/ping.rb
new file mode 100644
index 0000000..b3fa7f4
--- /dev/null
+++ b/lib/custodian/util/ping.rb
@@ -0,0 +1,88 @@
+
+require 'custodian/util/dns'
+
+
+
+#
+# This class has methods to determine whether the target
+# of a hostname/IP address is IPv4 or IPv6.
+#
+# Assuming the address resolves to one of those two types
+# it can invoke on of /usr/bin/ping or /usr/bin/ping6 appropriately.
+#
+module Custodian
+ module Util
+ class Ping
+
+ #
+ # The hostname we'll ping, and the resolved address.
+ #
+ attr_reader :hostname, :resolved
+
+ #
+ # Save the hostname away, resolve it if possible.
+ #
+ def initialize( hostname )
+ @hostname = hostname
+ @resolved = Custodian::Util::DNS.hostname_to_ip( hostname )
+ 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 =~ /^([a-f0-9:]+)$/i ) )
+ 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
+
+ end
+end