-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport
More file actions
executable file
·45 lines (39 loc) · 1.08 KB
/
port
File metadata and controls
executable file
·45 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
# Show process info for what's listening on a given port
if [[ $1 == --help ]]; then
echo "Usage: port [<port>]"
echo ""
echo "With no arguments, lists all used ports."
echo "With a port number, shows the process listening on that port."
exit 0
fi
if [[ -z $1 ]]; then
pairs=$(lsof -nP -iTCP -sTCP:LISTEN 2>/dev/null | awk 'NR>1 {
n = split($(NF-1), a, ":")
print a[n], $2
}' | sort -u -k1,1n)
if [[ -z $pairs ]]; then
echo "No ports in use"
exit 0
fi
first=true
while IFS=' ' read -r port pid; do
if $first; then
printf "%-7s " "PORT"
ps -p "$pid" -o pid,user,command 2>/dev/null | head -1
first=false
fi
info=$(ps -p "$pid" -o pid=,user=,command= 2>/dev/null)
[[ -n $info ]] && printf "%-7s %s\n" "$port" "$info"
done <<< "$pairs"
exit 0
fi
port=$1
pids=$(lsof -n -ti ":$port" 2>/dev/null)
if [[ -z $pids ]]; then
echo "Nothing listening on port $port"
exit 0
fi
for pid in $pids; do
ps -p "$pid" -o pid,user,command
done