Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

process to port binding

Status
Not open for further replies.

nitinkgoud

IS-IT--Management
Jun 28, 2006
87
0
0
US
Hey Guys,
how can you find which process is bound to perticular port.
eg: which process is bound to port 80.
 
One option is to download lsof from SunFreeware.com and type lsof -i :80.

Annihilannic.
 
I could not find sockstat!
Is it avilable in 5.8?
 
U can use the following script mentioned below. This script I got from unknown source and it did worked for me.
#!/bin/ksh

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

Thanks,
smishra
 
This "unknown resource" is sunmanagers.org.

Here is the complete summary of this discussion:

Listing all the pids:
---------------------
/usr/bin/ps -ef | sed 1d | awk '{print $2}'


Mapping the files to ports using the PID:
-----------------------------------------
/usr/proc/bin/pfiles <PID> 2>/dev/null | /usr/xpg4/bin/grep <PID>
or
/usr/bin/ps -o pid -o args -p <PID> | sed 1d


Mapping the sockname to port using the port number:
--------------------------------------------------
for i in `ps -e|awk '{print $1}'`; do echo $i; pfiles $i 2>/dev/null | grep 'port: 8080'; done
or
pfiles -F /proc/* | nawk '/^[0-9]+/ { proc=$2} ; /ockname: AF_INET/ { print proc "\n " $0 }'


There were two explanations why "lsof" did not show, what was expected:

1) One thing that might prevent lsof to print all, is if the ports are controlled by inetd (i.e. there is nothing actively listening on them until you try talking to them).

2) On Solaris 10, using "lsof -i" to show mapping of processes to TCP ports incorrectly shows all processes that have socket open. This is a known bug in lsof that can _not_ be fixed because of differences between Solaris 10 and previous versions. So the useful "lsof -i :<port>" is now not useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top