Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot


by zejames (Hermit)
on Jun 18, 2002 at 06:18 UTC ( #175289=sourcecode: print w/replies, xml ) Need Help??
Category: Cryptography
Author/Contact Info zejames (Julien Bordet <>)
Description: Supervisor is a set of 2 script :
  • Supervisor, that compares file md5 checksums with the ones stores in a fingerprint file
  • update, that updates information about supervised file into the fingerprint file
# Author : Julien Bordet <>
# check md5 cheksums again those stored in a fingerprint file. When an
+ything has
# changed, send an alert email.
# fingerprint file should be passed with the -f switch

use strict;
use Getopt::Long;
use Digest::MD5;
use Mail::Send;
use MIME::Base64;

use vars qw (   $NO_ERROR $NO_SUCH_FILE $MD5_ERROR
                %errors  %result $adminmail $filelist $text

$adminmail = 'julien';

$NO_ERROR     = 0;
$MD5_ERROR    = 2;

%errors = ( $NO_ERROR           => '',
            $NO_SUCH_FILE       => 'No such file',
            $MD5_ERROR          => 'Fingerprints do not correspond');

GetOptions('-f=s'       =>      \$filelist);

$filelist ||= "";

open FILELIST, "< $filelist" or die "Unable to open $filelist : $!\n";
while (<FILELIST>)
        next if ( m/^$/ or m/^#/ );
        my ($filename, $md5) = m!^([^:]+):(.*)$!;
        $result{$filename}->{'expected'} = $md5;
        if (! open FILE, "< $filename") {
                warn "Unable to open $filename\n";
                $result{$filename}->{'error'} = $NO_SUCH_FILE;
                $result{$filename}->{'real'} = '';
        my $ctx = Digest::MD5->new;
        my $digest = encode_base64($ctx->digest, '');
        $result{$filename}->{'real'} = $digest;
        if ($digest eq $md5) {
                $result{$filename}->{'error'} = $NO_ERROR;
        } else {
                $result{$filename}->{'error'} = $MD5_ERROR;

foreach my $file (keys %result) {
        if ($result{$file}->{'error'}) {
                $text .= <<FIN;
Alert on file '$file' : $errors{$result{$file}->{'error'}}
        Last seen MD5 : $result{$file}->{'expected'}
        Current MD5   : $result{$file}->{'real'}


if ($text) {
        my $mailer = new Mail::Send Subject => "Supervisor alert",
                                    To => "$adminmail";

        my $fh = $mailer->open;
        print $fh $text;

        print "Mail send to $adminmail : \n$text";

#!/usr/bin/perl -w
# Author : Julien Bordet <>
# Update a md5 fingerprint file. Supervised file name should be passed
+ as
# parameters : if they changed, their md5 is updated. If they are new,
+ they are
# added. If they do not exist anylonger, their entry is deleted in the
+ fingerprint
# file
# fingerprint file should be passed with the -f switch

use strict;
use Getopt::Long;
use Digest::MD5;
use MIME::Base64;

use vars qw ( @filenames %md5 $filelist );

my $filename = "";

GetOptions('-f=s'       =>      \$filelist);

if (! -f $filelist) {
        open LIST, "> $filelist" or die "Unable to create $filelist : 
        close LIST;

@filenames = @ARGV;
foreach my $filename (@filenames) {
        $md5{$filename} = '';
        if (open FILE, "< $filename") {
                my $ctx = Digest::MD5->new;
                $md5{$filename} = encode_base64($ctx->digest, '');
                close FILE;

@ARGV = ($filelist);
undef $/;
$^I = ".bak";
while (<>)
        foreach my $filename (@filenames) {
                my $digest = $md5{$filename};
                if ( m!\Q$filename\E:! and $digest) {   # File have be
+en changed
                } elsif ($digest) {             # File is being added
                        $_ .= "$filename:$digest\n";
                } else {                        # File is being delete
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://175289]
Discipulus is this the IT?
[Corion]: Discipulus: Well, in many cases it doesn't make sense to build an interface and complicated program just to enter 20 rows into a database ;) But yes, automating data imports should pay off in the long run
[LanX]: Choroba: this happened before I joined, was still in uni, but my boss was summoned to the CEO of the second biggest German bank at that time and could only say " I told them its not ready" ;)
[LanX]: memories....I missed my connection while chatting
[Discipulus]: in this case Corion we are speaking about software licensing: evry year or two we must rescan the whole ced to produce an excel report, while at every activation / disactivation we update a black box DB: i said that i a week i can produce the perl to..
[Discipulus]: rend out the xls IF i have access to the DB
[choroba]: LanX I miss working in a bank sometimes...
[Corion]: Discipulus: Ooof. Especially yearly things are things I like to automate instead of trying to remember how I did things last year...
[Corion]: And the second rule that I've learned is, that there is no one-off job, so writing a program for it pays off almost immediately. The third rule is to give all my programs numbers and have them reproduce that number in the name of their output files. :)

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2017-03-29 12:04 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.