How to parse netstat command in order to get process name and PID from it?

后端 未结 7 2260
不思量自难忘°
不思量自难忘° 2020-12-24 02:26

I\'m trying to determine what application is using certain port and get netstat -tlnp | grep for Linux.

This command return the following o

相关标签:
7条回答
  • 2020-12-24 03:03

    (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 -i tcp:80
    

    To show only the process name and PID, parse the output using:

    lsof | tail -n +2 | awk '{print $1 " " $2}'
    

    The tail command skips the output header while awk prints out the required columns.

    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.

    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.

    0 讨论(0)
提交回复
热议问题