How to find out which process listens on certain port on Solaris?

This week i (again after a while) needed to know which (damn) process held
certain port and so was quite "successfully" blocking my code.
In Tux world i believe problem like this could be solved simply by using a stupid
parameter of netstat command. It is unfortunately not that easy on Solaris :-(

In an excited mood i totally forgot i had written a handy shell script for this
purpose some time ago. So i killed all suspicious processes to solve the problem.
Just to help myself keep in mind there is a better
way i published the script at http://blogs.sun.com/japod/resource/portHolder.sh

The script exploits pfile command, which provides information
on files being used by certain process. And since everything is a file in the unix world,
pfiles provides information on port numbers as well.
The bad news is that you need to invoke pfile command
for every single running process in order to get complete information.

Core of the script looks like the following:


PIDS=`ps -ef $LIMIT_UID| awk '{ print $2 }' | sed 1d`
for p in $PIDS ;do
pfiles $p 2> /dev/null | grep "port: $PORTNUM" > /dev/null 2>&1
if test 0 -eq $? ;then
if test -z "$PRINT_ARGS" ;then
ps -p $p | sed 1d
else
pargs -l $p
fi
fi
done

Lets say, you want to find out who holds port number 1175. Then you run:


%portHolder.sh -p 11750
3673 pts/4 0:34 java

Well, you might have launched many java processes, so in order to find out which one is
"yours" you add -a parameter:


%portHolder.sh -p 11750 -a /usr/jdk/jdk1.6.0_01/bin/java '-Dcom.sun.aas.instanceRoot=/export/home/japod/.personalDomain9/personalDomain'
'-Dcom.sun.aas.ClassPathPrefix='
'-Dcom.sun.aas.ClassPathSuffix='
'-Dcom.sun.aas.ServerClassPath='
'-Dcom.sun.aas.classloader.appserverChainJars.ee='
... lines removed for clarity...

Comments:

Or you could just use lsof.

Posted by Mike on November 30, 2007 at 10:00 AM CET #

Which I can not find on my box (Nevada, x86 -- build 61: %which lsof gives me: lsof not found :-(

Posted by Jakub on November 30, 2007 at 10:21 AM CET #

On windows, I used couple GUIs, like activePorts, procexp, and the netstat -pant for linux, not sure, if sure if there's a gui on \*inx platform, that will quite useful for monitoring the port usage on the fly.

Posted by mmao on December 03, 2007 at 12:35 AM CET #

I just got it from Blastwave ... then 'lsof -i' and you get the ports held by processes. Works great, thanks Mike & Jakub! ;O)

Posted by MartinG on December 03, 2007 at 01:43 AM CET #

Hmm, some more observation. I tried to find who's holding ports 5800 and 5900 (it's the default vncserver + http vnc access to desktop in Solaris 11 builds). Jakub's portholder prints this easily:
bash-3.2$ ./portHolder.sh -p 5800
910 ? 1:13 vino-ser

Then I tried
lsof -i :5800
No result. Hmm, I tried the same with 5900 and got no result as well. Then, knowing the pid from Jakub's script, I tried
lsof -i | grep 910. Output is below:
vino-serv 910 snajper 12u IPv4 0xd67a4300 0t0 TCP \*:\*->0.0.166.192:\* (ESTABLISHED)
vino-serv 910 snajper 19u IPv6 0xd67a3000 0t0 TCP \*:\*->[::170c]:\* (ESTABLISHED)
vino-serv 910 snajper 20u IPv6 0xd678db00 0t0 TCP \*:\*->[::16a8]:\* (ESTABLISHED)
So, lsof is able to find the port holder, but only this command leads me to some results:
lsof -i | grep 170c
where first I need to convert the port numbers to hexa. Is there a way to tell lsof to work in human readable format?

Posted by MartinG on December 03, 2007 at 02:42 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Jakub Podlesak

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
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
   
       
Today