如何获得与特定端口连接的进程ID

问题描述:

我想在SunOS上使用端口7085获得进程。我试过下面的命令。如何获得与特定端口连接的进程ID

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的版本是5.10的SunOS。我相信netstat是唯一可以做到这一点的命令。

netstat的确切开关是什么,它会给我带有端口的进程ID?

+0

哪个版本的SunOS?我认为这是超过十年以来所谓的Solaris! –

+0

@basile。该版本是SunOS 5.10 Generic_118833-33 sun4v sparc SUNW,Sun-Fire-T200 – LOGAN

+0

@Basile ..仅供参考,我正在运行这个Commans作为超级用户... – LOGAN

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拥有所有特权)获取端口信息。

+1

没有为我工作。几秒钟后命令没有返回任何东西。 – LOGAN

+2

我在内部使用的脚本使用pfiles来获取详细信息。 – LOGAN

+2

代码简化和修复。 – jlliagre

看看lsof http://linux.about.com/library/cmd/blcmdl8_lsof.htm命令。

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

我会尝试这样的:

$ lsof -i :7085 

希望它可以帮助。

+0

是的。我试图说不幸的是,该命令不可用。我会安装这个软件包,但它是生产服务器,我没有权利在服务器上安装任何东西,并且不止一台服务器有超过200多台服务器。我试图得到solaris上可用命令的帮助... – LOGAN

+0

@LOGAN你有热熔器吗?试试这个: fuser -4 -v -n tcp 7085 – andrefsp

+0

它不起作用。我得到了以下put.fuser:非法选项 - 4 '非法选项? 用法:fuser [ - [k | s sig] un [c | f | d]] files [ - [[k | s sig] un [c | f | d]] files] ..' – LOGAN

我从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 
+0

剥离作者/信用名称是一个不好的做法。 http://www.unix.ms/pcp/ – jlliagre

+0

@ jiliagre。这不是专有的。我编辑过它。 :) – LOGAN