Created
April 15, 2019 11:35
-
-
Save eladg/554c6517f3a8abf517e0ede9e16c507b to your computer and use it in GitHub Desktop.
Openport2pid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Original at: http://www.brandonhutchinson.com/Port_to_PID_with_lsof.html | |
# was written originaly for Solaris 9. Fixed By Elad Gariany to OSX | |
# Map LISTENing TCP ports to their PIDs using lsof | |
#LSOF=lsof | |
# e.g. netstat -an | |
# 127.0.0.1.25 *.* 0 0 49152 0 LISTEN | |
# *.22 *.* 0 0 49152 0 LISTEN | |
# e.g. lsof -i | |
# sshd 5097 root 5u IPv4 0x30863fb1b58 0t0 TCP *:ssh (LISTEN) | |
printf "%-6s %-10s %-6s %-8s\n" "Port" "Command" "PID" "User" | |
printf "%-6s %-10s %-6s %-8s\n" "----" "-------" "---" "----" | |
for PORT in `netstat -an | grep LISTEN | \ | |
perl -ne 'print "$1\n" if /.*\.(\d+)\s+\*\.\*/' | sort -n | uniq` | |
do | |
lsof -i :${PORT} | grep LISTEN | tail -1 | while read line | |
do | |
set $line | |
COMMAND=$1 | |
PID=$2 | |
LSOF_USER=$3 | |
printf "%-6d %-10s %-6d %-8s\n" "$PORT" "$COMMAND" "$PID" "$LSOF_USER" | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment