前不久,看了一些这方面的资料,现汇总一下:
Windows平台
比如测试端口1793是谁在使用,首先找到进程id.
PS C:\Users\d00101270> netstat -aon | findstr "1793"
TCP 10.70.103.84:1793 72.14.213.113:443 SYN_SENT 792
上面最后一列是进程id, 我们使用tasklist来查找进程id对应的进程名
PS C:\Users\d00101270> tasklist | findstr "792"
GoogleUpdate.exe 792 Console 1 8,220 K
Linux
第一种方案:
$netstat -pan | grep 19916
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 10.71.173.225:19916 0.0.0.0:* LISTEN 28517/DiameterAdpt
第二种方案:
$lsof -i:23
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
telnet 10582 d101270 3u IPv4 263403399 TCP linux227.huawei:61172->10.71.173.225:telnet (ESTABLISHED)
AIX
$netstat -Aan|grep 30542
f10000f303321b58 tcp4 0 0 *.30542 *.* LISTEN
$rmsock f10000f303321b58 tcpcb
The socket 0x3321800 is being held by proccess 692476 (db2sysc).
HP
使用lsof -i 可以查到程序名,进程号, 如果查9090端口
#lsof -i|grep 9090
java 29607 sync 30u IPv4 0x12b6ce380 0t0 TCP *:9090 (LISTEN)
java 29607 sync 35u IPv4 0x122c194c0 0t1343 TCP zy:9090->10.17.109.110:1206 (ESTABLISHED)
java 29607 sync 36u IPv4 0xca46d9c0 0t2680 TCP zy:9090->172.16.4.109:1105 (ESTABLISHED)
java 29607 sync 37u IPv4 0x126e140c0 0t2796 TCP zy:9090->172.16.4.111:1094 (ESTABLISHED)
java 29607 sync 38u IPv4 0x105c25b80 0t2409 TCP zy:9090->172.16.4.122:1576 (ESTABLISHED)
java 29607 sync 39u IPv4 0x106bc4040 0t500 TCP zy:9090->10.17.77.96:1300 (ESTABLISHED)
java 29607 sync 41u IPv4 0xca2a1dc0 0t443 TCP zy:9090->132.97.238.187:1642 (ESTABLISHED)
java 29607 sync 42u IPv4 0x12245f200 0t2443 TCP zy:9090->132.97.191.143:2928 (ESTABLISHED)
java 29607 sync 47u IPv4 0x122c19dc0 0t2347 TCP zy:9090->10.17.109.110:1207 (ESTABLISHED)
#ps -ef|grep 29607
sync 29607 1 0 May 19 ? 163:02 /opt/java1.4/bin/PA_RISC2.0/java -server -Xms128m -Xmx512m -Dja
root 17445 16305 1 17:31:19 pts/to 0:00 grep 29607
SUN
第一种方案:
1. 使用下面shell script,先建立一个port.sh文件:
# more /tmp/port.sh
#!/bin/sh
for pid in `ls /proc`
do
pf=`/usr/bin/pfiles $pid 2>/dev/null`
if echo $pf | grep $1 > /dev/null 2>&1
then
echo $pid
/usr/bin/pargs $pid
fi
done
2. 运行port.sh, 传入端口号,比如53250 :
# /tmp/port.sh 53250
3. 运行结果如下:
1225
1225: /usr/lib/thunderbird/thunderbird-bin -UILocale zh-CN
-contentLocale CN
argv[0]: /usr/lib/thunderbird/thunderbird-bin
argv[1]: -UILocale
argv[2]: zh-CN
argv[3]: -contentLocale
argv[4]: CN
4212
4212: /bin/sh /tmp/port.sh 53250
argv[0]: /bin/sh
argv[1]: /tmp/port.sh
argv[2]: 53250
第二种方案:
下载lsof软件, 使用lsof软件可以实现,同Linux.
第三种方案:
使用MDB
from socket info (netstat output), you can know its vnode
from vnode info, you can know which process owns it
from process info, you can know its args, so comes the result.