-
Notifications
You must be signed in to change notification settings - Fork 2
/
listeners.sh
executable file
·61 lines (54 loc) · 1.29 KB
/
listeners.sh
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# (c) 20170707 Leif Sawyer
# License: GPL 3.0 (see https://github.com/akhepcat/)
# Permanent home: https://github.com/akhepcat/Miscellaneous/
# Direct download: https://raw.githubusercontent.com/akhepcat/Miscellaneous/master/listeners.sh
#
# replicates:
# lsof -i -n -P | grep LISTEN | sort -n -k +2 -t: | grep -vE '127.0.0.1|::1' | uniq -f 7
# without multiple execs
if [ -n "$1" -a "${1}" = "-l" ]
then
FILTER="all"
else
FILTER="local"
fi
GAWK=$(which gawk)
AWK=$(which awk)
USE_AWK=${GAWK:-$AWK}
lsof -i -n -P +c15 | ${USE_AWK} -v filter=${FILTER} '
# LSOF replaces spaces in the procname with \x20, which we CAN remove, else formatting
# $0 ~ /\\x20/ {
# gsub("\\\\x20"," ",$0)
# }
# Look for global listeners (ignore localhost if flagged)
/LISTEN/ {
if ( (filter ~ /local/) && (/127.0.0.1/ || /::1/) ) {
next;
}
myline=$0;
if ( filter ~ /all/ ) {
gsub("127.0.0.1","",myline);
gsub("::1","",myline);
}
FS=":";
split($2,portArr," ");
port=portArr[1];
if (!line[port]) {
line[port]=myline;
};
};
# Sort the command-lines by the listening port
END {
j=1;
for ( i in line) {
ports[j] = i;
j++;
};
n = asort(ports,null,"@ind_num_asc")
for (i = 1; i <= n; i++) {
print line[null[i]];
};
};
'
# end