lsof

lsof (Lista de archivos abiertos, en español) es una conocida herramienta de monitoreo de sistemas operativos tipo Unix que se utiliza para mostrar todos los archivos de disco que mantiene abiertos un determinado proceso (PID), incluyendo los sockets de red abiertos, tuberías ( pipes). lsof es software libre.

Comando:
lsof
Tipo:
cmd
Url:
http://people.freebsd.org/~abe/
Descripcion:
Lista de archivos abiertos
Plataforma:
Linux, AIX, Solaris
 $ lsof -i -n -P | grep java
   java    11819 lyonn   34u  IPv6  52669       TCP *:1095 (LISTEN)
   java    11819 lyonn   37u  IPv6  52671       TCP *:53872 (LISTEN)
   java    11819 lyonn   39u  IPv6  52732       TCP 10.225.183.218:41525->10.225.183.218:35812 (ESTABLISHED)
   java    11880 lyonn   34u  IPv6  52743       TCP *:1096 (LISTEN)
   java    11880 lyonn   37u  IPv6  52745       TCP *:33029 (LISTEN)
   java    11880 lyonn   39u  IPv6  52757       TCP 10.225.183.218:41530->10.225.18

Los parámetros pasados son:

   * -i Lists IP sockets.
   * -n Do not resolve hostnames (no DNS).
   * -P Do not resolve port names (lista el núumero de puerto en lugar del nombre).

Si no se indica ninguna opción restrictiva o parámetro lsof enumera todos los archivos abiertos en ese momento, que normalmente suelen ser bastantes.

Descargar código fuente de ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/.

Se tiene que tener un compilador instalado, en este caso se instaló el GCC para AIX.

Ejecutar:

# ./Configure aixgcc

En Solaris se puede usar este script en lugar del lsof

fuente: http://www.unix.ms/pcp/

pcp.sh

#!/usr/bin/ksh
#
# PCP (PID con Port)
# v1.10 08/10/2010 Sam Nelson sam @ unix.ms
#
# If you have a Solaris 8, 9 or 10 box and you can't
# install lsof, try this. It maps PIDS to ports and vice versa.
# It also shows you which peers are connected on which port.
# Wildcards are accepted for -p and -P options.
#
# Many thanks Daniel Trinkle trinkle @ cs.purdue.edu
# for the help, much appreciated.
i=0
while getopts :p:P:a opt
do
case "${opt}" in
p ) port="${OPTARG}";i=3;;
P ) pid="${OPTARG}";i=3;;
a ) all=all;i=2;;
esac
done
if [ $OPTIND != $i ]
then
echo >&2 "usage: $0 [-p PORT] [-P PID] [-a] (Wildcards OK) "
exit 1
fi
shift `expr $OPTIND - 1`
if [ "$port" ]
then
# Enter the port number, get the PID
#
port=${OPTARG}
echo "PID\tProcess Name and Port"
echo "_________________________________________________________"
for proc in `ptree -a | awk '/ptree/ {next} {print $1};'`
do
result=`pfiles $proc 2> /dev/null| egrep "port: $port$"`
if [ ! -z "$result" ]
then
program=`ps -fo comm= -p $proc`
echo "$proc\t$program\t$port\n$result"
echo "_________________________________________________________"
fi
done
elif [ "$pid" ]
then
# Enter the PID, get the port
#
pid=$OPTARG
# Print out the information
echo "PID\tProcess Name and Port"
echo "_________________________________________________________"
for proc in `ptree -a | awk '/ptree/ {next} $1 ~ /^'"$pid"'$/ {print $1};'`
do
result=`pfiles $proc 2> /dev/null| egrep port:`
if [ ! -z "$result" ]
then
program=`ps -fo comm= -p $proc`
echo "$proc\t$program\n$result"
echo "_________________________________________________________"
fi
done
elif [ $all ]
then
# Show all PIDs, Ports and Peers
#
echo "PID\tProcess Name and Port"
echo "_________________________________________________________"
for proc in `ptree -a | sort -n | awk '/ptree/ {next} {print $1};'`
do
out=`pfiles $proc 2>/dev/null| egrep "port:"`
if [ ! -z "$out" ]
then
name=`ps -fo comm= -p $proc`
echo "$proc\t$name\n$out"
echo "_________________________________________________________"
fi
done
fi
exit 0