linux shell 找端口号及对应的进程

#!/bin/bash
#author:zhongyulin
#crteate-time:2016-10-20

netstat -lnpt|grep -v grep>/tmp/script/netstat_status.txt
sed -i '1,2d' /tmp/script/netstat_status.txt
cat /tmp/script/netstat_status.txt|awk -F " " '{print $4 " " $7}'>/tmp/script/port_pid.txt
echo "port , process_id , cwd_location , exe_location"
while read line
do
  # echo $line
  port=`echo $line |awk -F " " '{print $1}'|awk -F ":" '{print $NF}'`
  server_pid=`echo $line |awk -F " " '{print $2}'|awk -F "/" '{print $1}'`
  cwd=`ls -la /proc/$server_pid|grep "cwd ->"|awk '{print $NF}'`
  exe_location=`ls -la /proc/$server_pid|grep "exe ->"|grep -v grep|awk '{print $NF}'`
  echo $port , $server_pid , $cwd , $exe_location
  #p=`echo $port,$server_pid,$cwd,$exe_location|awk -F "," '{print $4}'|awk -F "/" '{print $NF}'|grep java|wc -l`
  #if [ $p = '1' ];then
      # echo $port,$server_pid,$cwd
  #else
      # echo $port,$server_pid,$exe_location
  #fi
done</tmp/script/port_pid.txt

原文地址:https://www.cnblogs.com/sfnz/p/5980915.html