solaris10下看端口被哪个进程占用

在linux下,查询端口被哪个进程占用很简单:

[root@gd-ip-probe ~]# netstat -pan|grep 1521
tcp        0      0 192.168.0.89:47433          192.168.0.131:1521          ESTABLISHED 56130/ipprobe       
[root@gd-ip-probe ~]# ps -ef|grep 56130
root     53956 53179  0 14:02 pts/2    00:00:00 grep 56130
roamware 56130     1 90 Jan23 ?        6-00:31:40 /opt/Roamware/binaries/ipprobe/bin/ipprobe -c /opt/Roamware/binaries/ipprobe/config/IPProbe.cfg


在solaris10下就有点麻烦,一旦遇到紧急故障要处理,比如,要临时杀掉占用某个端口的进程,没有一个简单命令可以搞定。要用pfiles命令处理,网上看了几个脚本,有的只显示了pid和端口关系,有的看不出哪个是listening,哪个是peer,还是自己动手写了一个:

#!/bin/bash
for i in /proc/*
do
if pfiles $i 2>/dev/null| grep -i "port: $1" >/dev/null 2>&1;then
pinfo=`/usr/ucb/ps -auxww|grep \`echo $i|awk -F"/" '{print $3}'\`|grep -v grep`
  if pfiles $i 2>/dev/null| grep -i "peername.*port: $1" >/dev/null 2>&1;then
    echo -e "peer:\n$pinfo"
  else
    echo -e "listening:\n$pinfo"
  fi
fi
done


用法:

root@MSPRG-AP1 # port.sh 4803

listening:

roamware 19433  0.0 0.0 4688 3304 ?        S   Jan 27 0:13 /opt/Roamware/binaries/spread/bin/spread -c/opt/Roamware/binaries/spread/config/spread.conf

peer:

roamware 19455  0.0 1.41157912439968 ?        S   Jan 27 11:09 java -Xmx1024M -Xms1024M-server -Dapp=MSPRG_SDS com.roamware.sds2.fsmlib.FSMApp

peer:

roamware 19482  0.0 0.12424822608 ?        S   Jan 27 0:25 /opt/Roamware/binaries/storeserver/bin/StoreServer -c/opt/Roamware/binaries/storeserver/config/StoreServer.cfg

 

这里显示spread进程在监听4803端口,msprg_sds进程storeserver进程与其建立了连接。


你可能感兴趣的:(端口,进程,solaris10)