summaryrefslogtreecommitdiff
path: root/lib/custodian/util/dns.rb
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
commit6b777872b8d0ec8506ef90bc121eb70a8099db80 (patch)
tree7536362b6aa9c1f6c90c75d4b56b6533e73d7080 /lib/custodian/util/dns.rb
parent2d0160537ebfacc391d395f7d1ac0a4e02543c74 (diff)
Renamed.
Diffstat (limited to 'lib/custodian/util/dns.rb')
-rw-r--r--lib/custodian/util/dns.rb79
1 files changed, 79 insertions, 0 deletions
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