Compare commits
8 Commits
532f2abd1e
...
c7223c32b3
Author | SHA1 | Date |
---|---|---|
Asif Bacchus | c7223c32b3 | |
Asif Bacchus | ea979c5c93 | |
Asif Bacchus | d9d5f71b5a | |
Asif Bacchus | 26042b952e | |
Asif Bacchus | e24f578b8f | |
Asif Bacchus | 4d28f068f0 | |
Asif Bacchus | d9804ee8ec | |
Asif Bacchus | 37d51c50e7 |
107
cfddns.sh
107
cfddns.sh
|
@ -80,20 +80,27 @@ quit none
|
|||
function quit {
|
||||
if [ -z "$1" ]; then
|
||||
# exit cleanly
|
||||
echo -e "\e[1;32m--[SUCCESS] Script completed --\e[0m"
|
||||
echo -e "\e[1;32m--[SUCCESS] Script completed --\e[0m" >> $logFile
|
||||
exit 0
|
||||
elif [ "$1" = "none" ]; then
|
||||
if [ -z "$2" ]; then
|
||||
# exit cleanly
|
||||
exit 0
|
||||
else
|
||||
# exit with error code but don't display it
|
||||
# exit with error code but don't log/display it
|
||||
exit "$2"
|
||||
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
|
||||
# notify use that error has occurred and provide exit code
|
||||
echo -e "\e[1;31m-- [ERROR] Script exited with code $1 --"
|
||||
echo -e "\e[0;31m${errorExplain[$1]}\e[0m"
|
||||
echo -e "\e[1;31m-- [ERROR] Script exited with code $1 --" >> $logFile
|
||||
echo -e "\e[0;31m${errorExplain[$1]}\e[0m" >> $logFile
|
||||
exit "$1"
|
||||
fi
|
||||
}
|
||||
|
@ -111,6 +118,7 @@ cfDetails=()
|
|||
cfRecords=()
|
||||
currentIP=()
|
||||
recordID=()
|
||||
failedDNS=()
|
||||
ip4=1
|
||||
ip6=0
|
||||
|
||||
|
@ -125,44 +133,68 @@ 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
|
||||
## directory as this script
|
||||
scriptPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
logFile="$scriptPath/cfddns.log"
|
||||
unset logVerboseFile
|
||||
|
||||
|
||||
### Process script parameters
|
||||
if [ -z $1 ]; then
|
||||
echo -e "\e[1;31mNo parameter(s) provided\e[0m\n"
|
||||
scriptHelp 1
|
||||
fi
|
||||
|
||||
while getopts ':f:r:i:46hx' PARAMS; do
|
||||
while getopts ':f:r:i:46hxl:v' PARAMS; do
|
||||
case "$PARAMS" in
|
||||
f)
|
||||
# path to file with CloudFlare account details
|
||||
accountFile="${OPTARG}"
|
||||
;;
|
||||
r)
|
||||
# DNS records to update
|
||||
dnsRecords+=($OPTARG)
|
||||
;;
|
||||
i)
|
||||
# IP address to use -- NOT parsed for correctness
|
||||
ipAddress="$OPTARG"
|
||||
;;
|
||||
4)
|
||||
# Put script in IP4 mode (default)
|
||||
ip4=1
|
||||
ip6=0
|
||||
;;
|
||||
6)
|
||||
# Put script in IP6 mode
|
||||
ip4=0
|
||||
ip6=1
|
||||
;;
|
||||
h)
|
||||
# Display info on script usage
|
||||
scriptHelp
|
||||
;;
|
||||
x)
|
||||
# Show examples of script usage
|
||||
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
|
||||
;;
|
||||
esac
|
||||
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
|
||||
if [ -z "$accountFile" ] || [[ $accountFile == -* ]]; then
|
||||
quit 101
|
||||
|
@ -172,6 +204,15 @@ elif [ -z ${dnsRecords} ]; then
|
|||
quit 103
|
||||
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
|
||||
mapfile -t cfDetails < "$accountFile"
|
||||
|
@ -179,25 +220,25 @@ 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..."
|
||||
"this machine's IP address..." >> $logVerboseFile
|
||||
if [ $ip4 -eq 1 ]; then
|
||||
echo -e "\e[1;36m(set to IP4 mode)\e[0m"
|
||||
ipAddress=$(curl -s http://ipv4.icanhazip.com)
|
||||
elif [ $ip6 -eq 1 ]; then
|
||||
echo -e "\e[1;36m(set to IP6 mode)\e[0m"
|
||||
ipAddress=$(curl -s http://ipv6.icanhazip.com)
|
||||
fi
|
||||
# check if curl reported any errors
|
||||
ipLookupResult=$(echo "$?")
|
||||
if [ "$ipLookupResult" -ne 0 ]; then
|
||||
quit 201
|
||||
else
|
||||
echo -e "\e[0;36mUsing IP address: $ipAddress"
|
||||
fi
|
||||
else
|
||||
echo -e "\e[0;36mUsing 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"
|
||||
echo -e "\e[0;36mPerforming CloudFlare lookup on specified DNS" \
|
||||
"records...\e[0m" >> $logVerboseFile
|
||||
# perform checks on A or AAAA records based on invocation options
|
||||
if [ $ip4 -eq 1 ]; then
|
||||
echo -e "\t(IP4: ${dnsRecords[*]})"
|
||||
|
@ -220,7 +261,7 @@ 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"
|
||||
"CloudFlare DNS records***\e[0m" >> $logVerboseFile
|
||||
# remove the entry from the dnsRecords array
|
||||
unset dnsRecords[$recordIdx]
|
||||
# remove the entry from the records array
|
||||
|
@ -239,8 +280,8 @@ if [ -z ${dnsRecords} ]; then
|
|||
else
|
||||
for recordIdx in "${!cfRecords[@]}"; do
|
||||
echo -e "\n\e[0;33mFound ${dnsRecords[recordIdx]}" \
|
||||
"(Index: $recordIdx):\e[0m"
|
||||
echo -e "${cfRecords[recordIdx]}"
|
||||
"(Index: $recordIdx):\e[0m" >> $logVerboseFile
|
||||
echo -e "${cfRecords[recordIdx]}" >> $logVerboseFile
|
||||
done
|
||||
fi
|
||||
|
||||
|
@ -254,40 +295,54 @@ for recordIdx in "${!cfRecords[@]}"; do
|
|||
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" \
|
||||
"the current IP is \e[1;35m ${currentIP[recordIdx]}\e[0m"
|
||||
"the current IP is \e[1;35m ${currentIP[recordIdx]}" \
|
||||
"\e[0m" >> $logVerboseFile
|
||||
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"
|
||||
echo -e "\e[0;32m${dnsRecords[recordIdx]} is up-to-date.\e[0m" \
|
||||
>> $logVerboseFile
|
||||
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
|
||||
# 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]"
|
||||
echo -e "\e[1;32m${dnsRecords[recordIdx]} updated.\e[0m]" \
|
||||
>> $logFile
|
||||
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
|
||||
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"
|
||||
echo -e "\e[1;32m${dnsRecords[recordIdx]} updated.\e[0m" \
|
||||
>> $logFile
|
||||
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
|
||||
done
|
||||
|
||||
|
||||
|
||||
quit
|
||||
# Check if failedDNS array contains entries and exit with error, else exit 0
|
||||
if [ -z "${failedDNS}" ]; then
|
||||
quit
|
||||
else
|
||||
quit 199
|
||||
fi
|
||||
|
||||
# this code should never be executed
|
||||
exit 99
|
||||
|
|
Loading…
Reference in New Issue