Updated logging with colour variables and logging tags

This commit is contained in:
Asif Bacchus 2018-09-25 18:17:30 -06:00
parent 92519055fc
commit 8a0362a6e1

View File

@ -133,6 +133,7 @@ stamp="${stamp}"
normal="\e[0m" normal="\e[0m"
bold="\e[1m" bold="\e[1m"
default="\e[39m" default="\e[39m"
ok="\e[32m"
err="\e[31m" err="\e[31m"
info="\e[96m" info="\e[96m"
lit="\e[93m" lit="\e[93m"
@ -205,8 +206,8 @@ while getopts ':f:r:i:46hxl:' PARAMS; do
done done
# Log beginning of script # Log beginning of script
echo -e "\e[1;32m${stamp} -- Start CloudFlare" \ echo -e "${bold}${note}${stamp} -- Start CloudFlare" \
"DDNS script execution --\e[0m" >> "$logFile" "DDNS script execution --${normal}" >> "$logFile"
# Check validity of parameters # Check validity of parameters
if [ -z "$accountFile" ] || [[ $accountFile == -* ]]; then if [ -z "$accountFile" ] || [[ $accountFile == -* ]]; then
@ -220,17 +221,17 @@ fi
# Check if curl is installed # Check if curl is installed
command -v curl >> /dev/null command -v curl >> /dev/null
curlResult=$(echo "$?") curlResult=$(echo "$?")
if [ $curlResult -ne 0 ]; then if [ "$curlResult" -ne 0 ]; then
quit 2 quit 2
fi fi
# Log operating mode # Log operating mode
if [ $ip4 -eq 1 ]; then if [ $ip4 -eq 1 ]; then
echo -e "\e[1;36m${stamp} Script running in" \ echo -e "${info}${stamp} Script running in" \
"IP4 mode\e[0m" >> "$logFile" "IP4 mode${normal}" >> "$logFile"
elif [ $ip6 -eq 1 ]; then elif [ $ip6 -eq 1 ]; then
echo -e "\e[1;36m ${stamp} Script running in" \ echo -e "${info}${stamp} Script running in" \
"IP6 mode\e[0m" >> "$logFile" "IP6 mode${normal}" >> "$logFile"
fi fi
@ -239,8 +240,8 @@ 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[1;36m${stamp} No IP address for" \ echo -e "${info}${stamp} No IP address for" \
"update provided. Detecting this machine's IP address..." \ "update provided. Detecting this machine's IP address...${normal}" \
>> "$logFile" >> "$logFile"
if [ $ip4 -eq 1 ]; then if [ $ip4 -eq 1 ]; then
ipAddress=$(curl -s http://ipv4.icanhazip.com) ipAddress=$(curl -s http://ipv4.icanhazip.com)
@ -253,22 +254,20 @@ if [ -z "$ipAddress" ]; then
quit 201 quit 201
fi fi
fi fi
echo -e "\e[1;36m${stamp} Using IP address:" \ echo -e "${info}${stamp} [INFO] Using IP address:" \
"$ipAddress" >> "$logFile" "$ipAddress" >> "$logFile"
## Check if desired record(s) exist at CloudFlare ## Check if desired record(s) exist at CloudFlare
echo -e "\e[0m${stamp} Performing CloudFlare" \
"lookup on specified DNS records...\e[0m" >> "$logFile"
# 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 "${stamp} (IP4: ${dnsRecords[*]})" \ echo -e "${normal}${stamp} (IP4 lookup: ${dnsRecords[*]})" \
>> "$logFile" >> "$logFile"
for cfLookup in "${dnsRecords[@]}"; do 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")") 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
elif [ $ip6 -eq 1 ]; then elif [ $ip6 -eq 1 ]; then
echo -e "${stamp} (IP6: ${dnsRecords[*]})" \ echo -e "${normal}${stamp} (IP6 lookup: ${dnsRecords[*]})" \
>> "$logFile" >> "$logFile"
for cfLookup in "${dnsRecords[@]}"; do 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")") 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")")
@ -283,9 +282,9 @@ fi
for recordIdx in "${!cfRecords[@]}"; do 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${stamp} ***" \ echo -e "${err}${stamp} -- [INFO]" \
"${dnsRecords[recordIdx]} not found in your" \ "${dnsRecords[recordIdx]} not found in your" \
"CloudFlare DNS records***\e[0m" >> "$logFile" "CloudFlare DNS records --${normal}" >> "$logFile"
# 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
@ -303,8 +302,8 @@ if [ -z ${dnsRecords} ]; then
quit 104 quit 104
else else
for recordIdx in "${!cfRecords[@]}"; do for recordIdx in "${!cfRecords[@]}"; do
echo -e "\e[1;39m${stamp} Found" \ echo -e "${normal}${stamp} Found" \
"${dnsRecords[recordIdx]} (Index: $recordIdx):\e[0m" \ "${dnsRecords[recordIdx]} (Index: $recordIdx)" \
>> "$logFile" >> "$logFile"
done done
fi fi
@ -316,22 +315,22 @@ for recordIdx in "${!cfRecords[@]}"; do
grep -Po '(?<="content":")[^"]*')) grep -Po '(?<="content":")[^"]*'))
recordID+=($(echo "${cfRecords[recordIdx]}" | \ recordID+=($(echo "${cfRecords[recordIdx]}" | \
grep -Po '(?<="id":")[^"]*')) grep -Po '(?<="id":")[^"]*'))
echo -e "\e[0m${stamp} Index $recordIdx:" \ echo -e "${normal}${stamp} Index $recordIdx:" \
"For record ${dnsRecords[recordIdx]}" \ "For record ${lit}${dnsRecords[recordIdx]}${normal}" \
"with ID: ${recordID[recordIdx]}" \ "with ID: ${recordID[recordIdx]}" \
"the current IP is \e[1;35m ${currentIP[recordIdx]}" \ "the current IP is ${lit}${currentIP[recordIdx]}" \
"\e[0m" >> "$logFile" "${normal}" >> "$logFile"
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[1;32m${stamp}" \ echo -e "${bold}${ok}${stamp} -- [STATUS]" \
"${dnsRecords[recordIdx]} is up-to-date.\e[0m" \ "${dnsRecords[recordIdx]} is up-to-date.${normal}" \
>> "$logFile" >> "$logFile"
else else
echo -e "\e[0;31m${stamp}" \ echo -e "${lit}${stamp} -- [STATUS]" \
"${dnsRecords[recordIdx]} needs updating...\e[0m" \ "${dnsRecords[recordIdx]} needs updating...${normal}" \
>> "$logFile" >> "$logFile"
if [ $ip4 -eq 1 ]; then if [ $ip4 -eq 1 ]; then
# update record at CloudFlare with new IP # update record at CloudFlare with new IP
@ -342,12 +341,9 @@ for recordIdx in "${!currentIP[@]}"; do
fi fi
# 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${stamp}" \ echo -e "${bold}${ok}${stamp} -- [SUCCESS]" \
"${dnsRecords[recordIdx]} updated.\e[0m" >> "$logFile" "${dnsRecords[recordIdx]} updated.${normal}" >> "$logFile"
else else
echo -e "\e[1;31m${stamp}" \
"${dnsRecords[recordIdx]} update failed\e[0m" >> "$logFile"
echo -e "\e[0;39m$update\e[0m" >> "$logFile"
failedDNS+=("${dnsRecords[recordIdx]}") failedDNS+=("${dnsRecords[recordIdx]}")
fi fi
fi fi