Skip to content

Instantly share code, notes, and snippets.

@mkscrg
Created July 10, 2012 04:06
Show Gist options
  • Save mkscrg/3080929 to your computer and use it in GitHub Desktop.
Save mkscrg/3080929 to your computer and use it in GitHub Desktop.
Ruby script to periodically match a web page against a regex
require 'net/http'
$stdout.sync = true
if ARGV.include?("--help") || ![2,3,4].include?(ARGV.length)
puts "ruby checksite.rb URL REGEX [NOTIFY_EMAIL] [INTERVAL_SECS]"
exit!
end
uri = URI.parse(ARGV[0])
regex = Regexp.compile(ARGV[1])
interval = ARGV[2].to_i
email = ARGV[3]
loop do
puts "Getting page from " + uri.to_s
resp = Net::HTTP.get(uri)
if resp =~ regex
puts "Found a match!"
if email
puts "Emailing " + email
`echo "Go get em tiger! #{uri.to_s}" | sendmail #{email}`
else
puts "No email, doing nothing"
end
break
end
puts "No match."
if interval != 0
puts "Sleeping for " + interval.to_s + " seconds"
sleep(interval)
else
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment