-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-tcp-connection.sh
70 lines (60 loc) · 1.76 KB
/
count-tcp-connection.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
62
63
64
65
66
67
68
69
70
#!/bin/bash
# @Function
# show count of tcp connection.
#
# @Usage
# $ ./count-tcp-connection
set -eEuo pipefail
# NOTE: DO NOT declare var PROG as readonly in ONE line!
PROG="$(basename "$0")"
readonly PROG
readonly PROG_VERSION='2.5.0-dev'
################################################################################
# util functions
################################################################################
usage() {
cat <<EOF
Usage: ${PROG} [OPTION]...
show count of tcp connection
Example:
${PROG}
Options:
-h, --help display this help and exit
-V, --version display version information and exit
EOF
exit
}
progVersion() {
echo "$PROG $PROG_VERSION"
exit
}
################################################################################
# parse options
################################################################################
for a; do
[[ "-h" == "$a" || "--help" == "$a" ]] && usage
done
for a; do
[[ "-V" == "$a" || "--version" == "$a" ]] && progVersion
done
################################################################################
# biz logic
################################################################################
# On MacOS, netstat need to using -p tcp to get only tcp output.
uname | grep Darwin -q && option_for_mac="-ptcp"
# @todo: check compatibility for Monterey+
netstat -tna ${option_for_mac:-} | awk 'NR > 2 {
++s[$NF]
}
END {
# get max length of stat and count
for(v in s) {
stat_len = length(v)
if(stat_len > max_stat_len) max_stat_len = stat_len
count_len = length(s[v])
if (count_len > max_count_len) max_count_len = count_len
}
for(v in s) {
printf "%-" max_stat_len "s %" max_count_len "s\n", v, s[v]
}
}' | sort -nr -k2,2