Skip to content

Instantly share code, notes, and snippets.

@daniel-marschner
Created November 2, 2012 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daniel-marschner/4001503 to your computer and use it in GitHub Desktop.
Save daniel-marschner/4001503 to your computer and use it in GitHub Desktop.
Checks if the given domain is reachable as long as there is a 200 OK coming back. Super simple! ;)
require 'net/http'
require 'date'
# Fetches the first argument passed to the script, if no is given
# it raises and error with this simple custom message.
unless @domain = ARGV.first
raise "You need to pass in a domain like www.6wunderkinder.com"
end
puts "[#{DateTime.now}] Started the DNS check for #{@domain}..."
# Sends a simple GET request to the given domain and checks
# if the response was 200 OK.
def do_request
uri = URI("http://#{@domain}")
response = Net::HTTP.get_response(uri)
response.is_a?(Net::HTTPOK)
end
# Puts a NotFound message and sleeps for 60 seconds as long as the
# request is not a 200 OK. If there is a 200 OK coming back it puts
# the success message and the script terminates.
begin
while do_request == false
puts "[#{DateTime.now}] NotFound: #{@domain}..."
sleep 60
end
puts "[#{DateTime.now}] Success: #{@domain} is up and running!"
puts "[#{DateTime.now}] Done."
# If something went wrong, any error occurs the script terminates
# with a error message.
rescue => e
puts "[#{DateTime.now}] Error: We couldn't reach #{@domain}!"
puts "[#{DateTime.now}] Error: #{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