#!/usr/bin/perl

# Define the format for output lines
format =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<
$firstpart                                                    $address
.

# Get system info about open ports
$response = `netstat -ln`;

# Divide by lines
@lines = split(/\n/,$response);

# The variable `flag' tracks if the header line
# has been printed out or not.
$flag = 1;

foreach (@lines) 
{
   # Look only at lines starting by tcp or udp
   next if ! /^((tcp)|(udp))/;

   # Parse the line into fields
   @fields = split();

   # Get relevant data: protocol, address and
   # port
   $protocol = $fields[0];
   ($address,$port) = split(/:/,$fields[3]);

   # Issue a `fuser' request about protocol
   # and port
   $response = `fuser -vn $protocol $port`;

   # Get relevant data from response
   @rows = split(/\n/,$response);
   # Header line will be printed once
   if ($flag)
   {
      $flag = 0;
      print("$rows[1]\n");
   }
   
   # Print data
   $firstpart = $rows[2];
   write();
}
