use CLRunner; my $netstatcmd=CLRunner->new("netstat","-n"); # Assumes NETSTAT is in the path my $fportcmd=CLRunner->new("fport",""); # Assumes FPORT is in the path my $results; my $rest; my @lines; my @record; my %fport; my $i,$j,$k; my $ip,$port; print "Finding port connections and responsible apps .... \n"; # Run FPORT and build a hash of its output - the running applications and their associated ports $fportcmd->runcommand(); $results=$fportcmd->getoutput(); @lines=split("\n",$results); foreach $i (@lines) { if ($i =~ /->/) { @record = split(" ",$i); $rest=""; for ($j=5; $j<=@record; $j++) { $rest.=$record[$j]." "; } $fport->{$record[4]}->{$record[3]} = $record[1]."(".$rest.")"; } } # Run NETSTAT to find ESTABLISHED connections, and relate to FPORT hash to see what application is responsible $netstatcmd->runcommand(); $results=$netstatcmd->getoutput(); @lines=split("\n",$results); foreach $i (@lines) { if ($i =~ /TCP/) { @record = split(" ",$i); ($ip,$port)=split(":",$record[1]); print "Source $ip at port $port as $fport->{$record[0]}->{$port}\n\t is connected to "; ($ip,$port)=split(":",$record[2]); print "Remote $ip at port $port\n\n"; } }