#! /usr/bin/perl # prints out the number and state of connections on port 389 by default # usage clients.pl [-v] [-p port] # port $port = 389; # process command line options $sn = 0; while ($sn < $#ARGV) { if ("$ARGV[$sn]" eq "-p") { $port = $ARGV[++$sn]; } elsif ("$ARGV[$sn]" eq "--port") { $port = $ARGV[++$sn]; } else { ++$sn; } } # temporary file $tmpFile = "/tmp/z"; $result = `netstat -an -f inet | fgrep $port > $tmpFile`; open(CONN,$tmpFile); $established = 0; $listen = 0; $timeWait = 0; $closeWait = 0; $synRcvd = 0; while() { ++$total; if(m/ESTABLISHED/) { ++$established; } if(m/LISTEN/) { ++$listen; } if(m/TIME_WAIT/) { ++$timeWait; } if(m/CLOSE_WAIT/) { ++$closeWait; } if(m/SYN_RCVD/) { ++$synRcvd; } ($dst,$src,$sw,$sq,$rw,$rq,$state) = split(" ",$_,7); ($q1,$q2,$q3,$q4,$remotePort) = split(/\./,$src,5); $srcIp = sprintf "%s.%s.%s.%s\n",$q1,$q2,$q3,$q4; $srcs[$srcIp] = $srcIp; print $srcIp; } close(CONN); print sprintf "total: %d listen: %d established: %d timewait: %d closewait: %d synrcvd: %d\n",$total,$listen,$established,$timeWait,$closeWait,$synRcvd;