Tuesday, April 14, 2009

without lsof

I've encountered situations when I had to find which ports certain processes are using, it will be a breeze "IF" the system has lsof installed. Otherwise I've found this script to do the job.

You can also use the commands in here if you don't want to use it as a script (credits to sysunconfig.net).

#!/bin/ksh
#
#
# find from a port the pid that started the port
#
line='-------------------------------------------------------------------------'
pids=`/usr/bin/ps -ef | sed 1d | awk '{print $2}'`

# Prompt users or use 1st cmdline argument
if [ $# -eq 0 ]; then
read ans?"Enter port you like to know pid for: "
else
ans=$1
fi

# Check all pids for this port, then list that process
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\nPort: $ans is being used by PID: \c"
/usr/bin/ps -o pid -o args -p $f | sed 1d
fi
done

Readers who read this page, also read:




Bookmark and Share My Zimbio http://www.wikio.com

0 comments: