#!/usr/bin/perl

#Take from filter table. Take from nat table is incorrect, incoming traffic is zerro.         
my $command = "iptables -n -v -x -L FORWARD";
my $i = 0;

open(PASS, $command."|") || die "Файл не найден!";

 %input = ();
 %output = ();  
 %all = ();

while(<PASS>)
{
	if ( $i < 2 )
	{
	  $i++;
	  next;
	}  

	if ($_ =~ /\S+\s+(\S+)\s+(?:\S+\s+){5}(\S+?)(?:\/\d+)*\s+(\S+?)(?:\/\d+)*\s+/)
	{
	  $input{$3} = $1;
	  $output{$2} = $1;
	  $all{$2} = $1;
	  $all{$3} = $1;
	                                                                             
	  #print "\"".$1."\"\t\"".$2."\"\t\"".$3."\"\n"

    }
}

for my $k ( keys %all )
{

  my $inputBytes = $input{$k}?  $input{$k}:0;
  my $outputBytes = $output{$k}? $output{$k}:0;

  if ( $k != "0.0.0.0" )
  {
  	print "$k\t".$inputBytes."\t".$outputBytes."\n";
  }
}

close(PASS);
exit 0;		# Завершение программы
