Skip to content

Instantly share code, notes, and snippets.

@stanfrbd
Created June 13, 2024 09:58
Show Gist options
  • Save stanfrbd/42c1c0eb45fda5d0ab27735865fc9c4e to your computer and use it in GitHub Desktop.
Save stanfrbd/42c1c0eb45fda5d0ab27735865fc9c4e to your computer and use it in GitHub Desktop.
sshfilter.sh using ipinfo API
#!/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