Determine what process is listening on a port on Solaris, without lsof

for x in `ptree | awk '{print $1}'`; do pfiles $x | grep ${PORT} > /dev/null 2>&1; if [ x"$?" == "x0" ]; then ps -ef | grep $x | grep -v grep; fi; done 2> /dev/null
Can use lsof, but since it's not part of the base OS, it's not always available.
Sample Output
root@shell02:~# for x in `ptree | awk '{print $1}'`; do pfiles $x | grep -w ${PORT} > /dev/null 2>&1; if [ x"$?" == "x0" ]; then ps -ef | grep $x | grep -v grep; fi; done 2> /dev/null
    root  5386  5331   0   Oct 06 ?           1:29 /lib/svc/bin/svc.configd
    root  6211  6209   0   Oct 06 ?           0:02 /usr/lib/autofs/automountd
    root 17437  6425   0 09:02:56 ?           0:00 /usr/lib/ssh/sshd
    root  6425  5331   0   Nov 10 ?          18:54 /usr/lib/ssh/sshd
    root 17437  6425   0 09:02:56 ?           0:00 /usr/lib/ssh/sshd
     bob 17438 17437   0 09:02:56 ?           0:00 /usr/lib/ssh/sshd
     bob 17438 17437   0 09:02:56 ?           0:00 /usr/lib/ssh/sshd
     bob 17441 17438   0 09:02:57 pts/1       0:00 -sh
  daemon 12227  5331   0   Dec 03 ?           0:00 /usr/lib/nfs/statd
  daemon 12232  5331   0   Dec 03 ?           0:00 /usr/lib/nfs/lockd
root@shell02:~# 

0
By: bpfx
2010-01-05 17:02:23

These Might Interest You

  • another formatting/oneliner for lsof User - Process - Port Show Sample Output


    3
    alias oports="echo 'User: Command: Port:'; echo '----------------------------' ; lsof -i 4 -P -n | grep -i 'listen' | awk '{print \$3, \$1, \$9}' | sed 's/ [a-z0-9\.\*]*:/ /' | sort -k 3 -n |xargs printf '%-10s %-10s %-10s\n' | uniq"
    wickedcpj · 2011-08-02 04:54:25 0
  • A way not so simple but functional for print the command for the process that's listening a specific port. I got the pid from lsof because I think it's more portable but can be used netstat netstat -tlnp Show Sample Output


    0
    port=8888;pid=$(lsof -Pan -i tcp -i udp | grep ":$port"|tr -s " " | cut -d" " -f2); ps -Afe|grep "$pid"|grep --invert-match grep | sed "s/^\([^ ]*[ ]*\)\{7\}\(.*\)$/\2/g"
    glaudiston · 2010-01-11 17:49:22 4
  • lsof - cleaned up for just open listening ports, the process, and the owner of the process - simple as lsof man suggests Show Sample Output


    0
    lsof -iTCP -sTCP:LISTEN
    hukketto · 2011-08-09 00:17:02 0
  • This command is more portable than it's cousin netstat. It works well on all the BSDs, GNU/Linux, AIX and Mac OS X. You won't find lsof by default on Solaris or HPUX by default, but packages exist around the web for installation, if needed, and the command works as shown. This is the most portable command I can find that lists listening ports and their associated pid. Show Sample Output


    27
    lsof -Pan -i tcp -i udp
    atoponce · 2010-06-07 15:22:44 0

What Others Think

Sounds good only one problem: NEVER do this on production system. The 'pfiles' command effectively halts the process in order to inspect it. (see the man page). If your process is doing heavy socket i/o while being halted it will crash as a result.
MrKnowItAll · 350 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: