SunOS 中通过 netstat 命令获取进程名称、pid 和端口映射

发布于 2024-11-06 12:38:07 字数 165 浏览 4 评论 0原文

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

$netstat -tlnp
netstat: illegal option -- t

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

我怎样才能得到这个映射?

I am trying to get the mapping of port number to application that is running/using the port in SunOS

$netstat -tlnp
netstat: illegal option -- t

It seems the -t option is illegal in SunOS.

how can i get this mapping?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2024-11-13 12:38:07

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

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

例子:
#pcp -p PORT_NUMBER 或 #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

I got his script from somewhere. Log into solaris system. Open vi editor. Go into insert mode. Copy and paste this script. Close the file. Give execute permission. Run this script with -p or -P swithc. It will give an output with the PID, PROCESS Name and Port.

PCP is a script that enables administrators to see what open TCP ports are in use on a Solaris system. It maps ports to PIDs and vice versa. It accepts wildcards and will also show at a glance all open ports and their corresponding
PIDs. It is nice script gives a very fine out put. Just try it.

Example:
#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
冬天的雪花 2024-11-13 12:38:07

如果您没有安装 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); }}'

将 port 变量设置为您要查找的端口号(如果有),或者将其保留为未设置以查看所有正在使用的 IPV4 端口。

If you don't have lsof installed, here is one way using standard Solaris commands:

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); }}'

Set the port variable to the port number you are looking for, if any, or leave it unset to see all IPV4 ports in use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文