2011-05-11 102 views

回答

0

如果你没有安装lsof的你,这里是一个使用标准Solaris单向命令:

pfiles /proc/* 2>/dev/null | nawk -v port=$port ' 
/^[0-9]/ { cmd=$0; type="unknown"; continue } 
$1 == "SOCK_STREAM" { type="tcp" } 
$1 == "SOCK_DGRAM" { type="udp" } 
$2 == "AF_INET" { if((port!="")&&($5!=port)) continue; 
        if(cmd!="") { printf("%s\n %s:%s/%s\n",cmd,$3,$5,type); cmd="" } 
        else { printf(" %s:%s/%s\n",cmd,$3,$5,type); }}' 

将端口设置变量,你正在寻找的端口号,如果有的话,或设置就可以到查看正在使用的所有IPV4端口。

1

我从某处得到了他的剧本。登录solaris系统。打开vi编辑器。进入插入模式。复制并粘贴此脚本。关闭文件。给予执行权限。使用-p或-P swithc运行此脚本。它将给出一个带有PID,PROCESS名称和端口的输出。

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

实施例: #pcp -p PORT_NUMBER or #pcp -P PROCESS_ID

#!/usr/bin/ksh 
# 
# Wildcards are accepted for -p and -P options. 
# 
# 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

尼斯脚本。需要对我的5.10系统上的“-P”选项稍作调整,但其他方面相当不错。 *那里的帮助,非常感谢*评论来自? – Signal15 2014-12-03 18:19:17