Skip to content

Instantly share code, notes, and snippets.

@nexus166
Created December 3, 2018 17:04
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 nexus166/3cd408ea95c2eae32f33530462f880b5 to your computer and use it in GitHub Desktop.
Save nexus166/3cd408ea95c2eae32f33530462f880b5 to your computer and use it in GitHub Desktop.
#!/bin/sh
COUNTRY="$(echo "$1" | tr '[:upper:]' '[:lower:]')"
if [ -z "$COUNTRY" ]; then printf "\\n\\tNO COUNTRY CODE SPECIFIED\\n\\n" && exit 1; fi
IPSET_SET="${COUNTRY}-subnets"
IPSET="$(command -v ipset || exit 1)"
printf "\\n\\tPREPARING IPSET SET\\t\\t\\t[%s]" "$IPSET_SET" && \
"$IPSET" flush "$IPSET_SET" || "$IPSET" create "$IPSET_SET" hash:net && \
printf "\\n\\tDOWNLOADING AND IMPORTING SUBNETS.." && \
curl -s -o- "http://www.ipdeny.com/ipblocks/data/countries/$COUNTRY.zone" | \
grep -Eo '[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+/[0-9]+' | \
while read -r IP; do "$IPSET" add "$IPSET_SET" "$IP"; done && printf "\\tOK"
printf "\\n\\tTOTAL SUBNETS IN SET %s:\\t%s\\n\\n" "$IPSET_SET" "$(echo $(($($IPSET list "$IPSET_SET" | wc -l) - 8)))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment