Hi All,
I am working on a bash script to create a Page Rule for a list of domains we have. All the domains are going to get the same page rule.
I am trying to feed it a domain list, have store the zone_id and then run it in a loop to create the page rule.
I have been banging my head on this and I am missing something simple. Any one have any suggestions?
#!/bin/bash
auth_email=
auth_key=
zonefile=
zone_name=
echo -e "\nThis script will bulk add a default page rule for zones in a file\n"
echo -e "*************************************************\n"
read -p 'Enter your email account to login to Cloudflare: ' auth_email
read -sp 'Enter your Cloudflare API key : ' auth_key
echo
read -p 'Enter the filename containing the zones to add page rule in this directory (Enter blank to use zones.txt): ' zonefile
zonefile="${zonefile:="zones.txt"}"
while read z; do
zone_name="$z"
result=$(curl -X GET "https://api.cloudflare.com/client/v4/zones?name=${zone_name}&status=active&page=1&per_page=20&order=status&direction=desc&match=all" -H "X-Auth-Email: ${auth_email}" -H "X-Auth-Key: ${auth_key}" -H "Content-Type: application/json")
zonetag=$(echo "$result" | jq -r '. | .result | .id')
pagerules_result=$(curl -X POST "https://api.cloudflare.com/client/v4/zones/${zonetag}/pagerules" -H "X-Auth-Email: ${auth_email}" -H "X-Auth-Key: ${auth_key}" -H "Content-Type: application/json" --data '{"targets":[{"target":"url","constraint":{"operator":"matches","value":"'*${zone_name}\/*'"}}],"actions":[{"id":"forwarding_url","value":{"url":"https:\/\/www.ourdomain.com","status_code":301}}],"priority":1,"status":"active"}')
pagerules_status=$(echo "$pagerules_result" | jq -r '. | .result | .status')
echo "Zone: $zone_name, zone id: $zonetag, status: $pagerules_status"
done <"$zonefile"