![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Enter the following code in your text editor, ensuring that the file and path names defined at the top match valid names on your system: #!perl/bin/perl # hitcnt.pl # # First version. Goes through a single IIS log file, tallying # the hits and IP addresses from which they came. # Command-line version output to screen. # Define path to a single log file. $LogDir = "c:/winnt/system32/logfiles/"; $LogFile = "in970502.log"; $LogPath = $LogDir.$LogFile; # Attempt to open the log file; die if it doesnt happen. open (LOG, $LogPath) || die "Cant open $LogPath: $!\n"; # Loop through the log file a line at a time and extract # the entry information. $n = 0; # Initialize a counter. while (<LOG>){ ($ClientIP, $Dummy, $Date, $Time, $SvcName, $SrvrName, $SrvrIP, $CPUTime, $BytesRecv, $BytesSent, $SvcStatus, $NTStatus, $Operation, $Target, $Dummy) = split (/,/); # Store the client IP address, increment counter. $IPArray[$n] = $ClientIP; $n++; } # end while (<LOG>) close (LOG); # Close the log file. # Store the total hits, then initialize two arrays for the IPs # and the number of hits for each. $TotalHits = $n; @IPHits = (); @NumHits = (); $HitCount = 0; # Loop through @IPArray and sort out the IPs that match, # incrementing that IPs hit count for each match. for ($n = 0, $i = 0; $n < $TotalHits; $n++) { for ($p = 0; $p < $HitCount; $p++) { if ($IPArray[$n] eq $IPHits[$p]) { $NumHits[$p]++; last; # Same as break in C } } # If $p == $HitCount, no matches were found. This is a new # IP address, so add it to the list. if ($p == $HitCount) { $IPHits[$HitCount] = $IPArray[$n]; $NumHits[$HitCount]++; $HitCount++; } } # end for ($n = 0...) # Print out the results. print "On 05/02/97:\n\n"; for ($n = 0; $n < $HitCount; $n++) { print "$IPHits[$n] registered $NumHits[$n] hits\n"; } # End hitcnt.pl Save this Perl script as hitcnt.pl and run it from the command line. Output similar to that illustrated in Figure 9.7 will be written to the screen.
Analyzing the First Hit CounterThe first version of hitcnt.pl is ugly in its outputas are most command-line applicationsbut you have built the foundation for something much bigger.
The only new Perl construct presented in hitcnt.pl is the last statement, which will be covered shortly. The script presents some slightly knotty logical concepts, though, and they might be easier to understand if we step through the logic in English:
The client IP addresses are picked up from the log file in the same way you did it in the previous section. Notice that we didnt change the call to split: while (<LOG>) { ($ClientIP, $Dummy, $Date, $Time, $SvcName, $SrvrName, $SrvrIP, $CPUTime, $BytesRecv, $BytesSent, $SvcStatus, $NTStatus, $Operation, $Target, $Dummy) = split (/,/); There is one exception, however. Rather than storing the input line in a local variable, we have allowed split to take its data directly from the standard input, defined in Perl, youll recall, as $_. This is why split has only its search pattern as a parameter.
The next code section stores the total number of entries in the log file and initializes a couple of empty arrays: # Store the total hits, then initialize two arrays for the IPs # and the number of hits for each. $TotalHits = $n; @IPHits = (); @NumHits = (); $HitCount = 0; The variable $HitCount will be used to store the number of individual IP addressesthat is, unique IPsthat will be picked out of the full array of entries. Now the two loops are set up to pick out the individual IPs and keep track of the number of times theyve hit. # Loop through @IPArray and sort out the IPs that match, # incrementing that IPs hit count for each match. for ($n = 0; $n < $TotalHits; $n++) { for ($p = 0; $p < $HitCount; $p++) { if ($IPArray[$n] eq $IPHits[$p]) { $NumHits[$p]++; last; # Same as break in C } } # If $p == $HitCount, no matches were found. This is a new # IP address, so add it to the list. if ($p == $HitCount) { $IPHits[$HitCount] = $IPArray[$n]; $NumHits[$HitCount]++; $HitCount++; } } # end for ($n = 0...)
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |