3

I have a fast question. I want to know what is the losf -i equivalent command in a Solaris system.

I only want to show the files with network connection.

Thank you!!

Alberto
  • 701
  • 4
  • 9
  • 25

3 Answers3

6

Here is a shell script that list all processes having open TCP or UDP ports on Solaris, you can limit it to a given port number by passing it as an argument:

pfiles /proc/* 2>/dev/null | nawk -v port=$1 '
    /^[0-9]/ { cmd=$2; type="unknown"; continue }
    $1 == "SOCK_STREAM" { type="tcp" }
    $1 == "SOCK_DGRAM" { type="udp" }
    $2 ~ "AF_INET" { if((port!="")&&($5!=port)) continue;
                      if(cmd!="") { printf("%s\n",cmd); cmd="" }
                      printf("    %s:%s/%s\n",$3,$5,type); }'

Note: As documented in the warning section of the pfiles manual page, it is not recommended to run this command on a heavily loaded production system with a time sensitive process running as deadlocks or crashes might happen.

Note #2: The previous warning doesn't apply to the last update of Solaris (Oracle Solaris 11.4) because pfiles no more suspend the monitored process(es). It now just uses ad hoc /proc pseudo files.

jlliagre
  • 29,783
  • 6
  • 61
  • 72
  • Can I do it in just one line?? Thank you – Alberto Dec 19 '13 at 18:04
  • Sure, make it a shell function or a shell script and you'll be able to call it with one single command line. – jlliagre Dec 19 '13 at 20:53
  • I'm sorry but I don't know how to do it. :S – Alberto Dec 19 '13 at 21:31
  • Something like `mkdir ~/Functions;cat >~/Functions/myLsof <<%function myLsof() { pfiles /proc/* ... ,type); }' % ; export FPATH=~/Functions` Just make sure the % appears in a different line. Feel free to ask a new question by itself if this is unclear. – jlliagre Dec 19 '13 at 22:34
  • Unfortunately I've seen cases where running `pfiles` on a process would crash that process. The reason is that `pfiles` temporarily halts the process in order to inspect it. I would think that 99.9% of all processes are immune to this but a process which is very 'time sensitive' may not like it. It may have been an odd case but it was reproducible, i.e. you could crash the process *every time* just by running `pfiles` on it. You've been warned! – peterh Jan 10 '14 at 08:00
  • @nolan6000 indeed, although I would say more like 99.99999+ are immune. Answer updated. – jlliagre Jan 10 '14 at 08:35
5

As of Solaris 11.2 this type of information is now available directly in the netstat command (-u option) so you don't have to use the pfiles hack for the purpose or use the lsof tool. Personally I've always wondered why this information could not be part of the netstat output so glad to see that'll finally be the case.

There's a nice blog from Oracle on the topic.

(caveat: at the time of writing v11.2 is in beta but fully disclosed as to the contents / new features)

peterh
  • 18,404
  • 12
  • 87
  • 115
1

you can try pfiles,fuser. you can install lsof on solaris.

http://andriigrytsenko.net/2010/08/lsof-installation-on-solaris-10/

Tharanga Abeyseela
  • 3,255
  • 4
  • 33
  • 45