This immediately got me thinking about Tasker, and I set to work on a Tasker-driven method for automatically updating a DDNS record for my phone. This serves no real purpose for me (yet) but it was a bit of fun to work out.
This page has the full details on how to create a Dynamic DNS synthetic record on your Google Domains-registered domain. Each of these dynamic synthetic records will have a subdomain/hostname (that you specify) as well as an auto-generated username+password pair. Once created, you should be able to put the pieces together to make an HTTP request that looks like this:
That's the easy part. The fun part is getting it into Tasker:
Note: This profile doesn't require root but it does depend upon busybox to provide the "nslookup" command.
Don't worry, I'll break this description down a bit by line number - and maybe even include some images if you're nice:
- Profile: DynDNS
The name given to the profile.
- Notification: no
This profile will be a "fire-and-forget" sort of thing, so I don't want an ongoing notification when it's active.
- State: Wifi Connected [ SSID:* MAC:* IP:* ]
I'm going to attempt to update DynDNS when connected to Wifi.
- Enter: DDNS
Name of the Enter (and only) Task.
- A1: Wait [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ]
Wait 5 seconds after connecting to Wifi (to allow time to receive local DHCP IP and establish Internet connection).
- A2: HTTP Get [ Server:Port:http://ipv4.icanhazip.com/ Path: Attributes: Cookies: User Agent: Timeout:20 Mime Type: Output File: Trust Any Certificate:Off ]
Connect to a dead-simple service for resolving the phone's public-facing IP. icanhazip.com returns just the IP address in plain-text with no filler, which means I don't have to do any crazy string parsing (yay!). Tasker automatically stores the returned data of an HTTP GET request into the global variable %HTTPD. Only the Server:Port field is required.
- A3: If [ %HTTPD !~ %PUB_IP ]
I don't want to run the rest of the task (and submit repeated DynDNS updates) if the current public IP matches what I've already registered. I store the current IP in the %PUB_IP variable. For this If statement, I'm just checking to see if the HTTP GET return does not match the value of %PUB_IP. If TRUE (if it doesn't match), I run the rest of the Task; if FALSE (if it does match), I just skip to the end.
- A4: Variable Set [ Name:%PUB_IP To:%HTTPD Do Maths:Off Append:Off ]
Store the public IP as %PUB_IP.
- A5: HTTP Post [ Server:Port:https://username:firstname.lastname@example.org Path:/nic/update?hostname=nexusXXXXX.codesplice.net&myip=%PUB_IP Data / File: Cookies: User Agent: Timeout:10 Content Type: Output File: Trust Any Certificate:Off ]
This is the heavy lifter. Username and password should be the autogenerated values obtained from Google's Dynamic DNS Synthetic Records Creator Thing - NOT your Google credentials (pls). Replace nexusXXXXX.codesplice.net with your DynDNS record's subdomain (duh).
- A6: Wait [ MS:0 Seconds:0 Minutes:2 Hours:0 Days:0 ]
I'm feeling patient, so let's wait two minutes before we check to see if the DNS change has propagated.
- A7: Variable Set [ Name:%newline To:
- A8: Run Shell [ Command:nslookup nexusXXXXX.codesplice.net Timeout (Seconds):0 Use Root:Off Store Output In:%NSLOOKUP Store Errors In: Store Result In: ]
Run the nslookup command against the hopefully-updated DynDNS record and store the returned data (command output) in %NSLOOKUP. The output of nslookup will look something like this:
- A9: Variable Split [ Name:%NSLOOKUP Splitter:%newline Delete Base:Off ]
Split the variable %NSLOOKUP into multiple variables at each newline character. We're really only interested in the last two lines of the nslookup output.
- A10: Notify [ Title:%NSLOOKUP4 Text:%NSLOOKUP5 Icon:hd_ab_navigation_accept Number:0 Permanent:Off Priority:1 ]
Create a low-priority notification to display the name of the DynDNS record (%NSLOOKUP4) and the public-facing IP (%NSLOOKUP5).
- A11: Variable Clear [ Name:%NSLOOKUP* Pattern Matching:On ]
Clean up some of the variable mess.
- A12: End If
So now, my phone will have a nice dynamic DNS record any time that it's connected to a Wifi network. What will I do with this? Not a clue! But hey, it was a fun little project.