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

标签 linux parsing command sunos

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

$netstat -tlnp
netstat: illegal option -- t

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

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

最佳答案

我从某处得到了他的剧本。登录到 solaris 系统。打开 vi 编辑器。进入插入模式。复制并粘贴此脚本。关闭文件。授予执行权限。使用 -p 或 -P swithc 运行此脚本。它将提供包含 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

关于linux - 从 SunOS 中的 netstat 命令获取进程名称、pid 和端口映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5960183/

相关文章:

.net - 为什么 .NET 不能解析带时区的日期字符串?

linux - 如何在 Bash 中使一组命令超时

c# - 将命令绑定(bind)到 ItemTemplate 内的按钮

linux - 控制另一个进程的内存映射

linux - bash 中的日历计算

linux - 软件配置文件,我应该把它们放在Linux系统的什么地方?

python - Microsoft Powerpoint Python 解析器

c - 所有信号的信号处理程序

c - 从标准输入读取输入直到空行

windows - 运行.bat文件的命令