I'm trying to determine what application is using certain port and get netstat -tlnp | grep <port> for Linux
.
我正在尝试确定哪个应用程序正在使用特定的端口,并为Linux获取netstat -tlnp | grep
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 I的结果。java 3591 e。
What is best way to do it?
最好的方法是什么?
Thank you.
谢谢你!
7 个解决方案
#1
27
Try
试一试
ps -p $(lsof -ti tcp:80) o comm=,pid=
or
或
netstat -tlnp | awk '/:80 */ {split($NF,a,"/"); print a[2],a[1]}'
#2
14
(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 $1 " " $2}'
The tail
command skips the output header while awk
prints out the required columns.
尾命令跳过输出头,而awk打印出所需的列。
Why 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.
尝试获取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 manpage for more examples.
lsof为您省去了匹配正确端口的麻烦,并提供了许多其他用途,例如,我们现在正在执行的操作的逆(查找进程正在使用哪些端口),或者确定哪个进程正在使用文件/挂载点(或反向)。有关更多示例,请参见lsof manpage。
#3
2
awk + sed:
awk +对话:
awk '{print $7}' | sed "s/\// /g"
#4
1
... | awk '{print $7;}'| sed 's/\// /g'
…| awk '{$7;}'| sed 's/\// /g'
#5
1
Also you can get rid of that "you have to be root" message by redirecting stderr to /dev/null
您还可以通过将stderr重定向到/dev/null来消除“您必须是根”消息
netstat -tlnp 2>/dev/null | grep ...
#6
0
netstat -tlnp 2>/dev/null | awk '/\.[0-9]:X/ {print $7}' | sed 's/\//\s/g'
where X in the awk bit is the port you're looking at.
在awk位中的X是你所看到的端口。
#7
0
Try netstat -p
尝试netstat - p
-p, --program Show the PID and name of the program to which each socket belongs.
程序显示每个套接字所属的程序的PID和名称。
#1
27
Try
试一试
ps -p $(lsof -ti tcp:80) o comm=,pid=
or
或
netstat -tlnp | awk '/:80 */ {split($NF,a,"/"); print a[2],a[1]}'
#2
14
(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 $1 " " $2}'
The tail
command skips the output header while awk
prints out the required columns.
尾命令跳过输出头,而awk打印出所需的列。
Why 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.
尝试获取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 manpage for more examples.
lsof为您省去了匹配正确端口的麻烦,并提供了许多其他用途,例如,我们现在正在执行的操作的逆(查找进程正在使用哪些端口),或者确定哪个进程正在使用文件/挂载点(或反向)。有关更多示例,请参见lsof manpage。
#3
2
awk + sed:
awk +对话:
awk '{print $7}' | sed "s/\// /g"
#4
1
... | awk '{print $7;}'| sed 's/\// /g'
…| awk '{$7;}'| sed 's/\// /g'
#5
1
Also you can get rid of that "you have to be root" message by redirecting stderr to /dev/null
您还可以通过将stderr重定向到/dev/null来消除“您必须是根”消息
netstat -tlnp 2>/dev/null | grep ...
#6
0
netstat -tlnp 2>/dev/null | awk '/\.[0-9]:X/ {print $7}' | sed 's/\//\s/g'
where X in the awk bit is the port you're looking at.
在awk位中的X是你所看到的端口。
#7
0
Try netstat -p
尝试netstat - p
-p, --program Show the PID and name of the program to which each socket belongs.
程序显示每个套接字所属的程序的PID和名称。