|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!剧本功效
剧本利用Perl编写,盘算文件的MD5值
剧本用处
办事器在安装完操纵体系后,盘算PATH变量内里一切的二进制文件的MD5、盘算单个文件MD5值、盘算某个目次(包含子目次)下一切的文件的MD5值纪录到文件,等往后再天生一份,然后2份文件举行对照,便可晓得这些文件是不是又被修改过.
剧本用法(6种用法)- 剧本名-p[>输入的文件名]←该功效是盘算PATH变量内里一切文件的MD5值剧本名-p-fMD5文件[>输入的文件名]←该功效是盘算PATH变量内里一切文件的MD5值并跟上一次天生的MD5文件绝对比剧本名文件1文件2文件3...[>输入的文件名]←该功效是盘算指定文件的MD5值剧本名目次1目次2目次3...[>输入的文件名]←该功效是盘算指定目次下一切文件的MD5值剧本名-cMD5文件1MD5文件2[>输入的文件名]←该功效是对照统一个目次下一切文件天生的2次MD5文件中有哪些MD5值纷歧致剧本名目次-fMD5文件[>输入的文件名]←该功效是跟下面的功效相似,只是这个不天生文件间接比对
复制代码 注重事项
不克不及对/proc目次举行盘算
不克不及对/(根)举行盘算,由于/内里包括/proc
不克不及对挂载在Linux上Windows的同享目次举行盘算
剧本内容
【第二版】
[code]<olclass="dp-clist-paddingleft-2"><liclass="alt">#!/usr/bin/perl
useDigest::MD5;
<liclass="alt">useFile::Find;
<liclass="alt">#2012-12-1615:24Leochanyipiaomiao@163.com
#Blog:http://linux5588.blog.51cto.com
<liclass="alt">
#用法提醒
<liclass="alt">$usage="Usage:scriptname-p|Directory1...|File1...|-cMD5File1MD5File2|Directory-fMD5File[>OUTFILE]";
$usagecompare="Usage:scriptname-cMD5File1MD5File2[>OUTFILE]";
<liclass="alt">$usagepath="Usage:scriptname-p|-p-fMD5File[>OUTFILE]";
<liclass="alt">#判别下令行参数是不是为空,为空则间接盘算PATH路径内里一切的二进制文件的MD5值,不为空
#假如第一个参数是目次,那末挪用getDirectoryAllFileMD5盘算目次内里一切文件的MD5值
<liclass="alt">#假如第一个参数是文件,那末挪用getSingleFileMD5盘算下令行参数内里一切文件的MD5值
if(@ARGV){
<liclass="alt">my$arg=$ARGV[0];
if(-d$arg){
<liclass="alt">if($ARGV[1]eq-f&&-T$ARGV[2]){
&compareWithLastMD5File($arg);
<liclass="alt">}else{
&getDirectoryAllFileMD5(@ARGV);
<liclass="alt">}
}elsif(-f$arg){
<liclass="alt">&getSingleFileMD5(@ARGV);
}elsif($argeq-c&&@ARGV==3){
<liclass="alt">die"$usagecompare
"unless(-T$ARGV[1]&&-T$ARGV[2]);
&compareWithTwoMD5Files;
<liclass="alt">}elsif($argeq-p){
if(1==@ARGV){
<liclass="alt">&getPathBinFileMD5;
}elsif($ARGV[1]eq-f&&-T$ARGV[2]){
<liclass="alt">&compareWithLastPathMD5File;
}else{
<liclass="alt">die"$usagepath
";
}
<liclass="alt">}else{
die"$usage
";
<liclass="alt">}
}else{
<liclass="alt">die"$usage
";
}
<liclass="alt">
#失掉目次下一切文件(包含子目次)的MD5值
<liclass="alt">subgetDirectoryAllFileMD5{
find(&wantedPrint,@_);
<liclass="alt">}
<liclass="alt">#失掉PATH变量内里一切的二进制文件的MD5值
subgetPathBinFileMD5{
<liclass="alt">my@path=split/:/,$ENV{PATH};
find(&wantedPrint,@path);
<liclass="alt">}
<liclass="alt">#先失掉本次PATH变量一切的目次下的文件的MD5值,然后对照之前是天生的MD5文件
subcompareWithLastPathMD5File{
<liclass="alt">my@path=split/:/,$ENV{PATH};
&compareWithLastMD5File(@path);
<liclass="alt">}
<liclass="alt">#失掉单个文件的MD5值
subgetSingleFileMD5{
<liclass="alt">foreach(@_){
if(-R$_){
<liclass="alt">print"$_",&getMD5($_),"
";
}else{
<liclass="alt">print"Cantread$_
";
next;
<liclass="alt">}
}
<liclass="alt">}
<liclass="alt">#师长教师成该目次下一切文件的MD5值,然后跟上一次该目次的天生的MD5文件对照
#本次天生的MD5跟上一次天生的MD5文件比对,纷歧致的输入出来,同时将3个工夫输入出来
<liclass="alt">#假如是新增加的文件则输入出来其3个工夫值,atimemtimectime
subcompareWithLastMD5File{
<liclass="alt">find(&wantedHash,@_);#这里挪用谁人回调函数后就会天生一个%thisMD5Hash的哈希
my$md5file=$ARGV[2];
<liclass="alt">
openLASTMD5FILE,"<","$md5file"ordie"Cantread$md5file:$!
";
<liclass="alt">my$lastMD5Filerecords=(@lastMD5FilerecordsArray=<LASTMD5FILE>);
%lastMD5Hash=map{split}@lastMD5FilerecordsArray;
<liclass="alt">closeLASTMD5FILE;
<liclass="alt">foreach(keys%thisMD5Hash){
$thisMD5Filerecords++;
<liclass="alt">}
&compare($thisMD5Filerecords,$lastMD5Filerecords);
<liclass="alt">}
<liclass="alt">#对照2个天生的MD5文件(对统一个目次天生的),找出分歧的大概不存在的
subcompareWithTwoMD5Files{
<liclass="alt">my($md5file1,$md5file2)=($ARGV[1],$ARGV[2]);
openMD5FILE1,"<","$md5file1"ordie"Cantread$md5file1:$!
";
<liclass="alt">openMD5FILE2,"<","$md5file2"ordie"Cantread$md5file2:$!
";
my$file1record=(@file1record=<MD5FILE1>);
<liclass="alt">my$file2record=(@file2record=<MD5FILE2>);
closeMD5FILE1;
<liclass="alt">closeMD5FILE2;
<liclass="alt">%thisMD5Hash=map{split}@file1record;
%lastMD5Hash=map{split}@file2record;
<liclass="alt">&compare($file1record,$file2record);
}
<liclass="alt">
#对2个MD5文件举行对照大概是边天生边比对
<liclass="alt">subcompare{
my($file1record,$file2record)=($_[0],$_[1]);
<liclass="alt">
if($file1record>=$file2record){
<liclass="alt">%hash1=%thisMD5Hash;
%hash2=%lastMD5Hash;
<liclass="alt">}else{
%hash1=%lastMD5Hash;
<liclass="alt">%hash2=%thisMD5Hash;
}
<liclass="alt">
my$count=0;
<liclass="alt">foreach(keys%hash1){
if(exists$hash2{$_}){
<liclass="alt">if($hash1{$_}ne$hash2{$_}){
$count++;
<liclass="alt">($atime,$mtime,$ctime)=&getFileAMCTime($_);
print"Different-->$_
";
<liclass="alt">print"$hash2{$_}
";
print"$hash1{$_}Atime:$atimeMtime:$mtimeCtime:$ctime
";
<liclass="alt">}
}else{
<liclass="alt">$count++;
if(-e$_){
<liclass="alt">($atime,$mtime,$ctime)=&getFileAMCTime($_);
print"Added-->$_
$hash1{$_}Atime:$atimeMtime:$mtimeCtime:$ctime
";
<liclass="alt">}else{
print"Deleted-->$_$hash1{$_}
";
<liclass="alt">}
}
<liclass="alt">}
if($count==0){
<liclass="alt">print"NotFoundDifferent!!
";
}
<liclass="alt">}
<liclass="alt">#遍历前提,找到以后输入
subwantedPrint{
<liclass="alt">if(-f$_&&-R$_){
print"$File::Find::name",&getMD5($_),"
";
<liclass="alt">}
}
<liclass="alt">
#遍历前提,找到以后构成一个HASH
<liclass="alt">subwantedHash{
if(-f$_&&-r$_){
<liclass="alt">$thisMD5Hash{$File::Find::name}=&getMD5($_);
}
<liclass="alt">}
<liclass="alt">#盘算MD5值
subgetMD5{
<liclass="alt">my$file=shift@_;
open(FH,$file)ordie"Cantopen$file:$!
";
<liclass="alt">binmode(FH);
my$filemd5=Digest::MD5->new->addfile(FH)->hexdigest;
<liclass="alt">closeFH;
return$filemd5;
<liclass="alt">}
<liclass="alt">#猎取文件的atime,mtime,ctime
subgetFileAMCTime{
<liclass="alt">$filename=shift@_;
my($atime,$mtime,$ctime)=(stat($filename))[8,9,10];
<liclass="alt">$atime=&getTime($atime);
$mtime=&getTime($mtime);
<liclass="alt">$ctime=&getTime($ctime);
<liclass="alt">#将日期工夫格局转换为对照友爱的格局
subgetTime{
<liclass="alt">my$time=shift@_;
<p><spanstyle="padding:0px;margin:0px;border:none;color:#505050;background-color:inherit;">&n如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们! |
|