Comment savoir qui écoute sur quel port, question inévitable sur unix/linux.
On utilise en général lsof mais sur solaris on peut faire plus simple grace au script pcp.
se-srm-dev srmadm # ./pcp -p 587 
PID Process Name and Port
_________________________________________________________
9163 /usr/lib/sendmail 587
sockname: AF_INET 0.0.0.0 port: 587
_________________________________________________________
Pratique non !
C'est un script trouvé sur cette page.
Voici le code modifé pour checker si l'utilisateur est bien root :
#!/usr/bin/ksh
ID=`id | cut -f 2 -d '=' | cut -f 1 -d '('`
if [ $ID -ne 0 ] ;
then
echo >&2 "Need to be run by root"
exit 1
fi
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
Sous linux
/ # fuser -n tcp 8080
8080/tcp:             8349
/ # lsof -i :8080
COMMAND  PID   USER   FD   TYPE  DEVICE SIZE NODE NAME
java    8349 tomcat    3u  IPv4 2435417       TCP *:tomcat (LISTEN)
#### SHOW TIMER
/ # netstat -nao
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       Timer
tcp        0      0 127.0.0.1:8005          0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 0.0.0.0:8009            0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0     48 172.31.208.143:22       172.26.44.48:13277      ESTABLISHED on (0.33/0/0)
tcp        0      0 172.31.208.143:22       172.28.120.138:4508     ESTABLISHED keepalive (1857.58/0/0)
udp        0      0 172.31.208.143:32770    172.31.208.50:514       ESTABLISHED off (0.00/0/0)
#### SHOW PID/PRG
/ #  netstat -lnp 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 127.0.0.1:8005          0.0.0.0:*               LISTEN      8349/java           
tcp        0      0 0.0.0.0:8009            0.0.0.0:*               LISTEN      8349/java           
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2473/portmap        
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      8349/java           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2980/sshd