How to receive an alert everytime a website changes…

I’ve been trying to learn how to use Perl to do some simple stuff whenever I just cannot use PHP. PERL is a language that I have access on every machine under my control, but unfortunately I cannot say the same thing about PHP and that is why I have decided to start learning using this ultra flexible language.

Now, the first thing I have developed (aside from the popular Hello World – LOL) was a simple script to check for website updates. This was particularly useful to check for updates on a website related to a contest.

use strict;
use warnings;
use LWP::Simple;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use Encode qw(encode_utf8);
 
my $link                = 'http://www.foobar.com';
my $email_from          = '[email protected]';
my $email               = '[email protected]';
my $email_subject       = 'Website Changed!';
my $email_body          = 'Hello! I just want to let you know that the website ' 
                          . $link . ' have just changed!';
my $tmp_file            = '/tmp/stored_hash';
 
my $web_source = get( $link );
my $current_hash =  md5_hex(encode_utf8($web_source));
 
if(-e $tmp_file)
{
        open FH, "<$tmp_file" or die "could not open: $!\n";
        my $mod_hash = <FH>;
        if( $current_hash ne $mod_hash )
        {
                open(MAIL, "|/usr/sbin/sendmail -t");
                print MAIL "To: $email\n";
                print MAIL "From: $email_from\n";
                print MAIL "Subject: $email_subject\n\n";
                print MAIL "$email_body\n";
                close(MAIL);
 
        }
        close FH;
}
        open FH, ">$tmp_file" or die "could not create: $!\n";
        print FH $current_hash;
        close FH;

Just replace the email and link stuff to suite your needs and you are all set! Enjoy!

Leave a Reply

Your email address will not be published.