Skip to content

Commit 90c9945

Browse files
committed
List ports
1 parent f977c00 commit 90c9945

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

port

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,34 @@
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+
513
if [[ -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
833
fi
934

1035
port=$1

0 commit comments

Comments
 (0)