程式需求說明,對某一個目錄做 MD5 檢核!並將檢核結果以 E-Mail 方式自動通知!
md5deep
http://md5deep.sourceforge.net/
md5deep.exe -h
md5deep.exe version 4.3 by Jesse Kornblum and Simson Garfinkel.
C:\> md5deep.exe [OPTION]... [FILES]...
See the man page or README.txt file or use -hh for the full list of options
-p <size> - piecewise mode. Files are broken into blocks for hashing
-r - recursive mode. All subdirectories are traversed
-e - show estimated time remaining for each file
-s - silent mode. Suppress all error messages
-z - display file size before hash
-m <file> - enables matching mode. See README/man page
-x <file> - enables negative matching mode. See README/man page
-M and -X are the same as -m and -x but also print hashes of each file
-w - displays which known file generated a match
-n - displays known hashes that did not match any input files
-a and -A add a single hash to the positive or negative matching set
-b - prints only the bare name of files; all path information is omitted
-l - print relative paths for filenames
-t - print GMT timestamp (ctime)
-i/I <size> - only process files smaller/larger than SIZE
-v - display version number and exit
-d - output in DFXML; -u - Escape Unicode; -W FILE - write to FILE.
-j <num> - use num threads (default 2)
-Z - triage mode; -h - help; -hh - full help
#!/usr/bin/perl
#md5filenameok 這個陣列是用來是用來檢核並列出MD5正常的檔案
@md5filenameok=`c:\\perl-shell\\md5deep.exe -r -m c:\\perl-shell\\md5\\syslog.md5 d:\\backup\\*.*`;
#md5filenameoknumber 這個陣列是用來是用來檢核並列出MD5正常的檔案的檔案數量
$md5filenameoknumber=`c:\\perl-shell\\md5deep.exe -r -m c:\\perl-shell\\md5\\syslog.md5 d:\\backup\\*.* | find /c "d:\"`;
#下面這個方式是在 windows command mode 對目錄做出 MD5的檢核檔
#system(`c:\perl-shell\md5deep -l -r d:\backup > c:\perl-shell\md5\syslog.md5`);
#這個陣列是用來是用來檢核並列出MD5異常的檔案
@md5filename=`c:\\perl-shell\\md5deep.exe -r -x c:\\perl-shell\\md5\\syslog.md5 d:\\backup\\*.*`;
#這個陣列是用來是用來檢核並列出MD5異常的檔案的檔案數量
$md5number=`c:\\perl-shell\\md5deep.exe -r -x c:\\perl-shell\\md5\\syslog.md5 d:\\backup\\*.* | find /c "d:\" `;
chomp($md5filenameoknumber);
chomp($md5number);
print '=============== 正常 ===============';
print "\n\n";
print "\n MD5比對正確的檔案清單如下: \n\n @md5filenameok \n";
print "比對正確的檔案數量合計共有 $md5filenameoknumber 個 \n\n";
print '=============== 異常 ===============';
print "\n\n";
print "MD5比對異常的檔案清單如下: \n\n @md5filename \n";
print "比對異常的檔案數量合計共有 $md5number 個 \n ";
@message1="\n MD5比對正確的檔案清單如下: \n\n @md5filenameok \n 比對正確的檔案數量合計共有: $md5filenameoknumber \n\n";
@message2="\n MD5比對異常的檔案清單如下: \n\n @md5filename \n 比對異常的檔案數量合計共有: $md5number \n\n";
#可自行選擇在何種條件才寄出郵件通知!
if ($md5number==0){
print "\n ----> MD5 比對異常的檔案數量為 0 不寄出郵件通知! \n";
$status='[ Normal ]';
get_mail();
}
else {
print " \n ----> MD5 比對異常出郵件通知 \n ";
$status='[ --->Error ]';
get_mail();
}
sub get_mail{
use Encode qw(decode encode_utf8);
use Encode qw(encode);
use MIME::Base64;
use MIME::Lite;
($sec,$min,$hour,$day,$mon,$year,$mday)=localtime(time);
$mon++;
if (length ($mon) == 1) {$mon = '0'.$mon;}
if (length ($day) == 1) {$day = '0'.$day;}
if (length ($hour) == 1) {$hour = '0'.$hour;}
if (length ($min) == 1) {$min = '0'.$min;}
if (length ($sec) == 1) {$sec = '0'.$sec;}
$year+=1900;
$clock=" $status Syslog-Server md5 CheckMail $year/$mon/$day/$hour:$min/";
@foot0=' Syslog-Server D:\BACKUP MD5自動檢查通知程式!';
use Encode qw/from_to/;
from_to(@foot0, "utf8", "big5");
from_to(@message1, "utf8", "big5");
from_to(@message2, "utf8", "big5");
from_to($clock, "utf8", "big5");
$line='=====================================================';
$msg = MIME::Lite->new(
From =>'Syslog-MD5@xrcd2.com.tw',
To =>'xrcd2@xrcd2.com.tw',
Subject => "$clock",
Encoding => 'base64',
Data =>"@foot0 \n\n $line \n\n @message1 \n\n $line\n\n @message2 \n\n $line \n\n"
);
$msg->get_length;
$msg->attr("content-type.charset" => "UTF8");
$msg->send('smtp','smpt.server.ip.address');
print $Mail::SendMail::Error eq "" ? "\n send ok!\n" : $Mail::SendMail::Error;
}
沒有留言:
張貼留言