My first Perl script!!!
use strict;
use Fcntl qw(:flock :seek);
ProcessDirectory($ARGV[0]);
sub ProcessDirectory
{
my $directory = shift;
opendir(IMD, $directory) || die("Cannot open directory");
my @thecontents = readdir(IMD);
closedir(IMD);
foreach my $content (@thecontents)
{
my $fullpath = $directory . "\\" . $content;
if ((-f $fullpath) && (-r $fullpath) && (-w $fullpath) && (-T $fullpath))
{
ProcessFile($fullpath);
}
elsif(-d $fullpath && $content ne "." && $content ne "..")
{
ProcessDirectory($fullpath);
}
}
}
sub ProcessFile
{
my $filename = shift;
print "Processing $filename ...\n\n";
open(DAT, $filename) || die("Could not open $filename");
my @raw_data = <DAT>;
close(DAT);
my $threedigitcount = 0;
my $fourdigitcount = 0;
foreach my $linedata (@raw_data)
{
if($linedata =~ /<cvc>\d{3}<\/cvc>/i)
{
$linedata =~ s/<cvc>\d{3}<\/cvc>/<cvc>***<\/cvc>/i;
$threedigitcount++;
}
elsif($linedata =~ /<cvc>\d{4}<\/cvc>/i)
{
$linedata =~ s/<cvc>\d{4}<\/cvc>/<cvc>****<\/cvc>/i;
$fourdigitcount++;
}
elsif($linedata =~ /CVV2=\d{4}/i)
{
$linedata =~ s/CVV2=\d{4}/CVV2=****/i;
$fourdigitcount++;
}
elsif($linedata =~ /CVV2=\d{3}/i)
{
$linedata =~ s/CVV2=\d{3}/CVV2=***/i;
$threedigitcount++;
}
}
print "Number of occurences for 3 digits: $threedigitcount\n";
print "Number of occurences for 4 digits: $fourdigitcount\n\n";
open(DAT, ">$filename") || die("Cannot open $filename");
flock(DAT, LOCK_EX);
seek(DAT, 0, SEEK_SET);
print DAT @raw_data;
close(DAT);
}
Here's an update version.
use strict;
use Cwd;
use Fcntl qw(:flock :seek);
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
my $LOG_FILE_DIRECTORY = UnixPath2NTPath(getcwd()) . "\\Filter_Log";
my $LOG_FILE_PATH = "$LOG_FILE_DIRECTORY\\" . sprintf("%4d-%02d-%02d_log.txt", $year + 1900, $mon + 1, $mday);
my $EXCLUDE_LOG_DIRECTORY = sprintf("%4d-%02d-%02d", $year + 1900, $mon + 1, $mday);
# Check for log folder, if not there then create it
if (! -d "$LOG_FILE_DIRECTORY")
{
mkdir ("$LOG_FILE_DIRECTORY") || die("Cannot create folder $LOG_FILE_DIRECTORY!");
}
my $timeStamp = localtime time;
Log("\n----------------- Logging started $timeStamp -----------------\n\n");
ProcessDirectory($ARGV[0]);
$timeStamp = localtime time;
Log("\n---------------------- Ended $timeStamp ----------------------\n\n");
sub ProcessDirectory
{
my $directory = shift;
opendir(IMD, $directory) || Log("Can't open $directory directory\n");
my @thecontents = readdir(IMD);
closedir(IMD);
foreach my $content (@thecontents)
{
# if foldername has _ or equal to today's date, skip the process
if($content =~ /_\d{4}-\d{2}-\d{2}/ || $content eq $EXCLUDE_LOG_DIRECTORY)
{
Log("Skip $content\n");
next;
}
my $fullpath = "$directory\\$content";
if ((-f $fullpath) && (-r $fullpath) && (-w $fullpath) && (-T $fullpath))
{
ProcessFile($fullpath);
my @dirname = split(/\\/, $directory);
my $newdirectory = $directory;
$newdirectory =~ s/\\\d{4}-\d{2}-\d{2}/\\_@dirname[-1]/i;
rename($directory, $newdirectory);
}
elsif(-d $fullpath && $content ne "." && $content ne "..")
{
ProcessDirectory($fullpath);
}
}
}
sub ProcessFile
{
my $filename = shift;
Log("Processing $filename ...\n");
open(DAT, $filename) || Log("Can't open $filename\n");
my @raw_data = <DAT>;
close(DAT);
my $threedigitcount = 0;
my $fourdigitcount = 0;
foreach my $linedata (@raw_data)
{
if($linedata =~ /<cvc>\d{3}<\/cvc>/i)
{
$linedata =~ s/<cvc>\d{3}<\/cvc>/<cvc>***<\/cvc>/i;
$threedigitcount++;
}
elsif($linedata =~ /<cvc>\d{4}<\/cvc>/i)
{
$linedata =~ s/<cvc>\d{4}<\/cvc>/<cvc>****<\/cvc>/i;
$fourdigitcount++;
}
elsif($linedata =~ /CVV2=\d{4}/i)
{
$linedata =~ s/CVV2=\d{4}/CVV2=****/i;
$fourdigitcount++;
}
elsif($linedata =~ /CVV2=\d{3}/i)
{
$linedata =~ s/CVV2=\d{3}/CVV2=***/i;
$threedigitcount++;
}
}
Log("Number of occurences for 3 digits: $threedigitcount\n");
Log("Number of occurences for 4 digits: $fourdigitcount\n");
open(DAT, ">$filename") || Log("Can't open $filename\n");
flock(DAT, LOCK_EX);
seek(DAT, 0, SEEK_SET);
print DAT @raw_data;
close(DAT);
Log("$filename completed.\n\n");
}
sub Log($)
{
my $text = shift;
open(LOG_HANDLER, ">>$LOG_FILE_PATH") || die("Can't open $LOG_FILE_PATH");
print LOG_HANDLER "$text";
close(LOG_HANDLER);
print "$text";
}
sub UnixPath2NTPath($)
{
my $string = shift;
$string =~ s/\//\\/g;
return $string;
}
No comments:
Post a Comment