我想合并以下命令的输出:
-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
-聚苯乙烯
[root]# ps -eo pid,cmd | grep 151634
130485 grep --color=auto 151634
151634 java -classpath
我想合并以下两个命令并具有以下输出(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 results
非常感谢你!
LE:为了避免混淆,我删除了输出html。这只是一个关于它应该是什么样子的例子。
最佳答案
非常感谢朱什朱什,
我在这里写了我的回复,因为评论回复太长了。
我也用它更新了脚本,并且它工作了,但是,脚本似乎没有获得正确的输出。
我将试着在下面解释
如果我有以下netstat-nltp输出tcp 0 0 127.0.0.1:32552 0.0.0.0:* LISTEN 151634/javatcp 0 0 10.77.66.33:8081 0.0.0.0:* LISTEN 151634/javatcp 0 0 0.0.0.0:7070 0.0.0.0:* LISTEN 151634/javatcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 151634/javatcp 0 0 0.0.0.0:20100 0.0.0.0:* LISTEN 151634/javatcp 0 0 0.0.0.0:20101 0.0.0.0:* LISTEN 151634/javatcp 0 0 0.0.0.0:20102 0.0.0.0:* LISTEN 151634/java
PID=151634的命令是java -classpath /opt/application/nifi-1.6.0/./conf:/opt/application/nifi-1.6.0/./lib/jetty-schemas-3.1.jar:
脚本提供以下输出:PORT PID CMD32552 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 CMD32552 151634 CMD from above8081 151634 CMD from above7070 151634 CMD from above9090 151634 CMD from above20100 151634 CMD from above20101 151634 CMD from above20102 151634 CMD from above
或者,另一个例子:
如果我有以下netstat-nltp输出tcp 0 0 127.0.0.1:3030 0.0.0.0:* LISTEN 88284/rubytcp 0 0 127.0.0.1:3031 0.0.0.0:* LISTEN 88284/ruby
PID=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 CMD3030 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 warn3030 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 CMD3030 88284 CMD from above3031 88284 CMD from above
非常感谢您的帮助和耐心!
关于linux - 结合Netstat和PS的输出-Linux,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51669518/