Cloudflare Automation Scripts

this is a script to automate the creation of an A record (tested and working) with cloud flare

cloudflareA.sh
#!/usr/bin/env bash

echo -e
echo  "What is the email address associated with your cloudflare account?"
echo -e
read -r email
sleep 0.2s

echo -e
echo  "What is the Global API Key associated with your cloudflare account?"
echo -e
read -r key

echo -e
echo  "What is the name of the A record you wish to make?"
echo -e
read -r A

echo -e
echo  "What is IP address for $A?"
echo -e
read -r IP

read -r -p "Would you like to set this as Proxied? [Y/n] " input
 
case $input in
      [yY][eE][sS]|[yY])
curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "X-Auth-Email: $email" \
    -H "X-Auth-Key: $key" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt

            curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $key" \
-H "Content-Type: application/json" \
--data '{"type":"'"A"'","name":"'"$A"'","content":"'"$IP"'","proxied":'"true"',"ttl":'"1"'}' \
| python -m json.tool; 
            ;;
      [nN][oO]|[nN])

curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "X-Auth-Email: $email" \
    -H "X-Auth-Key: $key" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt
            curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $key" \
-H "Content-Type: application/json" \
--data '{"type":"'"A"'","name":"'"$A"'","content":"'"$IP"'","proxied":'"false"',"ttl":'"1"'}' \
| python -m json.tool; 
            ;;
      *)
            echo "Invalid input..."
            exit 1
            ;;
esac

this is a script to automate the creation of a CNAME record (tested and working) with cloud flare

cloudflareCNAME.sh
#!/usr/bin/env bash

echo -e
echo  "What is the email address associated with your cloudflare account?"
echo -e
read -r email
sleep 0.2s

echo -e
echo  "What is the Global API Key associated with your cloudflare account?"
echo -e
read -r key

echo -e
echo  "What is the name of the CNAME record you wish to make?"
echo -e
read -r CNAME

echo -e
echo  "What is The A record $CNAME will point to?"
echo -e
read -r A

read -r -p "Would you like to set this as Proxied? [Y/n] " input
 
case $input in
      [yY][eE][sS]|[yY])
curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "X-Auth-Email: $email" \
    -H "X-Auth-Key: $key" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt

           curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $key" \
-H "Content-Type: application/json" \
--data '{"type":"'"CNAME"'","name":"'"$CNAME"'","content":"'"$A"'","proxied":'"true"',"ttl":'"1"'}' \
| python -m json.tool; 
            ;;
      [nN][oO]|[nN])

curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "X-Auth-Email: $email" \
    -H "X-Auth-Key: $key" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt
            curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $key" \
-H "Content-Type: application/json" \
--data '{"type":"'"CNAME"'","name":"'"$CNAME"'","content":"'"$A"'","proxied":'"false"',"ttl":'"1"'}' \
| python -m json.tool;
            ;;
      *)
            echo "Invalid input..."
            exit 1
            ;;
esac

this is a script to automate the creation of an MX record (tested and working) with cloud flare

cloudflareMX.sh
#!/usr/bin/env bash

echo -e
echo  "What is the email address associated with your cloudflare account?"
echo -e
read -r email
sleep 0.2s

echo -e
echo  "What is the Global API Key associated with your cloudflare account?"
echo -e
read -r key

echo -e
echo  "What is the host to be (use @ for root domain)?"
echo -e
read -r domain

echo -e
echo  "What is the FQDN that this MX record will point to?"
echo -e
read -r MXDomain

echo -e
echo  "What priority do you want to set?"
echo -e
read -r priority

curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "X-Auth-Email: $email" \
    -H "X-Auth-Key: $key" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt


curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $key" \
-H "Content-Type: application/json" \
    --data '{"type":"'"MX"'","name":"'"$domain"'","content":"'"$MXDomain"'","priority":'"$priority"',"ttl":'"1"'}' \
| python -m json.tool;

this is a script to automate the creation of an TXT record (tested and working) with cloud flare

cloudflareTXT.sh
#!/usr/bin/env bash

echo -e
echo  "What is the email address associated with your cloudflare account?"
echo -e
read -r email
sleep 0.2s

echo -e
echo  "What is the Global API Key associated with your cloudflare account?"
echo -e
read -r key

echo -e
echo  "What is the host to be (use @ for root domain)?"
echo -e
read -r domain

echo -e
echo  "What content is to be included in the TXT record?"
echo -e
read -r content


curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "X-Auth-Email: $email" \
    -H "X-Auth-Key: $key" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt


curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $key" \
-H "Content-Type: application/json" \
    --data '{"type":"'"TXT"'","name":"'"$domain"'","content":"'"$content"'","ttl":'"1"'}' \
| python -m json.tool;

this is a script to automate the creation of a DMARC record (tested and working) with cloud flare

cloudflareDMARC.sh
#!/usr/bin/env bash

echo -e
echo  "What is the email address associated with your cloudflare account?"
echo -e
read -r email
sleep 0.2s

echo -e
echo  "What is the Global API Key associated with your cloudflare account?"
echo -e
read -r key

curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "X-Auth-Email: $email" \
    -H "X-Auth-Key: $key" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt


curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $key" \
-H "Content-Type: application/json" \
    --data '{"type":"'"TXT"'","name":"'"_dmarc"'","content":"'"v=DMARC1; p=quarantine; rua=mailto:autoreply@dmarctest.org"'","ttl":'"1"'}' \
| python -m json.tool;

this is a script to automate the creation of a PTR record (tested and working) with cloud flare

cloudflarePTR.sh
#!/usr/bin/env bash

echo -e
echo  "What is the email address associated with your cloudflare account?"
echo -e
read -r email
sleep 0.2s

echo -e
echo  "What is the Global API Key associated with your cloudflare account?"
echo -e
read -r key

echo -e
echo  "What is the IP of the Reverse IP record?"
echo -e
read -r IP

echo -e
echo  "What is FQDN that this record will be pointing to?"
echo -e
read -r FQDN


curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "X-Auth-Email: $email" \
    -H "X-Auth-Key: $key" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt


curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
-H "X-Auth-Email: $email" \
-H "X-Auth-Key: $key" \
-H "Content-Type: application/json" \
    --data '{"type":"'"PTR"'","name":"'"$IP"'","content":"'"$FQDN"'","ttl":'"1"'}' \
| python -m json.tool;
8 Likes

Sharing is caring. Thanks

4 Likes

here is another version of the scripts in which you create a api token that can be limited access wize (ie only able to edit dns for this@cloudflareaccount.tld and only valid for example 5 days)

this is a script to automate the creation of an A record with token (tested and working) with cloud flare

cloudflaretokenA.sh
#!/usr/bin/env bash

echo -e
echo  "What is your API token ?"
echo -e
read -r token
sleep 0.2s

echo -e
echo  "What is the name of the A record you wish to make?"
echo -e
read -r A

echo -e
echo  "What is IP address for $A?"
echo -e
read -r IP

read -r -p "Would you like to set this as Proxied? [Y/n] " input
 
case $input in
      [yY][eE][sS]|[yY])
curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "Authorization: Bearer $token" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt

            curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
    -H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
--data '{"type":"'"A"'","name":"'"$A"'","content":"'"$IP"'","proxied":'"true"',"ttl":'"1"'}' \
| python -m json.tool; 
            ;;
      [nN][oO]|[nN])

curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "Authorization: Bearer $token" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt
            curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
    -H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
--data '{"type":"'"A"'","name":"'"$A"'","content":"'"$IP"'","proxied":'"false"',"ttl":'"1"'}' \
| python -m json.tool; 
            ;;
      *)
            echo "Invalid input..."
            exit 1
            ;;
esac

this is a script to automate the creation of an CNAME record with token (tested and working) with cloud flare

cloudflaretokenCNAME.sh
#!/usr/bin/env bash

echo -e
echo  "What is your API token ?"
echo -e
read -r token
sleep 0.2s

echo -e
echo  "What is the name of the CNAME record you wish to make?"
echo -e
read -r CNAME

echo -e
echo  "What is the A record $CNAME will point to?"
echo -e
read -r A

read -r -p "Would you like to set this as Proxied? [Y/n] " input
 
case $input in
      [yY][eE][sS]|[yY])
curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "Authorization: Bearer $token" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt

            curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
    -H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
--data '{"type":"'"CNAME"'","name":"'"$CNAME"'","content":"'"$A"'","proxied":'"true"',"ttl":'"1"'}' \
| python -m json.tool; 
            ;;
      [nN][oO]|[nN])

curl -X GET "https://api.cloudflare.com/client/v4/zones" \
    -H "Authorization: Bearer $token" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt
            curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
    -H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
--data '{"type":"'"CNAME"'","name":"'"$CNAME"'","content":"'"$A"'","proxied":'"true"',"ttl":'"1"'}' \
| python -m json.tool; 
            ;;
      *)
            echo "Invalid input..."
            exit 1
            ;;
esac

this is a script to automate the creation of an MX record with token (tested and working) with cloud flare

cloudflaretokenMX.sh
#!/usr/bin/env bash

echo -e
echo  "What is your API token ?"
echo -e
read -r token
sleep 0.2s

echo -e
echo  "What is the host to be (use @ for root domain)?"
echo -e
read -r domain

echo -e
echo  "What is the FQDN that this MX record will point to?"
echo -e
read -r MXDomain

echo -e
echo  "What priority do you want to set?"
echo -e
read -r priority

curl -X GET "https://api.cloudflare.com/client/v4/zones" \
      -H "Authorization: Bearer $token" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt


curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
    -H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
    --data '{"type":"'"MX"'","name":"'"$domain"'","content":"'"$MXDomain"'","priority":'"$priority"',"ttl":'"1"'}' \
| python -m json.tool;

this is a script to automate the creation of an TXT record with token (tested and working) with cloud flare

cloudflaretokenTXT.sh
#!/usr/bin/env bash

echo -e
echo  "What is your API token ?"
echo -e
read -r token
sleep 0.2s

echo -e
echo  "What is the host to be (use @ for root domain)?"
echo -e
read -r domain

echo -e
echo  "What content is to be included in the TXT record?"
echo -e
read -r content


curl -X GET "https://api.cloudflare.com/client/v4/zones" \
      -H "Authorization: Bearer $token" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt


curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
    -H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
    --data '{"type":"'"TXT"'","name":"'"$domain"'","content":"'"$content"'","ttl":'"1"'}' \
| python -m json.tool;

this is a script to automate the creation of a DMARC record with token (tested and working) with cloud flare

cloudflaretokenDMARC.sh
#!/usr/bin/env bash

echo -e
echo  "What is your API token ?"
echo -e
read -r token
sleep 0.2s

curl -X GET "https://api.cloudflare.com/client/v4/zones" \
      -H "Authorization: Bearer $token" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt


curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
    -H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
    --data '{"type":"'"TXT"'","name":"'"_dmarc"'","content":"'"v=DMARC1; p=quarantine; rua=mailto:autoreply@dmarctest.org"'","ttl":'"1"'}' \
| python -m json.tool;

this is a script to automate the creation of a PTR record with token (tested and working) with cloud flare

cloudflaretokenPTR.sh
#!/usr/bin/env bash

echo -e
echo  "What is your API token ?"
echo -e
read -r token
sleep 0.2s

echo -e
echo  "What is the IP of the Reverse IP record?"
echo -e
read -r IP

echo -e
echo  "What is FQDN that this record will be pointing to?"
echo -e
read -r FQDN


curl -X GET "https://api.cloudflare.com/client/v4/zones" \
      -H "Authorization: Bearer $token" \
    -H "Content-Type: application/json" \
    | python -c $'import sys,json\ndata=json.loads(sys.stdin.read())\nif data["success"]:\n\tfor dict in data["result"]:print("Zone ID: " + dict["id"])\nelse:print("ERROR(" + str(data["errors"][0]["code"]) + "): " + data["errors"][0]["message"])' > zoneid.txt


curl -X POST "https://api.cloudflare.com/client/v4/zones/$(sed 's/Zone ID: //' zoneid.txt)/dns_records/" \
    -H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
    --data '{"type":"'"PTR"'","name":"'"$IP"'","content":"'"$FQDN"'","ttl":'"1"'}' \
| python -m json.tool;
2 Likes