-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportsearch
executable file
·40 lines (33 loc) · 923 Bytes
/
portsearch
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
#!/usr/bin/env bash
# desc: search process who own the port
# netstat -apn | grep $1, but is not be strict
source "$HOME/CodeHub/SHELL/libs/bootstrap.sh"
HELP_MESSAGE="
descript:
search process who own the port with 'lsof ...' or 'netstat ...'
usage:
portsearch option {port}
options:
--help,-h show the help
"
if [[ -z "$1" ]] ;then
sudo netstat -tlnp
exit 0
fi
if [[ -z "$1" || "$1" = "--help" || "$1" = "-h" ]]; then
helpShow
fi
# MSG=`sudo netstat -tlnp | egrep \(:$1\\\s+\|State\)`
MSG=`sudo netstat -tlna | egrep .$1` # Mac
COUNT=`echo "$MSG" | wc -l`
if [[ $COUNT -gt 0 ]]
then
# echo "======================== EXEC netstat =========================================================="
echo "$MSG"
fi
if MSG=`sudo lsof -P -i:$1`
then
echo
# echo "======================== EXEC lsof ============================================================="
echo "$MSG"
fi