Fearthepenguin.net

Diary of a Lazy SysAdmin

Skip to: Content | Sidebar | Footer

Script to see what process is using a TCP port [Solaris]

This is a script for solaris. Give it the port number you want to check, and it will tell you what process is listening on that port. It’s not as good as lsof, but it’ll get you this info if you need it and lsof isn’t available.


#!/bin/ksh
#This will tell you what processes are listening
#to or using a particular port. For Solaris
#Must be run as root

line='---------------------------------------------'
pids=$(/usr/bin/ps -ef | sed 1d | awk '{print $2}')

if [ $# -eq 0 ]; then
 read ans?"Enter port you would like to know pid for: "
else
 ans=$1
fi

for f in $pids
do
 /usr/proc/bin/pfiles $f 2>/dev/null | /usr/xpg4/bin/grep -q "port: $ans"
 if [ $? -eq 0 ]; then
 echo $line
 echo "Port: $ans is being used by PID:\c"
 /usr/bin/ps -ef -o pid -o args | egrep -v "grep|pfiles" | grep $f
 fi
done
exit 0

Comments

Comment from Steph
Time January 17, 2014 at 9:36 am

Hi,

you can simply use LSOF for that :

lsof | grep LISTEN | grep

Regards,

Comment from ender
Time January 17, 2014 at 10:23 am

Except that Solaris 10 doesn’t come with lsof, and for various reasons we weren’t interested in building it or installing 3rd party packages. This is a workaround.

Write a comment