Updated godaddy-ddns.sh script

This commit is contained in:
Matthew McKinnon 2018-07-05 11:44:53 +10:00
parent 986cb7f1d4
commit 75935219c9
2 changed files with 96 additions and 56 deletions

0
README.md Executable file → Normal file
View File

View File

@ -1,72 +1,112 @@
#!/bin/bash #!/bin/bash
# GoDaddy.sh v1.0 by Nazar78 @ TeaNazaR.com
# This script is used to check and update your GoDaddy DNS server to the IP address of your current internet connection. ###########################################
# Special thanks to mfox for his ps script # Simple DDNS script to update GoDaddy's DNS. Just schedule every 5mins in crontab.
# https://github.com/markafox/GoDaddy_Powershell_DDNS # With options to run scripts/programs/commands on update failure/success.
# #
# First go to GoDaddy developer site to create a developer account and get your key and secret # Requirements:
# - Bash - On LEDE/OpenWRT, opkg install bash
# - curl CLI - On Debian, apt-get install curl
# #
# https://developer.godaddy.com/getstarted # History:
# Be aware that there are 2 types of key and secret - one for the test server and one for the production server # v1.0 - 20160513 - 1st release.
# Get a key and secret for the production server
# #
# # PS: Feel free to distribute but kindly retain the credits (-:
#Create a godaddy_keys file with the lines ###########################################
#
# KEY <godaddy dev API KEY>
# SECRET <godaddy dev SECRET>
#
#
#Update the first 4 variables with your information
# Begin settings
# Get the Production API key/secret from https://developer.godaddy.com/keys/.
# Ensure it's for "Production" as first time it's created for "Test".
#Key=<API production key>
#Secret=<API secret>
MAILTO="support@comprofix.com" Key=A4vTD3PLEX1_G3C4VGGaDiha9BCZZc2ZpP
MAILFROM="support@comprofix.com" Secret=G3C6k2r1kAbKfMHnws3BAs
SMTP="mail.comprofix.com"
domain="comprofix.com" # your domain
name="home" # name of A record to update
key=$(cat /opt/scripts/godaddy_keys | grep KEY | awk '{ print $2 }') # key for godaddy developer API
secret=$(cat /opt/scripts//godaddy_keys | grep SECRET | awk '{ print $2 }') # secret for godaddy developer API
headers="Authorization: sso-key $key:$secret" # Domain to update.
Domain=comprofix.com
# echo $headers # Advanced settings - change only if you know what you're doing :-)
# Record type, as seen in the DNS setup page, default A.
Type=A
result=$(curl -s -X GET -H "$headers" "https://api.godaddy.com/v1/domains/$domain/records/A/$name") # Record name, as seen in the DNS setup page, default @.
Name=home
# echo $result # Time To Live in seconds, minimum default 600 (10mins).
# If your public IP seldom changes, set it to 3600 (1hr) or more for DNS servers cache performance.
TTL=600
dnsIp=$(echo $result | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b") # Writable path to last known Public IP record cached. Best to place in tmpfs.
CachedIP=/tmp/current_ip
# DEBUG: Remove hash from below line # External URL to check for current Public IP, must contain only a single plain text IP.
# echo "dnsIp:" $dnsIp # Default http://api.ipify.org.
CheckURL=http://api.ipify.org
# Get public ip address there are several websites that can do this. # Optional scripts/programs/commands to execute on successful update. Leave blank to disable.
ret=$(curl -s GET "http://ipinfo.io/json") # This variable will be evaluated at runtime but will not be parsed for errors nor execution guaranteed.
currentIp=$(echo $ret | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b") # Take note of the single quotes. If it's a script, ensure it's executable i.e. chmod 755 ./script.
# Example: SuccessExec='/bin/echo "$(date): My public IP changed to ${PublicIP}!">>/var/log/GoDaddy.sh.log'
SuccessExec=''
# DEBUG: Remove hash from below line # Optional scripts/programs/commands to execute on update failure. Leave blank to disable.
# echo "currentIp:" $currentIp # This variable will be evaluated at runtime but will not be parsed for errors nor execution guaranteed.
# Take note of the single quotes. If it's a script, ensure it's executable i.e. chmod 755 ./script.
if [ $dnsIp != $currentIp ]; # Example: FailedExec='/some/path/something-went-wrong.sh ${Update} && /some/path/email-script.sh ${PublicIP}'
then FailedExec=''
# echo "Ips are not equal" # End settings
request='{"data":"'$currentIp'","ttl":600}'
# echo $request
nresult=$(curl -i -s -X PUT \
-H "$headers" \
-H "Content-Type: application/json" \
-d $request "https://api.godaddy.com/v1/domains/$domain/records/A/$name")
# echo $nresult
sendemail -o tls=no -s $SMTP -t $MAILTO -f "$name.$domain <$MAILFROM>" -u "$name.$domain IP has been updated" -m "
$name.$domain IP has been updated
$name.$domain IP is now: $currentIp
" -q
Curl=$(/usr/bin/which curl 2>/dev/null)
Touch=$(/usr/bin/which touch 2>/dev/null)
[ "${Curl}" = "" ] &&
echo "Error: Unable to find 'curl CLI'." && exit 1
[ -z "${Key}" ] || [ -z "${Secret}" ] &&
echo "Error: Requires API 'Key/Secret' value." && exit 1
[ -z "${Domain}" ] &&
echo "Error: Requires 'Domain' value." && exit 1
[ -z "${Type}" ] && Type=A
[ -z "${Name}" ] && Name=@
[ -z "${TTL}" ] && TTL=600
[ "${TTL}" -lt 600 ] && TTL=600
${Touch} ${CachedIP} 2>/dev/null
[ $? -ne 0 ] && echo "Error: Can't write to ${CachedIP}." && exit 1
[ -z "${CheckURL}" ] && CheckURL=http://api.ipify.org
echo -n "Checking current 'Public IP' from '${CheckURL}'..."
PublicIP=$(${Curl} -kLs ${CheckURL})
if [ $? -eq 0 ] && [[ "${PublicIP}" =~ [0-9]{1,3}\.[0-9]{1,3} ]];then
echo "${PublicIP}!"
else
echo "Fail! ${PublicIP}"
eval ${FailedExec}
exit 1
fi fi
if [ "$(cat ${CachedIP} 2>/dev/null)" != "${PublicIP}" ];then
echo -n "Checking '${Domain}' IP records from 'GoDaddy'..."
Check=$(${Curl} -kLsH"Authorization: sso-key ${Key}:${Secret}" \
-H"Content-type: application/json" \
https://api.godaddy.com/v1/domains/${Domain}/records/${Type}/${Name} \
2>/dev/null|jq -r '.[0].data'>/dev/null)
if [ $? -eq 0 ] && [ "${Check}" = "${PublicIP}" ];then
echo -n ${Check}>${CachedIP}
echo -e "unchanged!\nCurrent 'Public IP' matches 'GoDaddy' records. No update required!"
else
echo -en "changed!\nUpdating '${Domain}'..."
Update=$(${Curl} -kLsXPUT -H"Authorization: sso-key ${Key}:${Secret}" \
-H"Content-type: application/json" \
https://api.godaddy.com/v1/domains/${Domain}/records/${Type}/${Name} \
-d "[{\"data\":\"${PublicIP}\",\"ttl\":${TTL}}]" 2>/dev/null)
if [ $? -eq 0 ] && [ "${Update}" = "" ];then
echo -n ${PublicIP}>${CachedIP}
echo "Success!"
eval ${SuccessExec}
else
echo "Fail! ${Update}"
eval ${FailedExec}
exit 1
fi
fi
else
echo "Current 'Public IP' matches 'Cached IP' recorded. No update required!"
fi
exit $?