Skip to content

Instantly share code, notes, and snippets.

@FlorinAsavoaie
Created June 29, 2015 12:47
Show Gist options
  • Save FlorinAsavoaie/2d61ec3aaded50465a32 to your computer and use it in GitHub Desktop.
Save FlorinAsavoaie/2d61ec3aaded50465a32 to your computer and use it in GitHub Desktop.
Bash script to update CloudFlare IPs in an ipset
#!/bin/bash
IPSET="/usr/sbin/ipset"
CURL="/usr/bin/curl"
DATE="/bin/date"
for VER in 4 6; do
URL="https://www.cloudflare.com/ips-v$VER"
SET_NAME="CloudFlare.IPv$VER"
TMP_NAME="$SET_NAME.$($DATE +%s)"
IP_LIST="$($CURL -s $URL)"
if [ ! $? ]; then
echo "Unable to download IPv$VER IPs list"
exit 1
fi
$IPSET create $TMP_NAME hash:net family "inet${VER/4/}"
for IP in $IP_LIST; do
$IPSET add $TMP_NAME $IP
done
if $IPSET list $SET_NAME &>/dev/null; then
$IPSET swap $SET_NAME $TMP_NAME
$IPSET destroy $TMP_NAME
else
$IPSET rename $TMP_NAME $SET_NAME
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment