Who is stealing my port ?

I often had to deal with problems about port already in use. The main question
was why my server cannot bind on port xxx ? On Linux the --program
option of netstat show you the id of the process currently owning a specific socket connection.

Solaris's netstat command does not have this option. Tired to scan the
/proc  with pfiles I've made a small script. I hope it can be useful to somebody else.




#!/bin/sh
if [ $# -ne 1 ]
then
echo "usage $0 <port number>"
exit 1
fi

PORT=$1
echo "looking for process consuming port $PORT"

old=`pwd`
cd /proc
for pid in \*
do
/usr/bin/pfiles $pid 2>/dev/null | /usr/bin/nawk -v port_num=$PORT '
$0 ~ /\^[0-9]\*:/ {
pid=$1
program=$2
}
$1 ~ /\^[\\t ]\*sockname/ && $5 == port_num {
printf ("program [%d:%s] is bounded on %s:%d\\n",pid,program,$3,port_num)
}'
done
cd $old


 


 

Comments:

lsof -i :8080
will list all processes busy with that port.

http://en.wikipedia.org/wiki/Lsof

Posted by Alain Honorez on août 09, 2007 at 01:21 AM CEST #

yep you are right. but as for 'netstat --program' you will
have to be on Linux (or a system on which lsof is installed) for that.
That was not my case :-(

Posted by guest on août 09, 2007 at 04:08 AM CEST #

Thank you very much for this useful script, works perfectly!

Posted by summerian on juin 01, 2009 at 05:28 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Emmanuel Jannetti blog

Search

Archives
« mai 2015
lun.mar.mer.jeu.ven.sam.dim.
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
       
Today