Linux 如何在 sunos 中获取附加到特定端口的进程 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13246309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get process id attached with particular port in sunos
提问by LOGAN
I am trying to get processes attached with a port 7085 on SunOS. i tried following commands.
我正在尝试在 SunOS 上使用端口 7085 连接进程。我尝试了以下命令。
netstat -ntlp | grep 7085
didn't return anything
netstat -ntlp | grep 7085
没有返回任何东西
netstat -anop | grep 7085
tried this one also. This switches are not valid in SunOs
netstat -anop | grep 7085
也试过这个。此开关在 SunOs 中无效
I am getting the following output.
我得到以下输出。
#netstat -anop
#netstat -anop
netstat: illegal option -- o
netstat: illegal option -- o
usage: netstat [-anv] [-f address_family]
usage: netstat [-anv] [-f address_family]
netstat [-n] [-f address_family] [-P protocol] [-g | -p | -s [interval [count]]]
netstat [-n] [-f address_family] [-P protocol] [-g | -p | -s [interval [count]]]
netstat -m [-v] [interval [count]]
netstat -m [-v] [interval [count]]
netstat -i [-I interface] [-an] [-f address_family] [interval [count]]
netstat -i [-I interface] [-an] [-f address_family] [interval [count]]
netstat -r [-anv] [-f address_family|filter]
netstat -r [-anv] [-f address_family|filter]
netstat -M [-ns] [-f address_family]
netstat -M [-ns] [-f address_family]
netstat -D [-I interface] [-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.
SunOS 的版本是 SunOS 5.10。我相信 netstat 是唯一可以做到这一点的命令。
What is the exact switches for netstat which will give me the process id attached with port?
netstat 的确切开关是什么,它将为我提供与端口相关的进程 ID?
采纳答案by jlliagre
pfiles /proc/* 2>/dev/null | nawk '
/^[0-9]*:/ { pid=$ lsof -i :7085
}
/port: 7085$/ { printf("%s %s\n",pid,#!/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: ##代码## [-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 };'`
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} ~ /^'"$pid"'$/ {print };'`
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 };'`
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
);}'
pfiles /proc/*
is retrieving all processes file descriptors details2>/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.
pfiles /proc/*
正在检索所有进程文件描述符的详细信息2>/dev/null
由于瞬态进程在此期间死亡而退出错误- 每行以数字开头,后跟冒号,报告进程 ID 和详细信息,它存储在 awk pid 变量中
- 当一行以字符串结尾
port: <portnumber>
(这里是 7085)时,会显示相应的 pid 变量。
Note: you need the required privilege(s) to get port information from processes you do not own (root has all privileges).
注意:您需要所需的权限才能从您不拥有的进程中获取端口信息(root 拥有所有权限)。
回答by andrefsp
Have a look on lsof
http://linux.about.com/library/cmd/blcmdl8_lsof.htmcommand.
查看lsof
http://linux.about.com/library/cmd/blcmdl8_lsof.htm命令。
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.
该命令描述了哪些进程正在使用哪些文件描述符。请记住,端口 7085 上的任何内容都有其自己的文件描述符,您可以使用它来追溯到使用它的进程。
I would try something like:
我会尝试类似的东西:
##代码##Hope it can help.
希望它能有所帮助。
回答by LOGAN
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.
我从这里得到了他的剧本。登录solaris系统。打开 vi 编辑器。进入插入模式。复制并粘贴此脚本。保存文件并命名为 PCP。授予执行权限。使用 -p 或 -P swithc 运行此脚本。它将给出带有 PID、进程名称和端口的输出。
Make sure you need to be in ksh shell to execute it.
确保您需要在 ksh shell 中执行它。
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.
PCP 是一个脚本,使管理员能够查看 Solaris 系统上正在使用的开放 TCP 端口。它将端口映射到 PID,反之亦然。它接受通配符,还会一目了然地显示所有打开的端口及其相应的 PID。很好的脚本给出了非常好的输出。就试一试吧。
Example:
#pcp -p PORT_NUMBER or #pcp -P PROCESS_ID
例子:
#pcp -p PORT_NUMBER or #pcp -P PROCESS_ID