Compare commits

..

No commits in common. "c7223c32b35592a3c5ec78f97506aacce30b9305" and "532f2abd1e91a3cdaabb3a0fc673ab4b31d28f39" have entirely different histories.

105
cfddns.sh
View File

@ -80,27 +80,20 @@ quit none
function quit { function quit {
if [ -z "$1" ]; then if [ -z "$1" ]; then
# exit cleanly # exit cleanly
echo -e "\e[1;32m--[SUCCESS] Script completed --\e[0m" >> $logFile echo -e "\e[1;32m--[SUCCESS] Script completed --\e[0m"
exit 0 exit 0
elif [ "$1" = "none" ]; then elif [ "$1" = "none" ]; then
if [ -z "$2" ]; then if [ -z "$2" ]; then
# exit cleanly # exit cleanly
exit 0 exit 0
else else
# exit with error code but don't log/display it # exit with error code but don't display it
exit "$2" exit "$2"
fi fi
elif [ "$1" = "199" ]; then
# list DNS entries that were not updated
for failedName in "${failedDNS[@]}"; do
echo -e "\e[1;31m-- [ERROR] $failedName was NOT updated --\e[0m" \
>> $logFile
done
exit "$1"
else else
# notify use that error has occurred and provide exit code # notify use that error has occurred and provide exit code
echo -e "\e[1;31m-- [ERROR] Script exited with code $1 --" >> $logFile echo -e "\e[1;31m-- [ERROR] Script exited with code $1 --"
echo -e "\e[0;31m${errorExplain[$1]}\e[0m" >> $logFile echo -e "\e[0;31m${errorExplain[$1]}\e[0m"
exit "$1" exit "$1"
fi fi
} }
@ -118,7 +111,6 @@ cfDetails=()
cfRecords=() cfRecords=()
currentIP=() currentIP=()
recordID=() recordID=()
failedDNS=()
ip4=1 ip4=1
ip6=0 ip6=0
@ -133,68 +125,44 @@ errorExplain[201]="Could not detect this machine's IP address. Please re-run thi
errorExplain[254]="Could not connect with CloudFlare API. Please re-run this script later." errorExplain[254]="Could not connect with CloudFlare API. Please re-run this script later."
## Logging parameters -- default set to 'quiet' (i.e. the logFile) in same
## directory as this script
scriptPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
logFile="$scriptPath/cfddns.log"
unset logVerboseFile
### Process script parameters ### Process script parameters
if [ -z $1 ]; then if [ -z $1 ]; then
echo -e "\e[1;31mNo parameter(s) provided\e[0m\n"
scriptHelp 1 scriptHelp 1
fi fi
while getopts ':f:r:i:46hxl:v' PARAMS; do while getopts ':f:r:i:46hx' PARAMS; do
case "$PARAMS" in case "$PARAMS" in
f) f)
# path to file with CloudFlare account details
accountFile="${OPTARG}" accountFile="${OPTARG}"
;; ;;
r) r)
# DNS records to update
dnsRecords+=($OPTARG) dnsRecords+=($OPTARG)
;; ;;
i) i)
# IP address to use -- NOT parsed for correctness
ipAddress="$OPTARG" ipAddress="$OPTARG"
;; ;;
4) 4)
# Put script in IP4 mode (default)
ip4=1 ip4=1
ip6=0 ip6=0
;; ;;
6) 6)
# Put script in IP6 mode
ip4=0 ip4=0
ip6=1 ip6=1
;; ;;
h) h)
# Display info on script usage
scriptHelp scriptHelp
;; ;;
x) x)
# Show examples of script usage
scriptExamples scriptExamples
;; ;;
l)
# Path to write log file
logFile="${OPTARG}"
;;
v)
# Verbose logging mode
logVerboseFile="$logFile"
;;
?) ?)
echo -e "\e[1;31mInvalid parameter(s) provided\e[0m\n"
scriptHelp 1 scriptHelp 1
;; ;;
esac esac
done done
# Log beginning of script
echo -e "\e[1;32m [`date %Y-%m-%d` `date %H:%M:%S`] -- Start script execution" \
"--\e[0m" >> $logFile
# Check validity of parameters # Check validity of parameters
if [ -z "$accountFile" ] || [[ $accountFile == -* ]]; then if [ -z "$accountFile" ] || [[ $accountFile == -* ]]; then
quit 101 quit 101
@ -204,15 +172,6 @@ elif [ -z ${dnsRecords} ]; then
quit 103 quit 103
fi fi
# Log operating mode
if [ $ip4 -eq 1 ]; then
echo -e "\e[1;36m [`date %Y-%m-%d` `date %H:%M:%S`] Script running in IP4" \
"mode\e[0m" >> $logFile
elif [ $ip6 -eq 1 ]; then
echo -e "\e[1;36m [`date %Y-%m-%d` `date %H:%M:%S`] Script running in IP6" \
"mode\e[0m" >> $logFile
fi
## Extract needed information from accountDetails file ## Extract needed information from accountDetails file
mapfile -t cfDetails < "$accountFile" mapfile -t cfDetails < "$accountFile"
@ -220,25 +179,25 @@ mapfile -t cfDetails < "$accountFile"
## Get current IP address, if not provided in parameters ## Get current IP address, if not provided in parameters
if [ -z "$ipAddress" ]; then if [ -z "$ipAddress" ]; then
echo -e "\e[0;36mNo IP address for update provided. Detecting" \ echo -e "\e[0;36mNo IP address for update provided. Detecting" \
"this machine's IP address..." >> $logVerboseFile "this machine's IP address..."
if [ $ip4 -eq 1 ]; then if [ $ip4 -eq 1 ]; then
echo -e "\e[1;36m(set to IP4 mode)\e[0m"
ipAddress=$(curl -s http://ipv4.icanhazip.com) ipAddress=$(curl -s http://ipv4.icanhazip.com)
elif [ $ip6 -eq 1 ]; then elif [ $ip6 -eq 1 ]; then
echo -e "\e[1;36m(set to IP6 mode)\e[0m"
ipAddress=$(curl -s http://ipv6.icanhazip.com) ipAddress=$(curl -s http://ipv6.icanhazip.com)
fi fi
# check if curl reported any errors
ipLookupResult=$(echo "$?") ipLookupResult=$(echo "$?")
if [ "$ipLookupResult" -ne 0 ]; then if [ "$ipLookupResult" -ne 0 ]; then
quit 201 quit 201
fi
else else
echo -e "\e[0;36mUsing IP address: $ipAddress" >> $logFile echo -e "\e[0;36mUsing IP address: $ipAddress"
fi
fi 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" \ echo -e "\e[0;36mPerforming CloudFlare lookup on specified DNS records...\e[0m"
"records...\e[0m" >> $logVerboseFile
# perform checks on A or AAAA records based on invocation options # perform checks on A or AAAA records based on invocation options
if [ $ip4 -eq 1 ]; then if [ $ip4 -eq 1 ]; then
echo -e "\t(IP4: ${dnsRecords[*]})" echo -e "\t(IP4: ${dnsRecords[*]})"
@ -261,7 +220,7 @@ for recordIdx in "${!cfRecords[@]}"; do
if [[ ${cfRecords[recordIdx]} == *"\"count\":0"* ]]; then if [[ ${cfRecords[recordIdx]} == *"\"count\":0"* ]]; then
# inform user that domain not found in CloudFlare DNS records # inform user that domain not found in CloudFlare DNS records
echo -e "\e[0;31m***${dnsRecords[recordIdx]} not found in your" \ echo -e "\e[0;31m***${dnsRecords[recordIdx]} not found in your" \
"CloudFlare DNS records***\e[0m" >> $logVerboseFile "CloudFlare DNS records***\e[0m"
# remove the entry from the dnsRecords array # remove the entry from the dnsRecords array
unset dnsRecords[$recordIdx] unset dnsRecords[$recordIdx]
# remove the entry from the records array # remove the entry from the records array
@ -280,8 +239,8 @@ if [ -z ${dnsRecords} ]; then
else else
for recordIdx in "${!cfRecords[@]}"; do for recordIdx in "${!cfRecords[@]}"; do
echo -e "\n\e[0;33mFound ${dnsRecords[recordIdx]}" \ echo -e "\n\e[0;33mFound ${dnsRecords[recordIdx]}" \
"(Index: $recordIdx):\e[0m" >> $logVerboseFile "(Index: $recordIdx):\e[0m"
echo -e "${cfRecords[recordIdx]}" >> $logVerboseFile echo -e "${cfRecords[recordIdx]}"
done done
fi fi
@ -295,54 +254,40 @@ for recordIdx in "${!cfRecords[@]}"; do
echo -e "\e[1;36mIndex $recordIdx: \e[0mFor record\e[1;33m" \ echo -e "\e[1;36mIndex $recordIdx: \e[0mFor record\e[1;33m" \
"${dnsRecords[recordIdx]}\e[0m" \ "${dnsRecords[recordIdx]}\e[0m" \
"with ID: \e[1;33m${recordID[recordIdx]}\e[0m" \ "with ID: \e[1;33m${recordID[recordIdx]}\e[0m" \
"the current IP is \e[1;35m ${currentIP[recordIdx]}" \ "the current IP is \e[1;35m ${currentIP[recordIdx]}\e[0m"
"\e[0m" >> $logVerboseFile
done done
## Check whether new IP matches old IP and update if they do not match ## Check whether new IP matches old IP and update if they do not match
for recordIdx in "${!currentIP[@]}"; do for recordIdx in "${!currentIP[@]}"; do
if [ ${currentIP[recordIdx]} = $ipAddress ]; then if [ ${currentIP[recordIdx]} = $ipAddress ]; then
echo -e "\e[0;32m${dnsRecords[recordIdx]} is up-to-date.\e[0m" \ echo -e "\e[0;32m${dnsRecords[recordIdx]} is up-to-date.\e[0m"
>> $logVerboseFile
else else
echo -e "\e[0;31m${dnsRecords[recordIdx]} needs updating...\e[0m" \ echo -e "\e[0;31m${dnsRecords[recordIdx]} needs updating...\e[0m"
>> $logVerboseFile
if [ $ip4 -eq 1 ]; then if [ $ip4 -eq 1 ]; then
# update record at CloudFlare with new IP # update record at CloudFlare with new IP
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records/${recordID[recordIdx]}" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json" --data "{\"id\":\"${cfDetails[2]}\",\"type\":\"A\",\"proxied\":false,\"name\":\"${dnsRecords[recordIdx]}\",\"content\":\"$ipAddress\"}") update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records/${recordID[recordIdx]}" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json" --data "{\"id\":\"${cfDetails[2]}\",\"type\":\"A\",\"proxied\":false,\"name\":\"${dnsRecords[recordIdx]}\",\"content\":\"$ipAddress\"}")
# check for success code from CloudFlare # check for success code from CloudFlare
if [[ $update == *"\"success\":true"* ]]; then if [[ $update == *"\"success\":true"* ]]; then
echo -e "\e[1;32m${dnsRecords[recordIdx]} updated.\e[0m]" \ echo -e "\e[1;32m${dnsRecords[recordIdx]} updated.\e[0m]"
>> $logFile
else else
echo -e "\e[1;31m${dnsRecords[recordIdx]} update failed\e[0m" \ echo -e "\e[1;31m${dnsRecords[recordIdx]} update failed\e[0m"
>> $logFile
echo -e "\e[0;39m$update" >> $logVerboseFile
failedDNS+=("${dnsRecords[recordIdx]}")
fi fi
elif [ $ip6 -eq 1 ]; then elif [ $ip6 -eq 1 ]; then
# update record at CloudFlare with new IP # update record at CloudFlare with new IP
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records/${recordID[recordIdx]}" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json" --data "{\"id\":\"${cfDetails[2]}\",\"type\":\"AAAA\",\"proxied\":false,\"name\":\"${dnsRecords[recordIdx]}\",\"content\":\"$ipAddress\"}") update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records/${recordID[recordIdx]}" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json" --data "{\"id\":\"${cfDetails[2]}\",\"type\":\"AAAA\",\"proxied\":false,\"name\":\"${dnsRecords[recordIdx]}\",\"content\":\"$ipAddress\"}")
# check for success code from CloudFlare # check for success code from CloudFlare
if [[ $update == *"\"success\":true"* ]]; then if [[ $update == *"\"success\":true"* ]]; then
echo -e "\e[1;32m${dnsRecords[recordIdx]} updated.\e[0m" \ echo -e "\e[1;32m${dnsRecords[recordIdx]} updated.\e[0m"
>> $logFile
else else
echo -e "\e[1;31m${dnsRecords[recordIdx]} update failed\e[0m" \ echo -e "\e[1;31m${dnsRecords[recordIdx]} update failed\e[0m"
>> $logFile
echo -e "\e[0;39m$update" >> $logVerboseFile
failedDNS+=("${dnsRecords[recordIdx]}")
fi fi
fi fi
fi fi
done done
# Check if failedDNS array contains entries and exit with error, else exit 0
if [ -z "${failedDNS}" ]; then
quit quit
else
quit 199
fi
# this code should never be executed # this code should never be executed
exit 99 exit 99