Skip to content

Instantly share code, notes, and snippets.

@aapis
Last active October 26, 2016 21:54
Show Gist options
  • Save aapis/10de58e91dba8139706705e2f1d7a2e4 to your computer and use it in GitHub Desktop.
Save aapis/10de58e91dba8139706705e2f1d7a2e4 to your computer and use it in GitHub Desktop.
Simple website checker; compares the site's URL against a known string
#!/usr/bin/ruby
require "open-uri"
require "nokogiri"
NUM_HITS = 50
def _request(url)
begin
open(url, :read_timeout => 5).read
rescue => e
nil
end
end
def query(url, comparable)
puts "Checking #{url}..."
errors = 0
timeouts = 0
for i in NUM_HITS.downto(0)
begin
resp = _request(url)
html = Nokogiri::HTML(resp)
title = html.css("title").text
errors += 1 unless (title == comparable)
timeouts += 1 if resp.nil?
rescue Interrupt
puts "Quit early"
exit(1)
end
end
puts " - Error ratio: #{errors} / #{NUM_HITS - timeouts} (#{(errors.to_f/NUM_HITS.to_f).to_f * 100}%)"
if timeouts == 1
puts " - 1 request timed out"
elsif timeouts > 1
puts " - #{timeouts} requests timed out"
end
end
if !ARGV.empty?
query(ARGV[0], ARGV[1])
else
puts "Missing some args"
puts " - First arg: The URL you want to test"
puts " - Second arg: The page title you want to check"
puts "Usage:"
puts "./verify https://example.com \"Example Domain\""
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment