Friday, July 21, 2006

Finding What Process is Binding Network Socket / Port

I found a very usefull site for this purpose. It guide you step by step on how to find the process.

http://linux-sxs.org/networking/portnumid.html

From the site, I wrote a script to ease the process.

#!/bin/bash
#
# Script to find what process is binding to which network socket
# Usage
# portowner <port>
# portowner all
#
# Author
# Mohd Izhar Firdaus Ismail <kagesenshi.87@gmail.com>
#

findproc(){
PIDS=`fuser -n tcp $@ 2>/dev/null`
echo "--------------"
echo "Port $@"
ps $PIDS
echo "-------------"
}

if [ "$1" == "" ];then
echo "Usage:"
echo " $( basename $0 <port> ) "
echo " $( basename $0 ) all"
elif [ "$1" == "all" ];then
PORTS=`netstat -na|grep tcp\
|grep LISTEN|sed s/"\:\:\:"/"0.0.0.0\:"/g \
|sed s/"\:"/" "/g|awk '{print $5}'|sort|uniq`
for PORT in $PORTS;do
findproc $PORT ;
done
else
for PORT in $@;do
findproc $PORT ;
done
fi
Post a Comment
Locations of visitors to this page