Dyn.com stopped providing free dyndns hosting some time ago. It were sad news.
However most necessary functionality can be implemented in 70 lines of code.
To dynamically update dns records we must find a way to:
- get current ip
- update DNS record
- schedule script for execution
1. Getting current ip address
Many ISP use NAT for security and money saving reasons. Also NAS used in routers. So, to get your IP address you need to ask your IP address from service located outside your network.
For example internet.yandex.ru web page ask resource http://ipv4.internet.yandex.ru/api/v0/ip to determine IP address. So, let’s use it.
1 2 3 4 5 6 |
use constant IP_API_URI => 'http://ipv4.internet.yandex.ru/api/v0/ip'; sub get_ip($) { my ($ua) = @_; my $response = get($ua, IP_API_URI); return substr $response, 1, length($response)-2; #clean out quotes } |
2. Updating DNS record
Yandex provide API for DNS records management for domains parked in yandex. API reference should be located here but link is currently broken :-(. UPD: API reference.
In two words you should send POST request with token in header and data in request’s body to https://pddimp.yandex.ru/api2/admin/dns/edit url.
It should looks like following snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
use constant DNS_API_URI_PREFIX => 'https://pddimp.yandex.ru/api2/admin/dns/'; sub post($$$%){ my ($ua, $url, $data, %headers) = @_; my $req = HTTP::Request->new(POST => $url); $req->header(%headers) if keys %headers; $req->content_type('application/x-www-form-urlencoded'); $req->content($data); my $res = $ua->request($req); return $res->content if $res->is_success; return undef; } sub set_domain_ip($$$$$$){ my ($ua, $token, $domain, $subdomain, $record_id , $ip) = @_; return decode_json post($ua, DNS_API_URI_PREFIX."edit", "domain=$domain&record_id=$record_id&subdomain=$subdomain&ttl=14400&content=$ip", PddToken => $token); } |
3. Scheduling dns records updating
Use the cron, Luke :-) I mean that adding following line to cronjobs would be a simplies way to schedule updating of our dns records:
1 |
* * * * * ( token=123 /home/user/yandex_dns.pl --domain=domain.com --subdomain=subdomain ) |
That’s all. Complete script on github.