Geo-Block with ufw in Ubuntu 14.04

Quickly and Easily block ip addresses in bulk using ufw.
This is helpful to block heavy spam countries.

#!/bin/bash
if [ -f $1 ]; then
  while read line; do sudo ufw deny from $line; done < $1
fi
#!/bin/bash
if [ -f $1 ]; then
  while read line; do sudo ufw delete deny from $line; done < $1
fi

create a text file with CIDR format ip addresses

114.134.184.0/21
1.80.0.0/13
1.92.0.0/14
1.192.0.0/13
...

I recommend the list from wizcrafts.net or ip2location.com

run ./ufwblock.sh china.txt to block all ip adresses from file
run ./ufwunblock.sh china.txt to unblock

Leave a Reply

Your email address will not be published. Required fields are marked *