Removed echo testing section.

CloudFlare lookup section removes non-existant domains from arrays.
This commit is contained in:
Asif Bacchus 2018-09-12 05:18:54 -06:00
parent aa10ba23e1
commit 41a66ad2e1

View File

@ -72,7 +72,7 @@ unset accountFile
unset ipAddress unset ipAddress
dnsRecords=() dnsRecords=()
cfDetails=() cfDetails=()
record=() cfRecords=()
ip4=1 ip4=1
ip6=0 ip6=0
@ -151,44 +151,36 @@ fi
## Check if desired record(s) exist at CloudFlare ## Check if desired record(s) exist at CloudFlare
echo -e "\e[0;36mPerforming CloudFlare lookup on specified DNS records...\e[0m" echo -e "\e[0;36mPerforming CloudFlare lookup on specified DNS records...\e[0m"
echo -e "\t(${dnsRecords[*]})"
for cfLookup in "${dnsRecords[@]}"; do for cfLookup in "${dnsRecords[@]}"; do
record+=("$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records?name=$cfLookup&type=A" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json")") cfRecords+=("$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records?name=$cfLookup&type=A" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json")")
done done
# check for curl errors
cfLookupResult=$(echo "$?") cfLookupResult=$(echo "$?")
if [ "$ipLookupResult" -ne 0 ]; then if [ "$cfLookupResult" -ne 0 ]; then
echo -e "\e[1;31mThere was a problem accessing the CloudFlare API" echo -e "\e[1;31mThere was a problem accessing the CloudFlare API"
echo -e "\e[0;31mPlease re-run this script later.\e[0m" echo -e "\e[0;31mPlease re-run this script later.\e[0m"
exit 254 exit 254
else
echo -e "\e[0;36m...done"
fi fi
# check for any non-existant domain names and contract array accordingly
for recordIdx in "${!cfRecords[@]}"; do
### Echo results (testing) if [[ ${cfRecords[recordIdx]} == *"\"count\":0"* ]]; then
echo -e "\nBased on parameters provided:" # inform user that domain not found in CloudFlare DNS records
echo -e "\e[0;35mLogin details at: ${accountFile}" echo -e "\e[0;31m***${dnsRecords[recordIdx]} not found in your" \
echo -e "\tAuthorized email: ${cfDetails[0]}" "CloudFlare DNS records***\e[0m"
echo -e "\tAuthorized key: ${cfDetails[1]}" # remove the entry from the dnsRecords array
echo -e "\tZone identifier: ${cfDetails[2]}" unset dnsRecords[$recordIdx]
echo -e "\e[0;33mUpdating records: ${dnsRecords[*]}" dnsRecords=("${dnsRecords[@]}")
if [ $ip4 -eq 1 ]; then # remove the entry from the records array
echo -e "\e[0;92mUpdating A records" unset cfRecords[$recordIdx]
elif [ $ip6 -eq 1 ]; then cfRecords=("${cfRecords[@]}")
echo -e "\e[0;92mUpdating AAAA records"
fi
echo -e "\e[0;92mPointing records to IP: $ipAddress"
echo -e "\e[0m\n"
echo -e "\e[0;39mRecord check:"
echo "Array length: ${#record[@]}"
echo "Results:"
for recordIdx in "${!record[@]}"; do
echo -e "\n\e[0;33mResult for ${dnsRecords[recordIdx]}:\e[0m"
if [[ ${record[recordIdx]} == *"\"count\":0"* ]]; then
echo -e "\e[0;31m***not found in your CloudFlare DNS records***\e[0m"
else
echo -e "${record[recordIdx]}"
fi fi
done done
# list array contents
for recordIdx in "${!cfRecords[@]}"; do
echo -e "\n\e[0;33m Found ${dnsRecords[recordIdx]}" \
"(Index: $recordIdx):\e[0m"
echo -e "${cfRecords[recordIdx]}"
done
exit 0 exit 0