Linux 如何解析 netstat 命令以从中获取进程名称和 PID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4247932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse netstat command in order to get process name and PID from it?
提问by yart
I'm trying to determine what application is using certain port and get netstat -tlnp | grep <port> for Linux
.
我正在尝试确定哪个应用程序正在使用特定端口并获取netstat -tlnp | grep <port> for Linux
.
This command return the following output:
此命令返回以下输出:
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:<port> 0.0.0.0:* LISTEN 3591/java
I need to get in result only name of the process and PID, i.e java 3591.
我只需要获得进程名称和PID,即java 3591。
What is best way to do it?
什么是最好的方法?
Thank you.
谢谢你。
采纳答案by Paused until further notice.
Try
尝试
ps -p $(lsof -ti tcp:80) o comm=,pid=
or
或者
netstat -tlnp | awk '/:80 */ {split($NF,a,"/"); print a[2],a[1]}'
回答by shuvalov
awk + sed:
awk + sed:
awk '{print }' | sed "s/\// /g"
回答by khachik
... | awk '{print $7;}'| sed 's/\// /g'
... | awk '{print $7;}'| sed 's/\// /g'
回答by Jacek Prucia
Also you can get rid of that "you have to be root" message by redirecting stderr to /dev/null
你也可以通过将 stderr 重定向到 /dev/null 来摆脱“你必须是 root”的消息
netstat -tlnp 2>/dev/null | grep ...
回答by Wesley Rice
netstat -tlnp 2>/dev/null | awk '/\.[0-9]:X/ {print }' | sed 's/\//\s/g'
where X in the awk bit is the port you're looking at.
其中 awk 位中的 X 是您正在查看的端口。
回答by Shawn Chin
(Detracting slightly from your original question), to find out which process listens to a certain port number, I usually use the lsof
command. For example:
(稍微偏离您原来的问题),要找出哪个进程侦听某个端口号,我通常使用该lsof
命令。例如:
lsof -i tcp:80
To show only the process name and PID, parse the output using:
要仅显示进程名称和 PID,请使用以下命令解析输出:
lsof | tail -n +2 | awk '{print " " }'
The tail
command skips the output header while awk
prints out the required columns.
该tail
命令在awk
打印出所需的列时跳过输出标题。
Why lsof
为什么 lsof
Trying to grep
the output of netstat
can be messy as you'll need to make sure you're matching against the correct column. A robust solution can be rather long winded and difficult (for me anyway) to produce on-demand.
尝试grep
输出netstat
可能会很麻烦,因为您需要确保与正确的列匹配。一个强大的解决方案可能相当冗长且难以(无论如何对我来说)按需生产。
lsof
saves you the trouble of matching the right ports, and has lots of other uses too, e.g. the inverse of what we're doing now (find out which ports are being used by a process), or determining which process is using a file / mount point (or the inverse). See lsof manpagefor more examples.
lsof
省去了匹配正确端口的麻烦,并且还有很多其他用途,例如与我们现在所做的相反(找出进程正在使用哪些端口),或者确定哪个进程正在使用文件 /挂载点(或相反)。有关更多示例,请参见lsof 联机帮助页。
回答by ZRJ
Try netstat -p
尝试 netstat -p
-p, --program Show the PID and name of the program to which each socket belongs.
-p, --program 显示每个套接字所属程序的PID和名称。