meesteridle

block country iptables bash script

Jun 19th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.49 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. PATH=/bin:/usr/bin:/usr/local/bin:/sbin
  4. CURDATE=`date +%s`
  5. CURL=/usr/bin/curl
  6. ISOLIST=(${*})
  7. OLDDATE=`expr ${CURDATE} - 86400`
  8. URL=https://ip.ludost.net/raw/country.db.gz
  9.  
  10. # Set modification date if database is around
  11. if [ -f ./country.db ]; then
  12.     MODDATE=`stat -c %Y ./country.db`
  13. fi
  14.  
  15. # Only root can run this script
  16. if [[ $EUID -ne 0 ]]; then
  17.     echo "This script must be run as root" 1>&2
  18.     exit 1
  19. fi
  20.  
  21. # Check for curl
  22. if [ ! -x ${CURL} ]; then
  23.     printf "\n \033[1mcurl not found\033[0m\n \033[1mplease install curl\033[0m\n\n"
  24.     exit 1
  25. fi
  26.  
  27. # If database does not exist, Print usage and download database
  28. if [ ! -f ./country.db ]; then
  29.     printf "\n \033[1mUsage:\t\t./mine.sh <space delimited ISO codes>\033[0m\n"
  30.     printf " \t\t\033[1m ** country ISO codes are two characters **\033[0m\n"
  31.     printf " \033[1mExample:\t./mine.sh cn [ru] [gb] [eu]\033[0m\n"
  32.     printf "\n ** Database not found."
  33.     printf "\n ** A new copy of country.db downloading...\n\n"
  34.     curl -O $URL && gunzip -f ${URL##*/}
  35.     printf "\n"
  36.     exit 1
  37.  
  38. # If database exists, but it is older than one day, Print usage and download fresh database
  39. elif [ -f ./country.db ] && [ ${MODDATE} -lt ${OLDDATE} ]; then
  40.     printf "\n \033[1mUsage:\t\t./mine.sh <space delimited ISO codes>\033[0m\n"
  41.     printf " \t\t\033[1m ** country ISO codes are two characters **\033[0m\n"
  42.     printf " \033[1mExample:\t./mine.sh cn [ru] [gb] [eu]\033[0m\n"
  43.     printf "\n ** Database is older than one day."
  44.     printf "\n ** A fresh copy of country.db downloading...\n\n"
  45.     curl -O $URL && gunzip -f ${URL##*/}
  46.     printf "\n"
  47.     exit 1
  48.  
  49. # If country code input is empty, the database exists, and database is not old, Print usage
  50. elif [ -z "${ISOLIST}" ] && [ -f ./country.db ] && [ ${MODDATE} -gt ${OLDDATE} ]; then
  51.     printf "\n \033[1mUsage:\t\t./mine.sh <space delimited ISO codes>\033[0m\n"
  52.     printf " \t\t\033[1m ** country ISO codes are two characters **\033[0m\n"
  53.     printf " \033[1mExample:\t./mine.sh cn [ru] [gb] [eu]\033[0m\n\n"
  54.     exit 0
  55.  
  56. # If script makes it to here, new-ish database exists and the country code has been entered
  57. # Git-R-Done
  58. else
  59.     for i in ${ISOLIST[*]}
  60.     do
  61.         IP+=($(grep -i $i ./country.db | awk '{print $1"-"$2}'))
  62.     done
  63.     # Wait message, rules applied
  64.     printf "\n \033[1mOne moment...\033[0m\n\n"
  65.     for r in ${IP[*]}
  66.     do
  67.         ## Print actions on console
  68.         ##printf "iptables -A INPUT -m iprange --src-range $r -j DROP\n"
  69.         # Uncomment line below to really commit
  70.         iptables -A INPUT -m iprange --src-range $r -j DROP
  71.     done
  72.  
  73. fi
Advertisement
Add Comment
Please, Sign In to add comment