-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh-list-active-connections.bash
executable file
·69 lines (49 loc) · 1.49 KB
/
ssh-list-active-connections.bash
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
#!/bin/bash
function out_line() {
local text="${1}"
local type="${2:-info}"
local newline="${3:-true}"
printf '%s %s [%s] %s' "$(basename "${BASH_SOURCE}" .bash)" "$(date +%y%m%d%H%M.%N)" "${type^^}" "${text}"
if [[ "${newline}" == "true" ]]; then
printf "\n"
fi
}
function out_crit() {
local text="${1}"
local stop="${2:-}"
out_line "${text}" CRIT
if [[ "${stop}" -gt 0 ]]; then
exit ${stop}
fi
}
function out_info() {
local text="${1}"
local newline="${2:-true}"
out_line "${text}" info "${newline}"
}
function out_record() {
local text="${1}"
local i="${2}"
out_line "${text}" "$(printf '%04d' ${i})"
}
function main() {
# if [[ ${EUID} -ne 0 ]]; then
# out_crit "Cannot fetch connections without elevated privileges. Use sudo or su to root user..." 1
# fi
out_info "Filtering all active connections (this could take some time) ... " "false"
local records="$(sudo netstat -atp 2> /dev/null | grep -E 'ESTABLISHED [0-9]+/sshd:' | awk '{printf "hostport=[%s] gateway=[%s] user=[%s]\n", $4, $5, $8}')"
if [[ $? -ne 0 ]]; then
printf "ERROR\n"
out_crit "Failed to fetch connections using netstat!" 255
fi
printf "OKAY\n"
local recordSize=$(echo ${records} | wc -l 2> /dev/null)
if [[ ! ${recordSize} -gt 0 ]]; then
out_crit "Found no active ssh connections." 254
fi
out_info "$(printf 'Listing %d active sshd connection records:' ${recordSize})"
while IFS= read -r r; do
out_record "${r}"
done <<< "${records}"
}
main