#! /usr/bin/perl # CSC 310 Project # 4 use 5.010; use Time::Local; #opening log and html file open (LOG, '<', 'IN-access.log'); open (HTML, '>', 'OUT-access.html'); #printing header/title/format of html print HTML "Visitors Log\n"; print HTML "The log file start date is: $startDate
\n"; print HTML "There were $IPcount unique visitors in the logfile.
\n"; print HTML "There were visits yesterday
\n"; print HTML "\n"; ############## #replaces the month with its corresponding number sub convert{ ($day,$month,$year) = split '/', $formattedDate; #print "$day $month $year\n"; %dates = ( 'Jan' => '00','Feb' => '01','Mar' => '02', 'Apr' => '03','May' => '04','Jun'=> '05', 'Jul' => '06','Aug' => '07','Sep' => '08', 'Oct' => '09','Nov' => '10','Dec' => '11', ); foreach $char ($month){ $char =~ s/.../$dates{$month}/; } $modified = "$day/$month/$year"; $modified; } ############### #reading log file while ($lines = ){ #assigning values ($remoteIP,$rfc,$userID,$dateTime,$timeZone,$requestType,$fileRequested,$requestProtocol,$statusCode,$sizeOfFile) = split ' ', $lines; ####### converting month to a number, then the date to epoch time $formattedDate = substr($dateTime, 1, 11); #gets hrs,mins,secs from $dateTime ($hr,$min,$sec) = split ':', substr($dateTime, 13,8); #converts month to corresponding number $modifiedDate = &convert($formattedDate); #open (HEAD, '>>', 'ipDate.txt'); #print HEAD "$remoteIP,$modifiedDate\n"; ($DAY,$MONTH,$YEAR) = split '/', $modifiedDate; $logDate = timelocal($sec,$min,$hr,$DAY,$MONTH,$YEAR); push(@listOfDates, $logDate); @sortedDates = sort {$a <=> $b} @listOfDates; #start date of log file my $startDate = $sortedDates[0]; print scalar(localtime($startDate))."\n"; #keeps track of unique visitors #checks if remoteIP is already in the array. If not, counter increases #and remoteIP gets added to array if ($remoteIP ~~ @listOfIPs){ $IPcount = $IPcount; }else{ $IPcount += 1; push(@listOfIPs, $remoteIP); } #verifies values were assigned properly print "\nUnique Visitors: $IPcount\n"; print "$remoteIP\n"."$rfc\n"."$userID\n"."$dateTime "."$timeZone\n"."$requestType $fileRequested $requestProtocol\n"."$statusCode\n"."$sizeOfFile\n\n"; #printing data to HTML file print HTML "\n"; print "\n\nNEXT\n\n"; }
IPLOGFILE
$remoteIP$remoteIP $rfc $userID $dateTime $timeZone $requestType $fileRequested $requestProtocol $statusCode $sizeOfFile