Skip to content

Instantly share code, notes, and snippets.

@frankstallone
Last active June 8, 2018 18:00
Show Gist options
  • Save frankstallone/2f9ea6681f0540e439ad9779f5da5492 to your computer and use it in GitHub Desktop.
Save frankstallone/2f9ea6681f0540e439ad9779f5da5492 to your computer and use it in GitHub Desktop.
CloudFlare Bash Whitelist Loop
#!/bin/bash
# Author: Frank Stallone
# $CF_AID, $CF_EMAIL, $CF_API_KEY are exported from .bashrc
# Update $IPLIST and $CF_NOTES
# Test API: Works
# curl -X GET "https://api.cloudflare.com/client/v4/memberships?status=accepted" \
# -H "X-Auth-Email: $CF_EMAIL" \
# -H "X-Auth-Key: $CF_API_KEY" \
# -H "Content-Type: application/json"
# txt file with IP addresses
IPLIST="https://managewp.com/wp-content/uploads/2016/11/managewp-ips.txt"
#IPLIST="https://uptimerobot.com/inc/files/ips/IPv4.txt"
CF_NOTES="ManageWP"
# grab current IP list
wget $IPLIST -O /tmp/IPLISTFILE
# Loop through array and add IPs to Cloudflare whitelist
for IP in $(cat /tmp/IPLISTFILE); do
echo "Submitting IP... $IP"
curl -s -X POST "https://api.cloudflare.com/client/v4/accounts/$CF_AID/firewall/access_rules/rules" \
-H "X-Auth-Email: $CF_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json" \
--data '{"mode":"whitelist","configuration":{"target":"ip","value":"'$IP'"},"notes":"'$CF_NOTES'"}'
sleep 2
done
# delete the temporary IPs file
rm -rf /tmp/IPLISTFILE
@frankstallone
Copy link
Author

Currently this is giving me the following error when I run it via . cl-whitelist.sh:
{ "result": null, "success": false, "errors": [ { "message": "firewallaccessrules.api.unknown_error" } ], "messages": [] }

But if I just run the CURL with those variables available it works fine.

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