从SunOS中的netstat命令获取进程名称,pid和端口映射

时间:2011-05-11 06:34:43

标签: linux parsing command sunos

我正在尝试将端口号映射到正在运行/使用SunOS端口的应用程序

$netstat -tlnp
netstat: illegal option -- t

似乎-t选项在SunOS中是非法的。

我怎样才能获得这种映射?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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端口。