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

后端 未结 7 2259
不思量自难忘°
不思量自难忘° 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 02:42

    ... | awk '{print $7;}'| sed 's/\// /g'

    0 讨论(0)
  • 2020-12-24 02:53

    awk + sed:

    awk '{print $7}' | sed "s/\// /g"
    
    0 讨论(0)
  • 2020-12-24 02:54
    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.

    0 讨论(0)
  • 2020-12-24 02:55

    Try netstat -p

    -p, --program Show the PID and name of the program to which each socket belongs.

    0 讨论(0)
  • 2020-12-24 02:57

    Also you can get rid of that "you have to be root" message by redirecting stderr to /dev/null

    netstat -tlnp 2>/dev/null | grep ...
    
    0 讨论(0)
  • 2020-12-24 02:58

    Try

    ps -p $(lsof -ti tcp:80) o comm=,pid=
    

    or

    netstat -tlnp | awk '/:80 */ {split($NF,a,"/"); print a[2],a[1]}'
    
    0 讨论(0)
提交回复
热议问题