Page 1 of 1

Google Dynamic DNS update script

Posted: Tue Jan 27, 2015 9:00 pm
by hhspiny
This script was adapted from the script that updates entryDNS. tested on routerOS 6.x

# Variables
:local GoogleDNSUsername "xxx"
:local GOogleDNSPassword "xxx"
:local hostName "xxx"
:local GoogleDNSForceUpdate false
:local currentIP ""
:global previousIP

# Script

/tool fetch url="http://myip.dnsomatic.com/" mode=http dst-path="publicip.txt"
:set currentIP [/file get "publicip.txt" contents]
/file remove "publicip.txt"

:if ([:typeof $previousIP] = "nothing") do={ :set previousIP "" }

:if ($currentIP != $previousIP) do={
:set GoogleDNSForceUpdate true
:set previousIP $currentIP
}

:if ($GoogleDNSForceUpdate) do={
:do {
/tool fetch url="https://$GoogleDNSUsername:$GOogleDNSPa ... $currentIP" mode=https keep-result=no
:log info ("GoogleDNS Updated: current IP = $currentIP")
} on-error={
:log error ("GoogleDNS: Failed Updating")
}
}

Re: Google Dynamic DNS update script

Posted: Sun Mar 29, 2015 1:02 am
by craigthom
Here is my modification, which uses /ip cloud to get the current address, eliminates the global variable for for the previous address by using :resolve, and captures and logs the results from Google.
# Variables
# Username and password are the long cryptic ones Google Domains provides,
# not your regular Google account ones
:local GoogleDNSUsername "yourDNSusername"
:local GoogleDNSPassword "yourDNSpassowrd"
:local hostName "yourhostname"
:local currentIP ""
:local setResults ""
:local previousIP ""

# Script

:set currentIP [/ip cloud get public-address]
:set previousIP [:resolve "$hostName"]

:if ($currentIP != $previousIP) do={
:do {
/tool fetch url="https://$GoogleDNSUsername:$GoogleDNSPassword@domains.google.com/nic/update?hostname=$hostName&myip=$currentIP" mode=https dst-path=GoogleDNS.txt
:set setresult [/file get GoogleDNS.txt contents];
:log info "GoogleDNS said this: $setResults"
} on-error={ 
:log error ("GoogleDNS: script failed to set new IP address") 
}
}

Re: Google Dynamic DNS update script

Posted: Tue Oct 25, 2016 12:13 am
by bjm904
For those of you coming from google. The script posted by craigthom has a few typos. Below is the working script:
# Variables
# Username and password are the long cryptic ones Google Domains provides,
# not your regular Google account ones
:local GoogleDNSUsername "yourDNSusername"
:local GoogleDNSPassword "yourDNSpassowrd"
:local hostName "yourhostname"
:local currentIP ""
:local setResults ""
:local previousIP ""

# Script
:set currentIP [/ip cloud get public-address]
:set previousIP [:resolve "$hostName"]

:if ($currentIP != $previousIP) do={
:do {
/tool fetch url="https://$GoogleDNSUsername:$GoogleDNSPassword@domains.google.com/nic/update?hostname=$hostName&myip=$currentIP" mode=https dst-path=GoogleDNS.txt
:set setResults [/file get GoogleDNS.txt contents];
:log info ("GoogleDNS said this: $setResults")
} on-error={ 
:log error ("GoogleDNS: script failed to set new IP address") 
}
}

Re: Google Dynamic DNS update script

Posted: Mon Oct 23, 2017 7:42 am
by jo2jo
This is an EXCELLENT script, thanks to OP for the main script (+others who have made changes and posted here)!!

my version/changes are tested working on rOS 6.40.4 and with Google Domains (Synthetic Records -> Dynamic DNS).

below are some changes that I made that will maybe help others (it does what i specifically need it to do, that is grab current public IP from a DHCP-CLIENT interface, strip off the 3-char subnet, and securely reports that to your google domains dynDNS). In the snipit below, im "finding" the dhcp-client via a Comment i added to that dhcpClient (i use a preset comment, incase i ever change the ether3 interface's name, at a later date, this script will still keep-on working )

note: alot of the addresses that you GET from ROS have the subnet appened to them, ie the /ip dhcp-client get command returns: 5.3.2.6/21 (so the code below STRIPPS OFF that /21 , or whatever subnet , IF it exists in your case)

My changes to OP script start at:
# get the current.....

and end at:
#:log info ("currentIP........

DONT FORGET TO ALSO ADD THIS SCRIPT TO /SYS SCHEDULER SO THAT IT AUTO RUNS ON AN INTERVAL, example provided at bottom

# credit to hhspiny
#
# Variables
# Username and password are long cryptic ones Google Domains generates for your dynDomain,
# IT IS NOT your regular Google account Username and Password
# 
:local GoogleDNSUsername "xxFILL_IN_YOURSxx"
:local GoogleDNSPassword "xxFILL_IN_YOURSxx"
:local hostName "xxxFILL_IN_YOURS_full__ex__DYN.MYDOMAIN.COMxxx"
:local currentIP ""
:local setResults ""
:local previousIP ""

# get the current IP address from a dhcp-client identified by a preset COMMENT FILL THIS IN BELOW
:set currentIP [/ip dhcp-client get [find comment=XXXyourCommentXXX] address]
# Strip netmask
:for i from=( [:len $currentIP] - 1) to=0 step=-1 do={ 
:if ( [:pick $currentIP $i] = "/") do={ 
:set currentIP [:pick $currentIP 0 $i]
} 
}

# comment OUT line below for DEBUGGING your GET IP ADDRESS command
#:log info ("currentIP_to_SEND_to_google_DEBUGGING: $currentIP")

:set previousIP [:resolve "$hostName"]

:if ($currentIP != $previousIP) do={
:do {
/tool fetch url="https://$GoogleDNSUsername:$GoogleDNSPassword@domains.google.com/nic/update?hostname=$hostName&myip=$currentIP" mode=https dst-path=GoogleDNS.txt
:set setResults [/file get GoogleDNS.txt contents];
:log info ("GoogleDNS said this: $setResults")
} on-error={ 
:log error ("GoogleDNS: script failed to set new IP address") 
}
}

scheduler example, to auto run this every 5m
/system scheduler add interval=10m name=SCHgoogleDYNdns on-event=YOURdynSCRIPTname

Re: Google Dynamic DNS update script

Posted: Thu Jul 18, 2019 11:12 pm
by tugsynz
Hi Team,

I have used this script, I keep getting a reply stating its a "badagent"

Have I missed something in the script?

Thanks,
B

Re: Google Dynamic DNS update script

Posted: Mon May 18, 2020 6:39 pm
by ch85
How if I have multiple WAN and want to use different Google DDNS for each WAN.. Is it possible? And if it is, how it can be done? Thanks :)

Re: Google Dynamic DNS update script

Posted: Sat Jul 11, 2020 9:14 am
by RafaG
Hi,
I have used this script, I keep getting a reply stating its a "badagent"
Similar problem for me, I use:
/tool fetch http-header-field="User-Agent: Mikrotik" url="https://$GoogleDNSUsername:$GoogleDNSPassword@domains.google.com/nic/update?hostname=$hostName&myip=$currentIP" mode=https dst-path=GoogleDNS.txt
And thanks a lot for this script!

RafaG