Compare commits
6 Commits
8c24c6e625
...
8a0362a6e1
Author | SHA1 | Date | |
---|---|---|---|
|
8a0362a6e1 | ||
|
92519055fc | ||
|
3585490f80 | ||
|
356dfc7f21 | ||
|
1e8ff53075 | ||
|
12c0cdde93 |
98
cfddns.sh
98
cfddns.sh
@ -82,8 +82,8 @@ quit none
|
||||
function quit {
|
||||
if [ -z "$1" ]; then
|
||||
# exit cleanly
|
||||
echo -e "\e[1;32m[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"--[SUCCESS] Script completed --\e[0m" >> $logFile
|
||||
echo -e "${bold}${note}${stamp} -- Script completed --\${normal}" \
|
||||
>> "$logFile"
|
||||
exit 0
|
||||
elif [ "$1" = "none" ]; then
|
||||
if [ -z "$2" ]; then
|
||||
@ -96,15 +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[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"-- [ERROR] $failedName was NOT updated --\e[0m" >> $logFile
|
||||
echo -e "${bold}${err}${stamp}" \
|
||||
"-- [ERROR] $failedName was NOT updated --${normal}" >> "$logFile"
|
||||
done
|
||||
exit "$1"
|
||||
else
|
||||
# notify use that error has occurred and provide exit code
|
||||
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
|
||||
echo -e "${bold}${err}${stamp}" \
|
||||
"-- [ERROR] ${errorExplain[$1]} (code: $1) --${normal}" >> "$logFile"
|
||||
exit "$1"
|
||||
fi
|
||||
}
|
||||
@ -127,6 +126,20 @@ ip4=1
|
||||
ip6=0
|
||||
|
||||
|
||||
### define script variables
|
||||
# timestamp
|
||||
stamp="${stamp}"
|
||||
# formatting
|
||||
normal="\e[0m"
|
||||
bold="\e[1m"
|
||||
default="\e[39m"
|
||||
ok="\e[32m"
|
||||
err="\e[31m"
|
||||
info="\e[96m"
|
||||
lit="\e[93m"
|
||||
note="\e[35m"
|
||||
|
||||
|
||||
## 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)."
|
||||
@ -193,8 +206,8 @@ while getopts ':f:r:i:46hxl:' PARAMS; do
|
||||
done
|
||||
|
||||
# Log beginning of script
|
||||
echo -e "\e[1;32m[`date +%Y-%m-%d` `date +%H:%M:%S`] -- Start CloudFlare" \
|
||||
"DDNS script execution --\e[0m" >> $logFile
|
||||
echo -e "${bold}${note}${stamp} -- Start CloudFlare" \
|
||||
"DDNS script execution --${normal}" >> "$logFile"
|
||||
|
||||
# Check validity of parameters
|
||||
if [ -z "$accountFile" ] || [[ $accountFile == -* ]]; then
|
||||
@ -208,17 +221,17 @@ fi
|
||||
# Check if curl is installed
|
||||
command -v curl >> /dev/null
|
||||
curlResult=$(echo "$?")
|
||||
if [ $curlResult -ne 0 ]; then
|
||||
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 "${info}${stamp} Script running in" \
|
||||
"IP4 mode${normal}" >> "$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 "${info}${stamp} Script running in" \
|
||||
"IP6 mode${normal}" >> "$logFile"
|
||||
fi
|
||||
|
||||
|
||||
@ -227,9 +240,9 @@ mapfile -t cfDetails < "$accountFile"
|
||||
|
||||
## Get current IP address, if not provided in parameters
|
||||
if [ -z "$ipAddress" ]; then
|
||||
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
|
||||
echo -e "${info}${stamp} No IP address for" \
|
||||
"update provided. Detecting this machine's IP address...${normal}" \
|
||||
>> "$logFile"
|
||||
if [ $ip4 -eq 1 ]; then
|
||||
ipAddress=$(curl -s http://ipv4.icanhazip.com)
|
||||
elif [ $ip6 -eq 1 ]; then
|
||||
@ -241,23 +254,21 @@ if [ -z "$ipAddress" ]; then
|
||||
quit 201
|
||||
fi
|
||||
fi
|
||||
echo -e "\e[1;36m[`date +%Y-%m-%d` `date +%H:%M:%S`] Using IP address:" \
|
||||
"$ipAddress" >> $logFile
|
||||
echo -e "${info}${stamp} [INFO] Using IP address:" \
|
||||
"$ipAddress" >> "$logFile"
|
||||
|
||||
|
||||
## Check if desired record(s) exist at CloudFlare
|
||||
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 "[`date +%Y-%m-%d` `date +%H:%M:%S`] (IP4: ${dnsRecords[*]})" \
|
||||
>> $logFile
|
||||
echo -e "${normal}${stamp} (IP4 lookup: ${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 "[`date +%Y-%m-%d` `date +%H:%M:%S`] (IP6: ${dnsRecords[*]})" \
|
||||
>> $logFile
|
||||
echo -e "${normal}${stamp} (IP6 lookup: ${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
|
||||
@ -271,9 +282,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[`date +%Y-%m-%d` `date +%H:%M:%S`] ***" \
|
||||
echo -e "${err}${stamp} -- [INFO]" \
|
||||
"${dnsRecords[recordIdx]} not found in your" \
|
||||
"CloudFlare DNS records***\e[0m" >> $logFile
|
||||
"CloudFlare DNS records --${normal}" >> "$logFile"
|
||||
# remove the entry from the dnsRecords array
|
||||
unset dnsRecords[$recordIdx]
|
||||
# remove the entry from the records array
|
||||
@ -291,9 +302,9 @@ if [ -z ${dnsRecords} ]; then
|
||||
quit 104
|
||||
else
|
||||
for recordIdx in "${!cfRecords[@]}"; do
|
||||
echo -e "\e[1;39m[`date +%Y-%m-%d` `date +%H:%M:%S`] Found" \
|
||||
"${dnsRecords[recordIdx]} (Index: $recordIdx):\e[0m" \
|
||||
>> $logFile
|
||||
echo -e "${normal}${stamp} Found" \
|
||||
"${dnsRecords[recordIdx]} (Index: $recordIdx)" \
|
||||
>> "$logFile"
|
||||
done
|
||||
fi
|
||||
|
||||
@ -304,23 +315,23 @@ for recordIdx in "${!cfRecords[@]}"; do
|
||||
grep -Po '(?<="content":")[^"]*'))
|
||||
recordID+=($(echo "${cfRecords[recordIdx]}" | \
|
||||
grep -Po '(?<="id":")[^"]*'))
|
||||
echo -e "\e[0m[`date +%Y-%m-%d` `date +%H:%M:%S`] Index $recordIdx:" \
|
||||
"For record ${dnsRecords[recordIdx]}" \
|
||||
echo -e "${normal}${stamp} Index $recordIdx:" \
|
||||
"For record ${lit}${dnsRecords[recordIdx]}${normal}" \
|
||||
"with ID: ${recordID[recordIdx]}" \
|
||||
"the current IP is \e[1;35m ${currentIP[recordIdx]}" \
|
||||
"\e[0m" >> $logFile
|
||||
"the current IP is ${lit}${currentIP[recordIdx]}" \
|
||||
"${normal}" >> "$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[1;32m[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"${dnsRecords[recordIdx]} is up-to-date.\e[0m" \
|
||||
>> $logFile
|
||||
echo -e "${bold}${ok}${stamp} -- [STATUS]" \
|
||||
"${dnsRecords[recordIdx]} is up-to-date.${normal}" \
|
||||
>> "$logFile"
|
||||
else
|
||||
echo -e "\e[0;31m[`date +%Y-%m-%d` `date +%H:%M:%S`]" \
|
||||
"${dnsRecords[recordIdx]} needs updating...\e[0m" \
|
||||
>> $logFile
|
||||
echo -e "${lit}${stamp} -- [STATUS]" \
|
||||
"${dnsRecords[recordIdx]} needs updating...${normal}" \
|
||||
>> "$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\"}")
|
||||
@ -330,12 +341,9 @@ for recordIdx in "${!currentIP[@]}"; do
|
||||
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
|
||||
echo -e "${bold}${ok}${stamp} -- [SUCCESS]" \
|
||||
"${dnsRecords[recordIdx]} updated.${normal}" >> "$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
|
||||
|
Loading…
Reference in New Issue
Block a user