Wednesday, April 9, 2014

Find the process id of the application running on certain port


This script helps you to find the application running on particular port.
To execure the script, make the script executable and supply the port as a command line parameter.
# netstat -an | grep 2051 ( to see if port is used)
# sh port_proc.sh <port_num>
# sh port_proc.sh 2051 (To check the process of the PID)

root@sunserv01:/var/tmp > cat port_proc.sh
#!/bin/bash
 # is the port we are looking for
if [ $# -lt 1 ]
 then
 echo "Please provide a port number parameter for this script"
 echo "e.g. %content 2051"
 exit
 fi
echo "Greping for your port, please be patient (CTRL+C breaks) . "
for i in `ls /proc`
 do
 pfiles $i | grep AF_INET | grep $1
 if [ $? -eq 0 ]
 then
 echo Is owned by pid $i
 echo "-----------------"
 fi
 done
root@sunserv01:/var/tmp >

root@sunserv01:/var/tmp > sh port_proc.sh 2051
Greping for your port, please be patient (CTRL+C breaks) .
pfiles: cannot examine 1291: no such process
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
        sockname: AF_INET 192.168.10.184  port: 2051
Is owned by pid 6692
---------------
root@sunserv01:/var/tmp > root@sunserv01:/var/tmp > ptree 6692
6692  /opt/sybase1253/ASE-12_5/bin/backupserver -e/opt/sybase1253/ASE-12_5/install/er
root@sunserv01:/var/tmp > ptree 6692
6692  /opt/sybase1253/ASE-12_5/bin/backupserver -e/opt/sybase1253/ASE-12_5/install/er
root@sunserv01:/var/tmp >

source: http://onlineappsdba.com/index.php/2008/06/10/how-to-find-pid-listening-particular-port-on-linuxsolaris-10/

No comments:

Post a Comment