Skip to content

Instantly share code, notes, and snippets.

@mohan-mu
Forked from djromero/monitor-page.sh
Created August 1, 2017 11:34
Show Gist options
  • Save mohan-mu/8639c8a1cc14010738b2d5354caa7e0f to your computer and use it in GitHub Desktop.
Save mohan-mu/8639c8a1cc14010738b2d5354caa7e0f to your computer and use it in GitHub Desktop.
Send email and tweet when a web page changes in bash.
#!/bin/bash
# inspired in "Monitoring a web page for changes using bash"
# http://bhfsteve.blogspot.com.es/2013/03/monitoring-web-page-for-changes-using.html
# monitor.sh - Monitors a web page for changes
# sends an email notification and a tweet if the file changes
# requirements:
# - twitter from the command line [https://github.com/sferik/t]
# gem install t
# - sendemail
# sudo apt-get install libio-socket-ssl-perl libcrypt-ssleay-perl libnet-ssleay-perl
# sudo apt-get sendemail
URL="http://example.com/monitored/page.html"
TO="email@example.com another@example.com"
HANDLER="@user @friend"
USERNAME="sender@gmail.com"
PASSWORD="secret!"
while true ; do
if [ -f new.html ] ; then
mv new.html old.html 2> /dev/null
fi
curl -s $URL -L --compressed > new.html
# for better results add a grep to exclude lines that always change (like a date, etc.)
# | egrep -v 'this|that'
DIFF_OUTPUT=`diff --suppress-common-lines -y new.html old.html`
if [ "x" != "x${DIFF_OUTPUT}" ] ; then
t update "${HANDLER} hey, the webpage $URL just changed"
sendEmail \
-t $TO \
-f $USERNAME \
-s smtp.gmail.com:587 -xu $USERNAME -xp $PASSWORD \
-o tls=yes \
-u "The page changed" \
-m "Visit it at $URL\n\n${DIFF_OUTPUT}"
fi
sleep 300
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment