Created
June 13, 2024 09:58
-
-
Save stanfrbd/42c1c0eb45fda5d0ab27735865fc9c4e to your computer and use it in GitHub Desktop.
sshfilter.sh using ipinfo API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# UPPERCASE space-separated country codes to ACCEPT | |
ALLOW_COUNTRIES="FR CH DE US" | |
LOGDENY_FACILITY="authpriv.notice" | |
TOKEN="token from ipinfo" | |
if [ $# -ne 1 ]; then | |
echo "Usage: `basename $0` <ip>" 1>&2 | |
exit 0 # return true in case of config issue | |
fi | |
COUNTRY=$(curl -s "https://ipinfo.io/$1" -H "Authorization: Bearer $TOKEN" | jq -r '. | "\(.country)"') | |
[[ $COUNTRY = "null" || $ALLOW_COUNTRIES =~ $COUNTRY ]] && RESPONSE="ALLOW" || RESPONSE="DENY" | |
if [[ "$RESPONSE" == "ALLOW" ]] ; then | |
logger -p $LOGDENY_FACILITY "$RESPONSE sshd connection from $1 ($COUNTRY)" | |
exit 0 | |
else | |
logger -p $LOGDENY_FACILITY "$RESPONSE sshd connection from $1 ($COUNTRY)" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment