I whipped up a profile that I'm fairly pleased with... since Google Domains just added dynamic DNS support (as well as an API for updating the records), I created a Tasker profile to automatically update a DynDNS record for my phone whenever it's connected to Wifi. It's not a particularly useful profile (unless you plan on hosting a website on your phone? I'm not going to judge) but it was a fun challenge to put together. Besides, it's been a while since I've whipped up a profile from scratch.
I also figured this was a good time to start using that domain name I purchased for something - a wild blog post appears!
Anyhoo, here are the deets:
Line-by-line explanations:
Ask if you have questions or, better yet, give me some tips on what I can use this for.
I also figured this was a good time to start using that domain name I purchased for something - a wild blog post appears!
Anyhoo, here are the deets:
Code:
Profile: DynDNS
Notification: no
State: Wifi Connected [ SSID:* MAC:* IP:* ]
Enter: DDNS
A1: Wait [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ]
A2: HTTP Get [ Server:Port:http://ipv4.icanhazip.com/ Path: Attributes: Cookies: User Agent: Timeout:20 Mime Type: Output File: Trust Any Certificate:Off ]
A3: If [ %HTTPD !~ %PUB_IP ]
A4: Variable Set [ Name:%PUB_IP To:%HTTPD Do Maths:Off Append:Off ]
A5: HTTP Post [ Server:Port:https://username:password@domains.google.com 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 ]
A6: Wait [ MS:0 Seconds:0 Minutes:2 Hours:0 Days:0 ]
A7: Variable Set [ Name:%newline To:
Do Maths:Off Append:Off ]
A8: Run Shell [ Command:nslookup nexusXXXXX.codesplice.net Timeout (Seconds):0 Use Root:Off Store Output In:%NSLOOKUP Store Errors In: Store Result In: ]
A9: Variable Split [ Name:%NSLOOKUP Splitter:%newline Delete Base:Off ]
A10: Notify [ Title:%NSLOOKUP4 Text:%NSLOOKUP5 Icon:hd_ab_navigation_accept Number:0 Permanent:Off Priority:1 ]
A11: Variable Clear [ Name:%NSLOOKUP* Pattern Matching:On ]
A12: End If
Line-by-line explanations:
- 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:password@domains.google.com 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:
- Do Maths:Off Append:Off ]
Set the local %newline variable to a newline character (carriage return - just press Enter on your Android keyboard). This will come in handy shortly. - 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:
Code:Server: google-public-dns-a.google.com Address: 8.8.8.8 Name: nexusXXXXX.codesplice.net Address: 71.228.X.X
- 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
FIN.
Ask if you have questions or, better yet, give me some tips on what I can use this for.