Who’s listening on that port? (Linux vs Solaris)

You install a new application… You try to start it up…

But it fails to bind to the port it needs…

Now – what in the world has tied up that port?

In Linux, netstat provides you that info…

# netstat -lnp | egrep "Local Address|^tcp|^udp"
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      2983/sshd
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      3266/0

But in Solaris, it’s not quite so easy… You’re going to have to work for it…

The following script gives you more or less the same thing…

# printf " PORT | INTERFACE       |   PID | FILEn"
printf "------+-----------------+-------+-------------------------------n"
for PID in `ps -ef | grep -v PID | awk '{print $2}' |  grep -v "^0$"`
do
  pfiles $PID 2> /dev/null | nawk '
  NR==1 {sub(/:/,"",$1); PID=$1; PROC=$2}
  /sockname:.*port:.*[0-9][0-9]/  { P2=$NF; IF2=$3; getline;
  if (!/peername/) {PORT=P2;IF=(IF2=="::"?"ALL":IF2)}}
  END { if (PORT != 0) printf ("%5d | %-15s | %5d | %sn",PORT,IF,PID,PROC) }'
done | sort -n
PORT | INTERFACE       |   PID | FILE
------+-----------------+-------+-------------------------------
22 | 172.16.200.2    |   329 | /usr/lib/ssh/sshd
80 | ALL             |   604 | /usr/local/apache2/bin/httpd
80 | ALL             |   608 | /usr/local/apache2/bin/httpd
80 | ALL             |   609 | /usr/local/apache2/bin/httpd
80 | ALL             |   610 | /usr/local/apache2/bin/httpd
80 | ALL             |   611 | /usr/local/apache2/bin/httpd
80 | ALL             |   612 | /usr/local/apache2/bin/httpd
6010 | 127.0.0.1       |   625 | /usr/lib/ssh/sshd