Cloudflare API- bash script to block IP address in firewall (check if IP exists)

Cloudflare API- bash script to block IP address in firewall 
[ check if IP exists in the firewall before blocking ]

Note: The details of organization,zone etc can be found in respective cloudflare login

Requirement: Install jq, https://stedolan.github.io/jq/

 

#!/bin/bash
organization=””
zones=””
email=””
authkey=””
id=””
org_name=””
notes=””

> ipsblocked.txt

for i in `cat ips.txt`; do
RESULT_JSON=`curl -sSX GET “https://api.cloudflare.com/client/v4/organizations/$organization/firewall/access_rules/rules?mode=block&configuration_target=ip&configuration_value=$i” -H “X-Auth-Email: $email” -H “X-Auth-Key: $authkey” -H “Content-Type: application/json”;`
echo “\n”
#echo “$RESULT_JSON”
#echo ” result with jq”
STATUS_IPBLOCKED=`echo “$RESULT_JSON” | jq ‘.mode’`

if [ $STATUS_IPBLOCKED != block ]
then
curl -sSX POST “https://api.cloudflare.com/client/v4/organizations/$organization/firewall/access_rules/rules” -H “X-Auth-Email: $email” -H “X-Auth-Key: $authkey” -H “Content-Type: application/json” –data “{\”mode\”:\”block\”,\”scope\”:{\”id\”:\”$id\”,\”name\”:\”$org_name\”,\”type\”:\”organization\”},\”configuration\”:{\”target\”:\”ip\”,\”value\”:\”$i\”},\”notes\”:\”$notes\”}” ;
echo “\n”
echo “IP $i blocked in Cloudflare”
echo “IP $i blocked in Cloudflare” >> ipsblocked.txt
echo “\n”
else
echo “IP $i already blocked in Cloudflare, skip”
echo “IP $i already blocked in Cloudflare, skip” >> ips_blocked.txt
fi

echo “\n”
done

6 thoughts on “Cloudflare API- bash script to block IP address in firewall (check if IP exists)”

  1. With havin so much written content do you ever run into any issues of plagorism or copyright violation? My site has a lot of unique content I’ve either written myself or outsourced but it looks like a lot of it is popping it up all over the web without my permission. Do you know any solutions to help prevent content from being stolen? I’d really appreciate it.

  2. Hmm is anyone else encountering problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any feed-back would be greatly appreciated.

Leave a Reply

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