how to get process id attached with particular port in sunos

僤鯓⒐⒋嵵緔 提交于 2019-12-04 23:56:07

问题


I am trying to get processes attached with a port 7085 on SunOS. i tried following commands.

netstat -ntlp | grep 7085 didn't return anything

netstat -anop | grep 7085 tried this one also. This switches are not valid in SunOs

I am getting the following output.

#netstat -anop

netstat: illegal option -- o

usage: netstat [-anv] [-f address_family]

netstat [-n] [-f address_family] [-P protocol] [-g | -p | -s [interval [count]]]

netstat -m [-v] [interval [count]]

netstat -i [-I interface] [-an] [-f address_family] [interval [count]]

netstat -r [-anv] [-f address_family|filter]

netstat -M [-ns] [-f address_family]

netstat -D [-I interface] [-f address_family]

The version of SunOS is SunOS 5.10. I believe netstat is the only command can do this.

What is the exact switches for netstat which will give me the process id attached with port?


回答1:


pfiles /proc/* 2>/dev/null | nawk '
/^[0-9]*:/ { pid=$0 }
/port: 7085$/ { printf("%s %s\n",pid,$0);}'
  • pfiles /proc/* is retrieving all processes file descriptors details
  • 2>/dev/null is dropping out errors due to transient processes died in the meantime
  • each line starting with a number followed by a colon reports the process id and details, it is stored in the awk pid variable
  • when a line ends with the string port: <portnumber> (here is 7085), the corresponding pid variable is displayed.

Note: you need the required privilege(s) to get port information from processes you do not own (root has all privileges).




回答2:


Have a look on lsof http://linux.about.com/library/cmd/blcmdl8_lsof.htm command.

This command describes which processes are using which file descriptors. Remember that anything on port 7085 will have its own file descriptor which you can use to trace back to the process using it.

I would try something like:

$ lsof -i :7085

Hope it can help.




回答3:


I got his script from HERE . Log into solaris system. Open vi editor. Go into insert mode. Copy and paste this script. save the file and give the name PCP. Give execute permission. Run this script with -p or -P swithc. It will give an output with the PID, PROCESS Name and Port.

Make sure you need to be in ksh shell to execute it.

PCP is a script that enables administrators to see what open TCP ports are in use on a Solaris system. It maps ports to PIDs and vice versa. It accepts wildcards and will also show at a glance all open ports and their corresponding PIDs. It is nice script gives a very fine out put. Just try it.

Example: #pcp -p PORT_NUMBER or #pcp -P PROCESS_ID

#!/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


来源:https://stackoverflow.com/questions/13246309/how-to-get-process-id-attached-with-particular-port-in-sunos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!