#!/usr/bin/perl -w # Using MD5 to check if a web page has changed, and notifying on change. # Author: Chris Ball use strict; use LWP::Simple; use Digest::MD5 qw(md5_base64); use Mail::Sendmail; # Get some vars. if (scalar @ARGV != 4) { print "Usage: perl $0 \n"; exit; } my ($page, $remote_address, $from_address, $local_file) = @ARGV; # Things to do. my $content = &download_page($page); my $changed = &checkmd5($content, $local_file); &mail_notify($remote_address, $from_address, $page) if $changed; print "\n$0 completed.\n"; exit; sub download_page { my $query = shift; print "Downloading $query.\n"; my $content = get("$query") or die "Eek: $!"; print "Page downloaded.\n"; return $content; } sub checkmd5{ my ($md5_content, $file) = @_; my $newmd5 = md5_base64($md5_content) or die "Wibble: $!"; my $oldmd5 = 0; # Compare with previous checksum if (-f $file && ! -z $file) { open(MD5FILE, "<$file"); while() { $oldmd5 = $1 if ( m/^md5=(.*)$/ ); } close MD5FILE; } # Write new checksum open(MD5FILE, ">$file"); print MD5FILE "md5=$newmd5"; close MD5FILE; if ($oldmd5 ne $newmd5) { print "md5sums /don't/ match - mailing.\n"; return 1; } else { print "md5sums match. Exiting gracefully.\n"; return 0; } } sub mail_notify { my ($remote_address, $from_address, $page) = @_; my %mail = ( To => $remote_address, From => $from_address, Subject => "Update: $page", Message => "The web page you asked me to keep me track of - $page - has been updated. Just thought I'd let you know.\n\n$0, on $ENV{HOST}." ); sendmail(%mail) or die "Couldn't send: $!"; print "Mail sent!\n"; }