-
-
Save RakhithJK/29c856dc03c4f0674141fe7fad3a70de 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! ;)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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