Friday, March 17, 2017

How to check which process is holding the port

How to check which process is holding the port

Most of the time we are in situation to find the process which is holding the same port which may cause the application issues. This post will help us whenever we are in same position. Yes, we can find the  process with using port in different methods. Let we start to search it.....

root@unixrock # ndd /dev/tcp tcp_listen_hash
    TCP            zone IP addr         port  seqnum   backlog (q0/q/max)
----skipped------
281 ffffffff81d46440 0 ::ffff:127.0.0.1 00025 00000000 0/0/16
----skipped------
from the above output we can find the process of the port 25, please follow the below steps
root@unixrock # mdb -k
Loading modules:[unix krtld genunix specfs dtrace cpu.generic uppc pcplusmp ufs md mpt ip hook neti sctp arp usba fctl nca lofs audiosup cpc fcip random crypto zfs logindmux ptm sppp sata nfs]
>
> ffffffff81d46440::print tcp_t tcp_rq
tcp_rq = 0xffffffff8961f028
>
> 0xffffffff8961f028::q2stream
ffffffff87052e10
>
> ffffffff87052e10$ 0xffffffff874bc780::whereopen
file ffffffff893689d8
ffffffff88dc8900
>
> ffffffff88dc8900::ps
S    PID   PPID   PGID    SID    UID      FLAGS             ADDR NAME
R    935      1    935    935      0 0x52010000 ffffffff88dc8900 sendmail
>
root@unixrock #
Yes !!! we have found the process (sendmail) which using the port 25

we can use lsof utility to find the process with using port. although lsof will not be available on all solaris server, we have to install the lsof package. NOTE : This utlity can't be used in Non-global zones.
root@unixrock # /usr/local/bin/lsof -i :25
lsof: WARNING: access /.lsof_unixrock: No such file or directory
lsof: WARNING: created device cache file: /.lsof_unixrock
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
sendmail 935 root    5u  IPv4 0xffffffff81d46240      0t0  TCP localhost:smtp (LISTEN)
root@unixrock #
Yes, we have found the process with using port by using lsof utility.

Now our concern would be how to check the process with using port in non-global zone level. Yes, we have the option to do that by using our customized script. Please follow the steps...
root@unixrock # cat /var/tmp/check_port.sh
#!/bin/ksh

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

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

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

Copy the script and executed as mentioned below
root@unixrock # /var/tmp/check_port.sh
Enter port you would like to know pid for: 25
---------------------------------------------
Port: 25 is being used by PID:  935 /usr/lib/sendmail -bl -q15m
root@unixrock #
Yes...We have found the process with using the port by executing customized scripts.

No comments: