Commit 4011f075 authored by Michael Völske's avatar Michael Völske

add counting active ssh keys

parent 2ed1c527
#!/bin/bash
# Collect all SSH keys in use across salt minions' user accounts
#
# Copyright 2019-today
#
# Project WEBIS
# Author: Michael Völske
scriptPath=${0%/*}
. "$scriptPath"/../../libs/bashhelper.sh
. "$scriptPath"/../../libs/shflags
check_tools "ssh" "salt"
#
# Define usage screen.
#
usage() {
echo "usage:
$(basename "$0")
description:
Collect all SSH keys occurring in .ssh/authorized_keys files across all reachable salt minions.
Must be run from a salt master.
"
}
#
# Define command line arguments and parse them.
#
FLAGS_HELP=$(usage)
export FLAGS_HELP
FLAGS "$@" || exit 1 # Parse command line arguments.
eval set -- "${FLAGS_ARGV}"
#
# Main
#
main() {
NUM_DEAD=0
declare -A KPH
salt '*' cmd.run 'find /root /home -path "*/.ssh/authorized_keys" -maxdepth 4 -exec cat \{\} \;' --output=txt \
| sort -t: -k2 -k1V | while IFS=":" read -r -a KEYHOST ; do
K=${KEYHOST[1]}
H=${KEYHOST[0]}
if [[ $H =~ "Minion did not return" ]]; then
NUM_DEAD=$(( NUM_DEAD + 1))
elif [[ $K != $LASTKEY && $K != "" ]]; then
echo Key: $K
echo Used on hosts:
KEYCOUNT=$(( $KEYCOUNT + 1 ))
LASTHOST=""
fi
if [[ $K != "" && $H != $LASTHOST ]]; then
echo " - $H"
LASTHOST=$H
${KPH[$H]}=$(( ${KPH[$H]} + 1 ))
fi
LASTKEY=$K
done
echo TOTAL UNIQUE KEYS: $KEYCOUNT; $NUM_DEAD hosts offline; ${#KPH[@]} hosts online.
}
#
# Start programm with parameters.
#
main "$@"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment