- 论坛徽章:
- 0
|
#!/usr/bin/perl
use strict;
use warnings;
die "Usage: $0 [ipaddr]\n"if(@ARGV < 1);
my $ipaddr = $ARGV[0];
print "$ipaddr\n";
if(!($ipaddr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ &&
(($1>=0)&&($1<255)&&($2>=0)&&($2<255)&&($3>=0)&&($3<255)&&($4>=0)&&($4<255))))
{
die "Invalid Argument \n";
}
print "$1-$2-$3-$4\n";
my $now = `date +%s`;
my $year = `date +%y`;
my $month = `date +%m`;
my $day = `date +%d`;
my $hour = `date +%H`;
my $min = `date +%M`;
my $sec = `date +%S`;
chomp($now);
chomp($year);
chomp($month);
chomp($day);
chomp($hour);
chomp($min);
chomp($sec);
print "$now-$year-$month-$day-$hour-$min-$sec\n";
my $dir_to_process = "/var/log/monitor_network";
my @f_pro_list;
opendir DH,$dir_to_process or die "Can't open $dir_to_process. $!\n";
while(my $file = readdir DH)
{
next if($file eq "." || $file eq "..");
print "$file\n";
$file=~ /^\w+_\w+_(\d+)_(\d+)_(\d+)_(\d+)_(\d+)_(\d+)$/;
print "<-$1,$2,$3,$4,$5,$6->\n";
if($year < $1){
next;
}
elsif($year == $1){
if($month < $2){next}
elsif($month == $2){
if($day < $3){next}
elsif($day == $3){
if($hour < $4){next}
elsif($hour == $4){
if($min < $5){next}
elsif($min == $5){
if($sec < $6){next}
elsif($sec){next} #exceptions
else{
push @f_pro_list,$file;
}
}
else{
push @f_pro_list,$file;
}
}
else{
push @f_pro_list,$file;
}
}
else{
push @f_pro_list,$file;
}
}
else{
push @f_pro_list,$file;
}
}
else{
push @f_pro_list,$file;
}
}
closedir DH;
my @f_final_list;
my $row = 0;
my $col;
my $hashref;
my $ip_matched = 0;
my @keys;
my @f_final_list;
chdir "/var/log/monitor_network/" or die "Can't chdir,error: $!\n";
foreach my $pro_file (@f_pro_list)
{
open FH,$pro_file or die "Can't open $pro_file: $!\n";
while(<FH>)
{
chomp;
my @temp = split;
if($row == 0){
@keys = @temp;
$col = scalar @keys;
#print "col = $col, $pro_file\n"
}
else{
my %temp_hash;
for(my $i=0;$i<$col;$i++)
{
$temp_hash{$keys[$i]} = $temp[$i];
if(($keys[$i] =~ /^src$/) &&($temp_hash{$keys[$i]} eq $ARGV[0])){
$ip_matched++;
print "$ip_matched---$_---\n";
}
}
push @$hashref,{%temp_hash};
}
$row++;
}
if($ip_matched > 0){
push @f_final_list,$pro_file;
}
$row = 0;
$ip_matched = 0;
close FH;
}
foreach(@f_final_list)
{
print "<>---$_---<>\n";
open FH,$_ or die "Can't Open $_: $!\n";
while(<FH>)
{
print;
}
close FH;
}
上面的是自己写的一些过滤出文件列表的代码部分,但是下面的比较部分没有思路了,如何两两比较文件里的一些字段,对于@f_final_list这个数组,还请高手指点下,谢谢。。。 |
|