【问题标题】:Combine output of Netstat and PS - linux结合Netstat和PS的输出 - linux
【发布时间】:2018-08-03 09:30:02
【问题描述】:

我想合并以下命令的输出:

-网络统计

    [root]# netstat -nltp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 127.0.0.1:32552 0.0.0.0:*               LISTEN     
 151634/java

-PS

[root]# ps -eo pid,cmd | grep 151634
130485 grep --color=auto 151634
151634 java -classpath

我想结合以下 2 个命令并有以下 OUTPUT(txt 文件):

PORT PID CMD
123  333 java/etc
234  444 java/etcetc
345  555 java/etcetcetc

我做了以下:

netstat -nltp | awk '{print $4}' | sed -e 's/.*://' 

这会打印来自 netstat -nltp 输出的端口

for i in `netstat -nltp | awk {'print $7}' | awk -F '/' {'print $1'} | uniq` ; do ps -eo pid,cmd | grep $(echo $i | sed "s/^\(.\)/[\1]/g") ; done 

这会从 netstat -nltp 命令获取 PID,然后从 PS 命令显示 PID 和 CMD(也排除显示 grep --color=auto 结果

非常感谢!

LE:我删除了输出 html 以避免混淆。这只是一个关于它应该是什么样子的例子。

【问题讨论】:

    标签: linux parsing output pid netstat


    【解决方案1】:

    非常感谢 JUSHJUSH,

    我在这里写了我的回复,因为评论回复太长了。

    我也用那个 FOR 更新了脚本并且它工作了,但是,脚本似乎没有获得正确的输出。

    下面我会尽量解释

    如果我有以下 netstat -nltp 输出

    tcp 0 0 127.0.0.1:32552 0.0.0.0:* LISTEN 151634/java tcp 0 0 10.77.66.33:8081 0.0.0.0:* LISTEN 151634/java tcp 0 0 0.0.0.0:7070 0.0.0.0:* LISTEN 151634/java tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 151634/java tcp 0 0 0.0.0.0:20100 0.0.0.0:* LISTEN 151634/java tcp 0 0 0.0.0.0:20101 0.0.0.0:* LISTEN 151634/java tcp 0 0 0.0.0.0:20102 0.0.0.0:* LISTEN 151634/java

    PID=151634 的 CMD 是 java -classpath /opt/application/nifi-1.6.0/./conf:/opt/application/nifi-1.6.0/./lib/jetty-schemas-3.1.jar:

    脚本给出以下输出: PORT PID CMD 32552 151634 java -classpath /opt/application/nifi-1.6.0/./conf:/opt/application/nifi-1.6.0/./lib/jetty-schemas-3.1.jar: 32552 151634 java -classpath /opt/application/nifi-1.6.0/./conf:/opt/application/nifi-1.6.0/./lib/jetty-schemas-3.1.jar: 32552 151634 java -classpath /opt/application/nifi-1.6.0/./conf:/opt/application/nifi-1.6.0/./lib/jetty-schemas-3.1.jar: 32552 151634 java -classpath /opt/application/nifi-1.6.0/./conf:/opt/application/nifi-1.6.0/./lib/jetty-schemas-3.1.jar: 32552 151634 java -classpath /opt/application/nifi-1.6.0/./conf:/opt/application/nifi-1.6.0/./lib/jetty-schemas-3.1.jar: 32552 151634 java -classpath /opt/application/nifi-1.6.0/./conf:/opt/application/nifi-1.6.0/./lib/jetty-schemas-3.1.jar: 32552 151634 java -classpath /opt/application/nifi-1.6.0/./conf:/opt/application/nifi-1.6.0/./lib/jetty-schemas-3.1.jar:

    输出应该是 PORT PID CMD 32552 151634 CMD from above 8081 151634 CMD from above 7070 151634 CMD from above 9090 151634 CMD from above 20100 151634 CMD from above 20101 151634 CMD from above 20102 151634 CMD from above

    或者,另一个例子:

    如果我有以下 netstat -nltp 输出

    tcp 0 0 127.0.0.1:3030 0.0.0.0:* LISTEN 88284/ruby tcp 0 0 127.0.0.1:3031 0.0.0.0:* LISTEN 88284/ruby

    PID=88284 的 CMD 是 /opt/sensu/embedded/bin/ruby /opt/sensu/bin/sensu-client -c /etc/sensu/config.json -d /etc/sensu/conf.d -e /etc/sensu/extensions -p /var/run/sensu/sensu-client.pid -l /var/log/sensu/sensu-client.log -L warn

    脚本给出以下输出: PORT PID CMD 3030 88284 /opt/sensu/embedded/bin/ruby /opt/sensu/bin/sensu-client -c /etc/sensu/config.json -d /etc/sensu/conf.d -e /etc/sensu/extensions -p /var/run/sensu/sensu-client.pid -l /var/log/sensu/sensu-client.log -L warn 3030 88284 /opt/sensu/embedded/bin/ruby /opt/sensu/bin/sensu-client -c /etc/sensu/config.json -d /etc/sensu/conf.d -e /etc/sensu/extensions -p /var/run/sensu/sensu-client.pid -l /var/log/sensu/sensu-client.log -L warn

    输出应该是 PORT PID CMD 3030 88284 CMD from above 3031 88284 CMD from above

    非常感谢您的帮助和耐心等待!

    【讨论】:

      【解决方案2】:

      好的,所以,我已经设法使脚本能够处理所有重复的问题等。 如果其他人需要,您可以在下面找到更新的脚本。 谢谢! :)

      netstat -ntlp | sed 1,2d > /tmp/output_netstat.txt
      
      echo PORT$'\t'PID$'\t'NAME
      for port in $(cat /tmp/output_netstat.txt | awk '{print $4 " " $7}' | sed -e 's/.*://' | awk '{print $1}' | uniq)
      do
          pid=$(cat /tmp/output_netstat.txt | grep -w "$port" | awk '{print $7}' | cut -d ' ' -f 7 | cut -d '/' -f 1 | uniq )
          ps_name=$(cat /tmp/output_netstat.txt | awk '{print $4 " " $7}' | sed -e 's/.*://' | sed 's/\// /g' | awk '{print $3}')
          ps_name_outputed=$(ps -ef | grep "$pid" | grep "$ps_name" | grep -v grep |tr -s ' '| sed 's/^[^0-9]*//g' | head -1 | cut -f2- -d/)
          echo  "$port"" ""$pid"" ""$ps_name_outputed"
      done
      
      rm -rf /tmp/output_netstat.txt
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-09
        • 1970-01-01
        • 2012-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-18
        相关资源
        最近更新 更多