Skip to content

Instantly share code, notes, and snippets.

@AbhishekGhosh
Created November 20, 2016 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhishekGhosh/a66c125af731ad3a8397f77d8f447dda to your computer and use it in GitHub Desktop.
Save AbhishekGhosh/a66c125af731ad3a8397f77d8f447dda to your computer and use it in GitHub Desktop.
Bash Script For SSH Country Based Deny
#!/bin/bash
# country codes to ACCEPT
ALLOW_COUNTRIES="US UK FR"
if [ $# -ne 1 ]; then
echo "Usage: `basename $0` <ip>" 1>&2
exit 0 # return true in case of config issue
fi
COUNTRY=`/usr/bin/geoiplookup $1 | awk -F ": " '{ print $2 }' | awk -F "," '{ print $1 }' | head -n 1`
[[ $COUNTRY = "IP Address not found" || $ALLOW_COUNTRIES =~ $COUNTRY ]] && RESPONSE="ALLOW" || RESPONSE="DENY"
if [ $RESPONSE = "ALLOW" ]
then
exit 0
else
logger "$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