in reply to Log's and MD5 Hashes

Okay, after taking in some ideas and punching up some code.... I came out with this:

use Digest::MD5;
use Algorithm::Diff;
open CONFIGFILE, "<config.ini" or die "I cant open the config file.";
open RESULTS, ">md5TEMP" or die "I cant write the MD5's temp file.";
open OLDMD5, "<md5" or die "I cant read the MD5's file.";
while (<CONFIGFILE>) {
chomp;
$file = $_;
open(FILE, $file) or die "Can't open $file";
binmode(FILE);
$md5 = Digest::MD5->new;
while (<FILE>) {
$md5->add($_);
}
close(FILE);
print RESULTS $md5->b64digest, " ", $file, "\n";
print $md5->b64digest, "\n";
}
close RESULTS;
open RESULTS, "<md5TEMP" or die "I cant read the MD5's temp file.";
while (<OLDMD5>) {
$changes = diff (<OLDMD5>, <RESULTS>);
print $changes;
}
close OLDMD5;
if ($changes != "") {
open OLDMD5, ">md5" or die "I cant write the MD5's file.";
while (<RESULTS>) {
print OLDMD5 $_;
}
}


It fails to work with this error Undefined subroutine &main::diff called at dascript.pl line 27, <RESULTS> line 2.
What am I doing wrong to compare the two files?
russ

Replies are listed 'Best First'.
Re: Log's and MD5 Hashes: UPDATE
by blakem (Monsignor) on Jan 31, 2002 at 05:22 UTC
    Perhaps you need to import the diff function explicitly with:
    use Algorithm::Diff qw(diff);

    -Blake

Re: Log's and MD5 Hashes: UPDATE
by Ryszard (Priest) on Jan 31, 2002 at 06:56 UTC
    how about something like this?
    #!/usr/bin/perl -w use strict; use Digest::MD5; my @fileary; $fileary[0] = { name => '<file>', + oldhash => '', newhash => '' }; $fileary[1] = { name => '<file>', oldhash => '', newhash => '' }; while (1) { foreach (@fileary) { # move the newhash (from last iteration) to the oldhash of # this iteration %{$_}->{oldhash}=%{$_}->{newhash}; #check to see if there is a different has to #last iteration if (%{$_}->{newhash} ne %{$_}->{oldhash}) { print "File ".%{$_}->{name}." has changed\n"; } else { print "oldhash: ".%{$_}->{oldhash}.". newhash: ". %{$_}->{n +ewhash}."\n"; } #go get a filehandle open (FILE, %{$_}->{name}) or die "Can't open '$_': $!"; #go get the hash of the file %{$_}->{newhash} =Digest::MD5->new->addfile(*FILE)->hexdigest; close FILE; } }

    Its not very scalable, however you could dynamically create your hash of arrays from a config file.

    If youre going to run it as a daemon i would suggest a wait/sleep time, and sending an alert to something like NetX/pager/SMS/email etc....