Skip to content

Instantly share code, notes, and snippets.

@m0rb
Last active September 5, 2021 21:00
Show Gist options
  • Save m0rb/8e8dd731802f54b99eea044a105903ec to your computer and use it in GitHub Desktop.
Save m0rb/8e8dd731802f54b99eea044a105903ec to your computer and use it in GitHub Desktop.
#!/bin/sh
# countryblock.sh -- yet another country blackholing script by morb
# psst, hey kid! wanna blackhole countries with the flick of a wrist?
CC=$1;[ -z $CC ]&&printf "Usage: $0 <country code>\nex:\n$0 cn #China\n"&&exit 1
[ "$USER" != "root" ]&&printf "root required.\n"&&exit 1
ZONE="http://www.ipdeny.com/ipblocks/data/countries/${CC}.zone"
curl -s $ZONE | xargs -I, ip route add blackhole , metric 999
@thedunston
Copy link

thedunston commented Sep 4, 2021

I added the if statement to take the option "list" and it will print all available codes from the commandline so you don't have to visit the site. The SSL cert expired so used -k.

if [[ "${CC}" == "list" ]];
then
	curl -k -s https://www.ipdeny.com/ipblocks/ | egrep -o '[A-Z].*\([A-Z]{2}\)\ ' \
| sed 's/([A-Z]\{2\})/:&/g' | column -t -s : | more

fi

full code:

#!/bin/sh
# countryblock.sh -- yet another country blackholing script by morb
# psst, hey kid! wanna blackhole countries with the flick of a wrist?
CC=$1
if [[ "${CC}" == "list" ]];then
	curl -k -s https://www.ipdeny.com/ipblocks/ | egrep -o '[A-Z].*\([A-Z]{2}\)\ ' \
| sed 's/([A-Z]\{2\})/:&/g' | column -t -s : | more
exit 0
fi
[ -z $CC ]&&printf "Usage: $0 <country code>\nex:\n$0 cn #China\n"&&exit 1
[ "$USER" != "root" ]&&printf "root required.\n"&&exit 1
ZONE="http://www.ipdeny.com/ipblocks/data/countries/${CC}.zone"
curl -s $ZONE | xargs -I, ip route add blackhole , metric 999

@m0rb
Copy link
Author

m0rb commented Sep 5, 2021

I added the if statement to take the option "list" and it will print all available codes from the commandline so you don't have to visit the site. The SSL cert expired so used -k.

cool

check this out

$ awk '{FS="\t";print $1, $5}' /usr/share/libtimezonemap/countryInfo.txt
$ awk '{FS="\t";print $1, $3}' /usr/share/zoneinfo/zone.tab | sed '/^#/d'

@thedunston
Copy link

That would work too, but there are some county codes (14) in the zone.tab that are not on the ipblocks website.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment