aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoni <matzeton@googlemail.com>2017-02-12 18:30:50 +0100
committertoni <matzeton@googlemail.com>2017-02-12 18:30:50 +0100
commit063e35d082ded57a2644cf4c54140fdfabe5ff7d (patch)
treee9d1b6098527e3dacdf07f6bf202fabfc5d52cd9
parent4437f782fad4e961f779c25b2fa683fbc092bdd0 (diff)
check ssh geoip
-rwxr-xr-xcheck_ssh_geoip.sh60
1 files changed, 60 insertions, 0 deletions
diff --git a/check_ssh_geoip.sh b/check_ssh_geoip.sh
new file mode 100755
index 0000000..05a77ac
--- /dev/null
+++ b/check_ssh_geoip.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+if [ x"${BASH_VERSINFO}" = x ]; then
+ echo "BASH_VERSINFO not found. Is \`/bin/bash\` a valid bash interpreter?"
+ exit 1
+fi
+if [ "${BASH_VERSINFO}" -lt 4 ]; then
+ echo "Bash version >= 4 required for declaring/using arrays/dicts."
+ exit 1
+fi
+
+export DESTHOST="host.lan"
+export DESTUSER="someuser"
+
+if [ x"$1" != x ]; then
+ DEST="$1"
+else
+ DEST="${DESTUSER}@${DESTHOST}"
+fi
+
+if [ x"$2" != x ]; then
+ LOGCMD="$2"
+else
+ LOGCMD="logread"
+fi
+
+if [ x"$3" != x ]; then
+ LOGREP="$3"
+else
+ LOGREP="dropbear"
+fi
+
+LINE="-------------------------"
+IFS=' ';
+OUT=$(ssh -o LogLevel=Error ${DEST} ${LOGCMD} | \
+ sed -ne 's/.*'"${LOGREP}"'.*\s\+\([0-9\.]\+\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/p' | \
+ uniq | tr '\n' ' ')
+declare -A ORIGINS
+for host in ${OUT}; do \
+ echo "${host}" | grep -qoE '(192.168.|10.10.|172.)'
+ [ $? -eq 0 ] && continue
+ echo "${LINE}"
+ echo "HOST: ${host}"
+ GEOIP=$(geoiplookup "${host}")
+ if [ x"${ORIGINS["${GEOIP}"]}" != x ]; then
+ ORIGINS["${GEOIP}"]=$(expr ${ORIGINS["${GEOIP}"]} + 1)
+ else
+ ORIGINS["${GEOIP}"]=1
+ fi
+ echo "${GEOIP}"
+done
+
+echo "${LINE}"
+echo -e "${LINE}\nEOF"
+echo "${LINE}"
+
+echo "COUNTRY STATS"
+for origin in "${!ORIGINS[@]}"; do
+ echo "${origin} -> ${ORIGINS["${origin}"]}"
+done