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

Write a comment