📜 ⬆️ ⬇️

Homemade Dynamic DNS

An article about how to do Dynamic DNS in a few minutes using Perl, Yandex DNS API and D-Link router.

Many D-Link routers support the built-in Dynamic DNS feature.
Unfortunately, only domains like example.dlinkddns.com are available for free.

There is also a very convenient DNS API from Yandex.
')
We will use this combination.

First you need to connect your domain to the Yandex service - pdd.yandex.ru/domains_add .
Next we get a token for our domain, for this you need to follow the link of the form:
pddimp.yandex.ru/get_token.xml?domain_name=example-site.ru
(example-site.ru - domain name).

Got a token - something like "gjkgwrth34wjh45kj2th234jkht34234lkj5".
Next - go to the link of the form:
pddimp.yandex.ru/nsapi/get_domain_records.xml?token=gjkgwrth34wjh45kj2th234jkht34234lkj5&domain=example-site.ru
We get the list of DNS-zone records of our domain in XML format, something like the following:

<page> <domains> <domain> <name>example-site.ru</name> <response> <record domain="example-site.ru" priority="" ttl="21600" subdomain="@" type="A" id="23232301">3.5.7.9</record> <record domain="www.example-site.ru" priority="" ttl="21600" subdomain="www" type="A" id="23232302">3.5.7.9</record> </response> </domain> <error>ok</error> </domains> </page> 


From this response, we need the attributes of the id tag record.

Using all this, we can write a perl script to update the IP.

 use LWP::UserAgent; my $hostout = `host example.dlinkddns.com`; //   if ($hostout =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) { my $ip = "$1.$2.$3.$4"; #: open (FILE,"my_ip.txt"); my @lines = <FILE>; $old_ip = $lines[0]; # IP   $old_ip =~ s/^\s+|\s+$//g; #trim close(FILE); if ($old_ip eq $ip) { die "IP not changed"; #   ,  IP   } open (FILE,">my_ip.txt"); print FILE $ip; #     IP close(FILE); my $token = "gjkgwrth34wjh45kj2th234jkht34234lkj5"; my $domain00 = "example-site.ru"; my $id00 = "23232301"; my $subdomain01 = "www"; my $id01 = "23232302"; my $url00 ="https://pddimp.yandex.ru/nsapi/edit_a_record.xml?token=$token&domain=$domain00&record_id=$id00&content=$ip"; my $url01 ="https://pddimp.yandex.ru/nsapi/edit_a_record.xml?token=$token&domain=$domain00&subdomain=$subdomain01&record_id=$id01&content=$ip"; my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } ); my $response = $ua->get($url00); if ( $response->is_success ) { print $response->decoded_content; } else { die $response->status_line; } my $response = $ua->get($url01); if ( $response->is_success ) { print $response->decoded_content; } else { die $response->status_line; } } 


It remains only to register in / etc / crontab a line like
* / 5 * * * * root perl /root/update_my_ip.pl
(for updating every 5 minutes, update_my_ip.pl is the name of our script).

Source: https://habr.com/ru/post/239465/


All Articles