结合Netstat和PS-Linux

时间:2018-08-03 09:30:03

标签: linux parsing output pid netstat

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

-NETSTAT

    [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以避免混淆。这只是有关其外观的一个示例。

3 个答案:

答案 0 :(得分:0)

#!/bin/bash

echo PORT$'\t'PID$'\t'NAME
for pid in $(netstat -nlpt | tail -n+3 | tr -s ' ' | cut -d ' ' -f 7 | cut -d '/' -f 1)
do
    ps=$(ps -eo pid,cmd | grep " $pid " | grep -v grep |tr -s ' '| sed 's/^[^0-9]*//g')
    name=$(echo "$ps" | cut -d' ' -f 2)
    cmd=$(echo "$ps" | cut -d' ' -f 3- )
    cutname=$(echo "$name" | rev | cut -d'/' -f 1| rev)
    port=$(netstat -nlpt |grep "${cutname}" | tr -s ' ' | cut -d' ' -f 4 | grep -Po ':\d+'| tr -d ':'| head -n1)
    echo  $port$'\t'$pid$'\t'$name $cmd

done

答案 1 :(得分:0)

非常感谢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 :(得分:0)

好的,所以,我设法使脚本解决了所有重复的问题,等等。 您可以在下面找到更新的脚本,以防其他人需要。 谢谢! :)

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