免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1506 | 回复: 0
打印 上一主题 下一主题

create md5 v1.0 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-16 15:07 |只看该作者 |倒序浏览
作者: reistlin
来源: http://www.reistlin.com/blog/33
更新时间: 2010.08
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要
  1. #!/usr/bin/perl -w

  2. # Name: create md5 v1.0
  3. # Author: reistlin
  4. # Website: www.reistlin.com
  5. # Hotfix: bigyong
  6. # Website: www.bigyong.com
  7. # Date: 2010.08.02

  8. use strict;
  9. use Data::Dumper;
  10. use Digest::MD5 qw(md5 md5_hex md5_base64);


  11. # Debug Switch
  12. my $debug = 0;

  13. # Start Time
  14. my $time_1 = time();
  15. # Clear
  16. system "clear";


  17. # Defined Original Hash File
  18. my $original_file = "check_md5.log";
  19. my %original_hash;

  20. # Defined CheckList Directory
  21. my $checklist_dir = "/home/reistlin";

  22. # Defined Exclude Directory
  23. my $exclude_tag = 0;
  24. my @exclude_dir = ("/home/reistlin/exclude1", "/home/reistlin/exclude2");
  25. my $exclude_key = join("|", @exclude_dir);


  26. # Create MD5 Mode
  27. sub createmd5 {
  28.         my $path = shift;
  29.         # clean path end "/"
  30.         $path =~ s/\/+$//;

  31.         opendir(DIR, $path) or die "[Error] Can not check directory [$path] \n";
  32.         my @list = readdir(DIR);
  33.         closedir(DIR);

  34.         foreach my $tmp (@list) {
  35.                 # exclude "." or ".."
  36.                 next if ( $tmp =~ m/^\.+$/ );
  37.                 # full path
  38.                 my $path_sub = $path . "/" . $tmp;
  39.                 # exclude directory
  40.                 next if ( ( $path_sub =~ /$exclude_key/ ) && ( $exclude_tag == 1 ) );
  41.                 # subdirectory recursive
  42.                 if ( -d $path_sub ) {
  43.                         &createmd5($path_sub);
  44.                 } else {
  45.                         $original_hash{$path_sub} = &md5sum($path_sub);
  46.                 }
  47.         }
  48. }


  49. # MD5 Check Mode
  50. sub md5sum {
  51.         my $file = shift;
  52.         open(FILE, $file) or die "[Error] Can not check file [$file] \n";
  53.         binmode(FILE);

  54.         my $md5 = Digest::MD5->new();
  55.         $md5->addfile(*FILE);

  56.         close(FILE);

  57.         $md5->hexdigest;
  58. }


  59. # File Stat Mode
  60. sub filestat {
  61.         my $file = shift;
  62.         my @file_stat = stat($file);
  63.         my $mtime = localtime $file_stat[9];
  64.         my $ctime = localtime $file_stat[10];

  65.         print "\n";
  66.         print "[$file] Modify: $mtime \n";
  67.         print "[$file] Change: $ctime \n";
  68.         print "[$file] UID: $file_stat[4] \n";
  69.         print "[$file] GID: $file_stat[5] \n";
  70.         print "[$file] Size: $file_stat[7] bytes \n";
  71.         print "\n";
  72. }


  73. # Main Program
  74. &createmd5($checklist_dir);


  75. # Create MD5 logfile
  76. open(LINE, "+>" . $original_file) or die "[Error] Can not create MD5 logfile [$original_file] \n";

  77. while ( my ($file, $md5) = each %original_hash ) {
  78.         print LINE "$md5 $file\n";
  79. }

  80. close(LINE);


  81. # Print Result
  82. if ( -e $original_file ) {
  83.         print "Congratulations! MD5 logfile Create Succeed! \n";
  84.         &filestat($original_file);
  85. }


  86. # End Time
  87. my $time_2 = time();
  88. my $time = $time_2 - $time_1;


  89. # Script Runtime
  90. print "Runtime \[$time\] Seconds ... \n";
  91. print "End! Good Luck! :-) \n";
  92. print "\n";
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP