[Perl] check md5 v1.0
作者:reistlin 发布时间:August 2, 2010 分类:原创文章
作者: reistlin
来源: http://www.reistlin.com/blog/32
更新时间: 2010.08
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要



[https://reistlin.googlecode.com/svn/trunk/perl/check_md5.pl]
#!/usr/bin/perl -w # Name: check md5 v1.0 # Author: reistlin # Website: www.reistlin.com # Hotfix: bigyong # Website: www.bigyong.com # Date: 2010.08.02 use strict; use Data::Dumper; use Digest::MD5 qw(md5 md5_hex md5_base64); # Debug Switch my $debug = 0; # Start Time my $time_1 = time(); # Clear system "clear"; # Defined Original Hash File my $original_file = "./check_md5.log"; my %original_hash; # Defined CheckList Directory my $checklist_dir = "/home/reistlin"; # Defined Exclude Directory my $exclude_tag = 0; my @exclude_dir = ("/home/reistlin/exclude1", "/home/reistlin/exclude2"); my $exclude_key = join("|", @exclude_dir); # Defined Result my @md5_ok; my @md5_no; my @md5_bad; # Check Hash File if ( ! -e $original_file ) { print "[Error] Can not open MD5 logfile [$original_file] \n"; print "[Error] Please run the \[create_md5.pl\] to create original MD5 logfile \n"; exit; } else { # check file stat &filestat($original_file); # load hash file open(FILE, $original_file); while (<FILE>) { my $line = $_; if ( $line =~ m/(.+)\s+(.+?)\s*$/ ) { $original_hash{$2} = $1; } } close(FILE); } # List file Check MD5 sub checkmd5 { my $path = shift; # clean path end "/" $path =~ s/\/+$//; opendir(DIR, $path) or die "[Error] Can not check directory [$path] \n"; my @list = readdir(DIR); closedir(DIR); foreach my $tmp (@list) { # exclude "." or ".." next if ( $tmp =~ m/^\.+$/ ); # full path my $path_sub = $path . "/" . $tmp; # exclude directory next if ( ( $path_sub =~ /$exclude_key/ ) && ( $exclude_tag == 1 ) ); # subdirectory recursive if ( -d $path_sub ) { &checkmd5($path_sub); } else { # check key if ( exists $original_hash{$path_sub} ) { # check md5 if ( $original_hash{$path_sub} eq &md5sum($path_sub) ) { push @md5_ok, $path_sub; } else { push @md5_bad, $path_sub; } } else { push @md5_no, $path_sub; } } } } # MD5 Check Mode sub md5sum { my $file = shift; open(FILE, $file) or die "[Error] Can not check file [$file] \n"; binmode(FILE); my $md5 = Digest::MD5->new(); $md5->addfile(*FILE); close(FILE); $md5->hexdigest; } # File Stat Mode sub filestat { my $file = shift; my @file_stat = stat($file); my $mtime = localtime $file_stat[9]; my $ctime = localtime $file_stat[10]; print "\n"; print "[$file] Modify: $mtime \n"; print "[$file] Change: $ctime \n"; print "[$file] UID: $file_stat[4] \n"; print "[$file] GID: $file_stat[5] \n"; print "[$file] Size: $file_stat[7] bytes \n"; print "\n"; } # Print Array Mode sub print_array { my @array = @_; foreach my $tmp (@array) { print "$tmp \n"; } } # Main Program &checkmd5($checklist_dir); # Print Result Mode my $scalar_ok = scalar(@md5_ok); my $scalar_no = scalar(@md5_no); my $scalar_bad = scalar(@md5_bad); print "[OK MD5] $scalar_ok Files:\n"; &print_array(@md5_ok); print "\n"; print "[NO MD5] $scalar_no Files:\n"; &print_array(@md5_no); print "\n"; print "[BAD MD5] $scalar_bad Files:\n"; &print_array(@md5_bad); print "\n"; # End Time my $time_2 = time(); my $time = $time_2 - $time_1; # Script Runtime print "Runtime \[$time\] Seconds ... \n"; print "End! Good Luck! :-) \n"; print "\n";








飘过