Compare commits
7 Commits
c7223c32b3
...
24ade5322f
Author | SHA1 | Date |
---|---|---|
Asif Bacchus | 24ade5322f | |
Asif Bacchus | 828dea9aee | |
Asif Bacchus | 8d7328c0d9 | |
Asif Bacchus | 9e10e4ddd2 | |
Asif Bacchus | 5a91819f3b | |
Asif Bacchus | b366996b85 | |
Asif Bacchus | 9eeaa1af31 |
|
@ -0,0 +1 @@
|
|||
*.log
|
120
cfddns.sh
120
cfddns.sh
|
@ -34,6 +34,8 @@ echo -e "\ta valid IP6 address using the -i parameter or that your"
|
|||
echo -e "\tmachine's IP6 address can be correctly detected externally."
|
||||
echo -e "-h\tDisplay this help page"
|
||||
echo -e "-x\tDisplay script examples"
|
||||
echo -e "-l\tLocation for log file output"
|
||||
echo -e "\tDefault: scriptname.ext.log in same directory as this script"
|
||||
echo -e "\n\e[1;39mExamples:"
|
||||
echo -e "\e[0;39mRun \e[1;36m$(basename ${0}) \e[1;92m-x\e[0m\n"
|
||||
echo -e "\n"
|
||||
|
@ -80,7 +82,8 @@ quit none
|
|||
function quit {
|
||||
if [ -z "$1" ]; then
|
||||
# exit cleanly
|
||||
echo -e "\e[1;32m--[SUCCESS] Script completed --\e[0m" >> $logFile
|
||||
echo -e "\e[1;32m[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"--[SUCCESS] Script completed --\e[0m" >> $logFile
|
||||
exit 0
|
||||
elif [ "$1" = "none" ]; then
|
||||
if [ -z "$2" ]; then
|
||||
|
@ -93,13 +96,14 @@ function quit {
|
|||
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
|
||||
echo -e "\e[1;31m[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"-- [ERROR] $failedName was NOT updated --\e[0m" >> $logFile
|
||||
done
|
||||
exit "$1"
|
||||
else
|
||||
# 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[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"-- [ERROR] Script exited with code $1 --" >> $logFile
|
||||
echo -e "\e[0;31m${errorExplain[$1]}\e[0m" >> $logFile
|
||||
exit "$1"
|
||||
fi
|
||||
|
@ -125,6 +129,7 @@ ip6=0
|
|||
|
||||
## define error code explainations
|
||||
errorExplain[1]="Missing or invalid parameters on script invocation."
|
||||
errorExplain[2]="curl is required to access CloudFlare API. Please install curl. (apt-get install curl on debian/ubuntu)."
|
||||
errorExplain[101]="Location of file with CloudFlare account details was NOT provided (-f parameter missing)."
|
||||
errorExplain[102]="CloudFlare account details file is empty or does not exist"
|
||||
errorExplain[103]="No DNS records to update were specified (-r parameter(s) missing)."
|
||||
|
@ -133,11 +138,10 @@ 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."
|
||||
|
||||
|
||||
## Logging parameters -- default set to 'quiet' (i.e. the logFile) in same
|
||||
## Logging parameters -- default set to scriptname.ext.log in same
|
||||
## directory as this script
|
||||
scriptPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
logFile="$scriptPath/cfddns.log"
|
||||
unset logVerboseFile
|
||||
logFile="$scriptPath/$(basename ${0}).log"
|
||||
|
||||
|
||||
### Process script parameters
|
||||
|
@ -145,7 +149,7 @@ if [ -z $1 ]; then
|
|||
scriptHelp 1
|
||||
fi
|
||||
|
||||
while getopts ':f:r:i:46hxl:v' PARAMS; do
|
||||
while getopts ':f:r:i:46hxl:' PARAMS; do
|
||||
case "$PARAMS" in
|
||||
f)
|
||||
# path to file with CloudFlare account details
|
||||
|
@ -181,10 +185,6 @@ while getopts ':f:r:i:46hxl:v' PARAMS; do
|
|||
# Path to write log file
|
||||
logFile="${OPTARG}"
|
||||
;;
|
||||
v)
|
||||
# Verbose logging mode
|
||||
logVerboseFile="$logFile"
|
||||
;;
|
||||
?)
|
||||
scriptHelp 1
|
||||
;;
|
||||
|
@ -192,8 +192,8 @@ while getopts ':f:r:i:46hxl:v' PARAMS; do
|
|||
done
|
||||
|
||||
# Log beginning of script
|
||||
echo -e "\e[1;32m [`date %Y-%m-%d` `date %H:%M:%S`] -- Start script execution" \
|
||||
"--\e[0m" >> $logFile
|
||||
echo -e "\e[1;32m[`date +%Y-%m-%d` `date +%H:%M:%S`] -- Start CloudFlare" \
|
||||
"DDNS script execution --\e[0m" >> $logFile
|
||||
|
||||
# Check validity of parameters
|
||||
if [ -z "$accountFile" ] || [[ $accountFile == -* ]]; then
|
||||
|
@ -204,13 +204,20 @@ elif [ -z ${dnsRecords} ]; then
|
|||
quit 103
|
||||
fi
|
||||
|
||||
# Check if curl is installed
|
||||
command -v curl
|
||||
curlResult=$(echo "$?")
|
||||
if [ $curlResult -ne 0 ]; then
|
||||
quit 2
|
||||
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
|
||||
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
|
||||
echo -e "\e[1;36m [`date +%Y-%m-%d` `date +%H:%M:%S`] Script running in" \
|
||||
"IP6 mode\e[0m" >> $logFile
|
||||
fi
|
||||
|
||||
|
||||
|
@ -219,8 +226,9 @@ mapfile -t cfDetails < "$accountFile"
|
|||
|
||||
## Get current IP address, if not provided in parameters
|
||||
if [ -z "$ipAddress" ]; then
|
||||
echo -e "\e[0;36mNo IP address for update provided. Detecting" \
|
||||
"this machine's IP address..." >> $logVerboseFile
|
||||
echo -e "\e[1;36m[`date +%Y-%m-%d` `date +%H:%M:%S`] No IP address for" \
|
||||
"update provided. Detecting this machine's IP address..." \
|
||||
>> $logFile
|
||||
if [ $ip4 -eq 1 ]; then
|
||||
ipAddress=$(curl -s http://ipv4.icanhazip.com)
|
||||
elif [ $ip6 -eq 1 ]; then
|
||||
|
@ -232,21 +240,24 @@ if [ -z "$ipAddress" ]; then
|
|||
quit 201
|
||||
fi
|
||||
else
|
||||
echo -e "\e[0;36mUsing IP address: $ipAddress" >> $logFile
|
||||
echo -e "\e[1;36m[`date +%Y-%m-%d` `date +%H:%M:%S`] Using IP address:" \
|
||||
"$ipAddress" >> $logFile
|
||||
fi
|
||||
|
||||
|
||||
## Check if desired record(s) exist at CloudFlare
|
||||
echo -e "\e[0;36mPerforming CloudFlare lookup on specified DNS" \
|
||||
"records...\e[0m" >> $logVerboseFile
|
||||
echo -e "\e[0m[`date +%Y-%m-%d` `date +%H:%M:%S`] Performing CloudFlare" \
|
||||
"lookup on specified DNS records...\e[0m" >> $logFile
|
||||
# perform checks on A or AAAA records based on invocation options
|
||||
if [ $ip4 -eq 1 ]; then
|
||||
echo -e "\t(IP4: ${dnsRecords[*]})"
|
||||
echo -e "[`date +%Y-%m-%d` `date +%H:%M:%S`] (IP4: ${dnsRecords[*]})" \
|
||||
>> $logFile
|
||||
for cfLookup in "${dnsRecords[@]}"; do
|
||||
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
|
||||
elif [ $ip6 -eq 1 ]; then
|
||||
echo -e "\t(IP6: ${dnsRecords[*]})"
|
||||
echo -e "[`date +%Y-%m-%d` `date +%H:%M:%S`] (IP6: ${dnsRecords[*]})" \
|
||||
>> $logFile
|
||||
for cfLookup in "${dnsRecords[@]}"; do
|
||||
cfRecords+=("$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records?name=$cfLookup&type=AAAA" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json")")
|
||||
done
|
||||
|
@ -260,8 +271,9 @@ fi
|
|||
for recordIdx in "${!cfRecords[@]}"; do
|
||||
if [[ ${cfRecords[recordIdx]} == *"\"count\":0"* ]]; then
|
||||
# inform user that domain not found in CloudFlare DNS records
|
||||
echo -e "\e[0;31m***${dnsRecords[recordIdx]} not found in your" \
|
||||
"CloudFlare DNS records***\e[0m" >> $logVerboseFile
|
||||
echo -e "\e[0;31m[`date +%Y-%m-%d` `date +%H:%M:%S`] ***" \
|
||||
"${dnsRecords[recordIdx]} not found in your" \
|
||||
"CloudFlare DNS records***\e[0m" >> $logFile
|
||||
# remove the entry from the dnsRecords array
|
||||
unset dnsRecords[$recordIdx]
|
||||
# remove the entry from the records array
|
||||
|
@ -279,9 +291,9 @@ if [ -z ${dnsRecords} ]; then
|
|||
quit 104
|
||||
else
|
||||
for recordIdx in "${!cfRecords[@]}"; do
|
||||
echo -e "\n\e[0;33mFound ${dnsRecords[recordIdx]}" \
|
||||
"(Index: $recordIdx):\e[0m" >> $logVerboseFile
|
||||
echo -e "${cfRecords[recordIdx]}" >> $logVerboseFile
|
||||
echo -e "\e[1;39m[`date +%Y-%m-%d` `date +%H:%M:%S`] Found" \
|
||||
"${dnsRecords[recordIdx]} (Index: $recordIdx):\e[0m" \
|
||||
>> $logFile
|
||||
done
|
||||
fi
|
||||
|
||||
|
@ -292,48 +304,40 @@ for recordIdx in "${!cfRecords[@]}"; do
|
|||
grep -Po '(?<="content":")[^"]*'))
|
||||
recordID+=($(echo "${cfRecords[recordIdx]}" | \
|
||||
grep -Po '(?<="id":")[^"]*'))
|
||||
echo -e "\e[1;36mIndex $recordIdx: \e[0mFor record\e[1;33m" \
|
||||
"${dnsRecords[recordIdx]}\e[0m" \
|
||||
"with ID: \e[1;33m${recordID[recordIdx]}\e[0m" \
|
||||
echo -e "\e[0m[`date +%Y-%m-%d` `date +%H:%M:%S`] Index $recordIdx:" \
|
||||
"For record ${dnsRecords[recordIdx]}" \
|
||||
"with ID: ${recordID[recordIdx]}" \
|
||||
"the current IP is \e[1;35m ${currentIP[recordIdx]}" \
|
||||
"\e[0m" >> $logVerboseFile
|
||||
"\e[0m" >> $logFile
|
||||
done
|
||||
|
||||
## Check whether new IP matches old IP and update if they do not match
|
||||
for recordIdx in "${!currentIP[@]}"; do
|
||||
if [ ${currentIP[recordIdx]} = $ipAddress ]; then
|
||||
echo -e "\e[0;32m${dnsRecords[recordIdx]} is up-to-date.\e[0m" \
|
||||
>> $logVerboseFile
|
||||
echo -e "\e[1;32m[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"${dnsRecords[recordIdx]} is up-to-date.\e[0m" \
|
||||
>> $logFile
|
||||
else
|
||||
echo -e "\e[0;31m${dnsRecords[recordIdx]} needs updating...\e[0m" \
|
||||
>> $logVerboseFile
|
||||
echo -e "\e[0;31m[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"${dnsRecords[recordIdx]} needs updating...\e[0m" \
|
||||
>> $logFile
|
||||
if [ $ip4 -eq 1 ]; then
|
||||
# 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\"}")
|
||||
# check for success code from CloudFlare
|
||||
if [[ $update == *"\"success\":true"* ]]; then
|
||||
echo -e "\e[1;32m${dnsRecords[recordIdx]} updated.\e[0m]" \
|
||||
>> $logFile
|
||||
else
|
||||
echo -e "\e[1;31m${dnsRecords[recordIdx]} update failed\e[0m" \
|
||||
>> $logFile
|
||||
echo -e "\e[0;39m$update" >> $logVerboseFile
|
||||
failedDNS+=("${dnsRecords[recordIdx]}")
|
||||
fi
|
||||
elif [ $ip6 -eq 1 ]; then
|
||||
# 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\"}")
|
||||
# check for success code from CloudFlare
|
||||
if [[ $update == *"\"success\":true"* ]]; then
|
||||
echo -e "\e[1;32m${dnsRecords[recordIdx]} updated.\e[0m" \
|
||||
>> $logFile
|
||||
else
|
||||
echo -e "\e[1;31m${dnsRecords[recordIdx]} update failed\e[0m" \
|
||||
>> $logFile
|
||||
echo -e "\e[0;39m$update" >> $logVerboseFile
|
||||
failedDNS+=("${dnsRecords[recordIdx]}")
|
||||
fi
|
||||
fi
|
||||
# check for success code from CloudFlare
|
||||
if [[ $update == *"\"success\":true"* ]]; then
|
||||
echo -e "\e[1;32m[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"${dnsRecords[recordIdx]} updated.\e[0m" >> $logFile
|
||||
else
|
||||
echo -e "\e[1;31m[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"${dnsRecords[recordIdx]} update failed\e[0m" >> $logFile
|
||||
echo -e "\e[0;39m$update\e[0m" >> $logFile
|
||||
failedDNS+=("${dnsRecords[recordIdx]}")
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
|
|
Loading…
Reference in New Issue