Skip to content

Instantly share code, notes, and snippets.

@michenriksen
Forked from daniel-marschner/gist:4001503
Created November 2, 2012 15:29
Show Gist options
  • Save michenriksen/4002006 to your computer and use it in GitHub Desktop.
Save michenriksen/4002006 to your computer and use it in GitHub Desktop.
Loops until given domain is resolvable. Used to check when new domains are live.
require 'resolv'
require 'date'
unless @domain = ARGV.first
puts "DNS Checker\n\n"
puts "Performs a DNS lookup on a domain until it can be resolved.\n\n"
puts "USAGE:"
puts " ruby #{__FILE__} <domain> [<sleep>]\n\n"
puts "The optional sleep argument defines the amount of time to wait between each"
puts "lookup attempt. If not specified, it defaults to 60 seconds."
exit
end
@sleep = (ARGV[1] || 60).to_i
puts "[#{DateTime.now}] Started DNS check for #{@domain}..."
# Public: Attempt to lookup the domain
#
# Returns IP address if resolved or false
def attempt_lookup
Resolv.getaddress(@domain)
rescue Resolv::ResolvError
false
end
begin
until ip_address = attempt_lookup
print '.'
sleep @sleep
end
puts "[#{DateTime.now}] Success: #{@domain} is pointing at #{ip_address}!"
puts "[#{DateTime.now}] Done."
rescue => e
puts "[#{DateTime.now}] Error: #{e.class}"
puts "[#{DateTime.now}] Message: #{e.message}"
puts "[#{DateTime.now}] Canceled."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment