#!/usr/bin/perl 
#
# copyright (c) 2005 Ange Ferrari All rights reserved.
#
# Ange Ferrari (ange.ferrari@digital-network.net)
#
# http://www.digital-network.net
#
#


use strict; 

my $file = $ARGV[0];
my @data;
my $i = 0;
my %ip_liste;


if (!$ARGV[0])
{
	print "\nUsage : \n\n";
	print "\t ./$0 <netstat output file> [number]\n\n";
	exit;
}

open FILE, "< ".$file or die "I can't open $file : $!";


while(@data = split(/\s+/,<FILE>))
{
	if($data[5] =~ /(FIN_WAIT|ESTABLISHED|SYN_RECV|CLOSING|TIME_WAIT)/i)
	{
		$data[4] =~ s/:\d+//;
		$ip_liste{$data[4]} += 1;
	}
}

close FILE;


foreach my $key (keys %ip_liste)
{
	if ($ARGV[1]) 
	{
		if($ip_liste{$key} >= $ARGV[1])
		{
			printf("IP : %-15s -> %3d\n",$key,$ip_liste{$key});
		}
	}
	else
	{
		printf("IP : %-15s -> %3d\n",$key,$ip_liste{$key});
	}
}
