#!/usr/contrib/bin/perl -w
$portnum=$ARGV[0];
@netstat=`netstat -an`;
@foundlines = grep /^\d+.\d+.\d+.\d+.$portnum/, @netstat;
foreach $port(@foundlines){
if ( $port =~ /(\d+.\d+.\d+.\d+.$portnum) (\d+.\d+.\d+.\d+.\d+)\s+\d+\s+\d\s+\d+\s+\d\s+(\w+)$/){
if ($3 ne "LISTEN" or $port ne "ESTABLISHED" ){
print "$1 $3\n";
}else{
print "ok";
}
}
}
####
131.228.132.245.9494 131.228.132.159.41713 24820 0 8760 0 TIME_WAIT
131.228.132.245.94 131.228.132.159.41703 24820 0 8760 0 TIME_WAIT
####
root@toybox: perl portstat.pl 94
228.132.245.9494 TIME_WAIT
####
root@toybox: perl portstat.pl 9494
131.228.132.245.9494 TIME_WAIT
####
root@toybox: perl portstat.pl 9494
131.228.132.245.94 TIME_WAIT