【问题标题】:how to get process id attached with particular port in sunos如何在 sunos 中获取附加到特定端口的进程 ID
【发布时间】:2012-10-26 03:03:04
【问题描述】:

我正在尝试在 SunOS 上使用端口 7085 连接进程。我尝试了以下命令。

netstat -ntlp | grep 7085 没有返回任何东西

netstat -anop | grep 7085 也试过这个。此开关在 SunOs 中无效

我得到以下输出。

#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]

SunOS 的版本是 SunOS 5.10。我相信 netstat 是唯一可以做到这一点的命令。

netstat 的确切开关是什么,它将为我提供端口附加的进程 ID?

【问题讨论】:

  • 哪个版本的 SunOS?十几年了还以为叫Solaris!!!
  • @basile。版本为 SunOS 5.10 Generic_118833-33 sun4v sparc SUNW,Sun-Fire-T200
  • @Basile.. 仅供参考,我以超级用户身份运行此命令...
  • 请编辑您的问题以改进它。
  • 您在系统上尝试过man netstat 吗?你应该使用man

标签: linux unix operating-system sunos


【解决方案1】:

查看lsofhttp://linux.about.com/library/cmd/blcmdl8_lsof.htm 命令。

此命令描述了哪些进程正在使用哪些文件描述符。请记住,端口 7085 上的任何内容都将具有自己的文件描述符,您可以使用它来追溯使用它的进程。

我会尝试类似:

$ lsof -i :7085

希望对您有所帮助。

【讨论】:

  • 是的。我试过了,不幸的是该命令不可用。我会安装这个软件包,但它是生产服务器,我无权在服务器上安装任何东西,而且不仅有 1 台服务器,还有 200 多台服务器。我正在尝试获取有关 solaris 上可用命令的帮助...
  • @LOGAN 你有定影器吗?试试这个: fuser -4 -v -n tcp 7085
  • 它不工作。我得到了 put.fuser: 非法选项 -- 4 Illegal option ?. Usage: fuser [-[k|s sig]un[c|f|d]] files [-[[k|s sig]un[c|f|d]] files]..
【解决方案2】:

我从HERE 得到了他的剧本。登录到 Solaris 系统。打开 vi 编辑器。进入插入模式。复制并粘贴此脚本。保存文件并命名为 PCP。授予执行权限。使用 -p 或 -P swithc 运行此脚本。它将提供带有 PID、PROCESS 名称和端口的输出。

确保您需要在 ksh shell 中才能执行它。

PCP 是一个脚本,它使管理员能够查看 Solaris 系统上正在使用的开放 TCP 端口。它将端口映射到 PID,反之亦然。它接受通配符,并且一目了然地显示所有打开的端口及其对应的 PID。很好的脚本提供了非常好的输出。试试看吧。

示例: #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

【讨论】:

  • 删除作者/信用姓名是一种糟糕的做法。 unix.ms/pcp
【解决方案3】:
pfiles /proc/* 2>/dev/null | nawk '
/^[0-9]*:/ { pid=$0 }
/port: 7085$/ { printf("%s %s\n",pid,$0);}'
  • pfiles /proc/* 正在检索所有进程文件描述符详细信息
  • 2>/dev/null 正在丢弃错误,因为瞬态进程在此期间死亡
  • 每行以数字开头,后跟一个冒号报告进程 ID 和详细信息,它存储在 awk pid 变量中
  • 当一行以字符串port: <portnumber>(这里是7085)结尾时,会显示对应的pid变量。

注意:您需要所需的权限才能从您不拥有的进程中获取端口信息(root 拥有所有权限)。

【讨论】:

  • 对我不起作用。几秒钟后命令没有返回任何内容。
  • 我在内部使用的脚本它使用 pfiles 来获取详细信息。
  • @jiliagre。这个按预期工作正常。如果你能给出这个脚本的解释,我将来可以根据我的需要改变它..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 2023-03-31
  • 2012-11-15
  • 2020-06-11
  • 1970-01-01
  • 2019-04-30
相关资源
最近更新 更多