File tree Expand file tree Collapse file tree 1 file changed +27
-2
lines changed
Expand file tree Collapse file tree 1 file changed +27
-2
lines changed Original file line number Diff line number Diff line change 22
33# Show process info for what's listening on a given port
44
5+ if [[ $1 == --help ]]; then
6+ echo " Usage: port [<port>]"
7+ echo " "
8+ echo " With no arguments, lists all used ports."
9+ echo " With a port number, shows the process listening on that port."
10+ exit 0
11+ fi
12+
513if [[ -z $1 ]]; then
6- echo " Usage: port-ps <port>" >&2
7- exit 1
14+ pairs=$( lsof -nP -iTCP -sTCP:LISTEN 2> /dev/null | awk ' NR>1 {
15+ n = split($(NF-1), a, ":")
16+ print a[n], $2
17+ }' | sort -u -k1,1n)
18+ if [[ -z $pairs ]]; then
19+ echo " No ports in use"
20+ exit 0
21+ fi
22+ first=true
23+ while IFS=' ' read -r port pid; do
24+ if $first ; then
25+ printf " %-7s " " PORT"
26+ ps -p " $pid " -o pid,user,command 2> /dev/null | head -1
27+ first=false
28+ fi
29+ info=$( ps -p " $pid " -o pid=,user=,command= 2> /dev/null)
30+ [[ -n $info ]] && printf " %-7s %s\n" " $port " " $info "
31+ done <<< " $pairs"
32+ exit 0
833fi
934
1035port=$1
You can’t perform that action at this time.
0 commit comments