############################### Initilize User Variables #############################################################

$topcount = 20 ;	# How many of the top IP's do you want reported
$myserver = "192.168.254.1" ; # I don't like to count my own server for stats..
#$filename = "blklst.txt" ; # The file name to open...
$filename = "//ns1/d\$/Progra\~1/SmarterTools/SmarterMail/Spool/blklst.txt" ; # or, if it's elsewhere


######################################################################################################################
# UNLESS YOU ARE A PERL PROGRAMMER, MOLEST BELOW THIS LINE AT YOUR OWN PERIL
#####################################################################################################################
system "cls" ; 

sub CLEANUP ;
sub PRINTIT ;


open ( INFILE, $filename )   or die "Can't open BLKLST.txt";
print "\n Opening File  \n\n\n" ;
	while (<INFILE>) {     				# reads in file line by line
		#print " $_ " ;				
		unshift (@tempfilenames, $_ ) ;		# Pop the answer into the array
	}
close(INFILE) ; # Close the file, and open the next, reset counters

CLEANUP ;
PRINTIT ;



# ********************  SUBROUTINES BELOW THIS LINE  ************************************
############################
sub CLEANUP {
	@sorted_list = sort(@tempfilenames) ;
	#print " @sorted_list \n " ;
	$testarraycounter = @sorted_list ; # First sort.... by IP address
	$placecounter = 0 ;
	$placecounter2 = 1 ;
	$DupeHolder = abc123 ;
	# Take the sorted Test array, chop it up, count the individual IP's, chuck them into an array
	while ( $placecounter < $testarraycounter ) {
		@testsplit = split /\s+/, $sorted_list[$placecounter] ;	
		#print " $testsplit[0] \n " ;		
		if ( $DupeHolder eq $testsplit[0] ) {
			#print " :$DupeHolder: :$testsplit[0]: \n" ; 
			$placecounter2 = $placecounter2 + 1 ;
			}		
		else {
			$counted_data = "$placecounter2" . "   " . "$DupeHolder" ;
			# print " $counted_data \n" ;
			if ( $DupeHolder ne $myserver ) {
				unshift ( @cleanedlist, $counted_data );
			}
			$placecounter2 = 1 ;
		}
		$DupeHolder = $testsplit[0] ;
		$placecounter = $placecounter + 1 ;
	}

	@sortedcleaned_list = sort {$a <=> $b}( @cleanedlist ) ; # Sort descending, by number of hits

}


#############################
sub PRINTIT {
	$testarraycounter = @sortedcleaned_list - 1 ;
	print " Total IP's : $testarraycounter \n" ;	
	print "Hits    IP Address       HostName\n";
	print "________________________________________________________\n" ;
	$placecounter = 0 ;
	while ( $placecounter <= $topcount ) {
		print " @sortedcleaned_list[$testarraycounter] " ;
		@testsplit = split /\s+/, @sortedcleaned_list[$testarraycounter] ;	
		$Host = $testsplit[1] ;
		$pinger = `ping -a $Host -n 1 2>&1`;
		@pingsplit = split /\s+/, $pinger ;
		print " \t $pingsplit[2] \n " ;
		print " --------------------------- \n" ;
		$testarraycounter = $testarraycounter - 1 ;
		$placecounter = $placecounter + 1 ;
	}
	#print " @sortedcleaned_list \n " ;

}