#!/bin/bash ############################################################################### # check_url 2002-jan-16 - gash - http://www.moosenugget.org # # # # # # simple bash2 script that incorporates curl / md5sum to notify the user # # when a url has changed # # # # curl http://curl.haxx.se # # md5sum http://www.gnu.org/directory/textutils.html # # # # command line options: # # -s ... exit after one sweep # ############################################################################### clear export PATH="/usr/bin:/bin:/usr/local/bin:/usr/sbin:/sbin:/usr/local/sbin" ### where to store the data files DATADIR="/tmp" # note: no trailing / ### this file is used to log the pages that have been updated, ### in case the user is afk... updatelog="$DATADIR/check_url.log" debug="0" curltimeout=30 pages="http://www.whitehouse.gov http://www.house.gov" ### how long to sleep between sweeps (seconds) sleeptime=7200 ### hey look, i'm theme-able!! :) spinners=". o 0 O" #spinners="| / - \\" function say { date=`date '+%Y-%b-%d %T'` echo -e "[${date}]:\n $@\n" } bash_ver=`echo $BASH_VERSION | awk -v FS='.' '{print $1}'` if [ "$bash_ver" = "1" ] ; then say "This script requires BASH 2.0 or greater:\n http://www.gnu.org/directory/bash.html" exit fi required_proggies="curl md5sum echo sed cat diff sleep rm" for a in $required_proggies ; do which $a 1> /dev/null 2> /dev/null if [ $? = 1 ] ; then say "required program not found: $a." ; exit 1 ; fi done echo "Monitoring URLs:" for page in $pages ; do echo " o $page" done echo "" while [ 1 ] ; do for page in $pages ; do pagefile=`echo $page | sed 's/\///g'` pagefile="$DATADIR/$pagefile" if [ "$debug" = "1" ] ; then echo "Converted: $pagefile" ; fi if [ -f "$pagefile" ] ; then if [ "$debug" = "1" ] ; then echo -n "Checking: $page..." ; fi curl --connect-timeout $curltimeout -o ${pagefile}.new $page 1> /dev/null 2> /dev/null if [ $? = 0 ] ; then ### this keeps md5sum from creating a filename within the md5sum file cat ${pagefile}.new | md5sum > ${pagefile}.new.md5 diff -u ${pagefile}.md5 ${pagefile}.new.md5 1> /dev/null 2> /dev/null if [ $? = 0 ] ; then rm ${pagefile}.new ${pagefile}.new.md5 if [ "$debug" = "1" ] ; then echo "unchanged." ; fi else ### log the update date=`date '+%Y-%b-%d %T'` echo -e "[${date}]:\n $page has changed\n" >> $updatelog diff -u $pagefile ${pagefile}.new >> $updatelog echo -e "--------------------------------------------------------------------------------\n" >> $updatelog ### notify the user say "\a\a\a$page has changed!\n (see $updatelog for details)" mv ${pagefile}.new $pagefile mv ${pagefile}.new.md5 ${pagefile}.md5 fi else say "Error ($?): $page retreival failed." fi else date=`date '+%Y-%b-%d %T'` echo -e -n "[${date}]:\n $page doesn't exist locally, getting..." curl --connect-timeout $curltimeout -o $pagefile $page 1> /dev/null 2> /dev/null if [ $? = 0 ] ; then echo -e "done.\n" ### this keeps md5sum from creating a filename within the md5sum file cat $pagefile | md5sum > ${pagefile}.md5 else date=`date '+%Y-%b-%d %T'` echo -e "\n\n[${date}]:\n Error ($?): $page failed, unable to create local copy.\n" fi fi done ### exit after one sweep if [ "$1" = "-s" -o "$1" = "-S" ] ; then echo -e "Exiting.\n" exit fi ### do we have a terminal? if [ -t 1 ]; then echo -n "Sleeping..." count=1 while [ $count -lt $sleeptime ] ; do for spinner in $spinners ; do echo -n "$spinner" sleep 1s echo -e -n "\b" done # count += # spinners let count=$count+4 done echo -e -n "\r \r" else sleep ${sleeptime}s fi done # EOF