Homemade Dynamic DNS
This article is about how to make 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 the domain to the Yandex service - pdd.yandex.ru/domains_add .
Next, we get a token for our domain, for this we need to go to the link of the form:
pddimp.yandex.ru/get_token.xml?domain_name=example-site.ru
(example-site.ru is the domain name).
They got a token - something like "gjkgwrth34wjh45kj2th234jkht34234lkj5."
Next, we follow the link of the form:
pddimp.yandex.ru/nsapi/get_domain_records.xml?token=gjkgwrth34wjh45kj2th234jkht34234lkj5&domain=example-site.ru
We get a list of DNS zones of our domain in XML format, something like the following:
From this response, we need the id attributes of the record tags.
Using all this, we can write a perl script to update the IP.
It remains only to write a line like
* / 5 * * * * root perl /root/update_my_ip.pl in / etc / crontab
(for updating every 5 minutes, update_my_ip.pl is the name of our script).
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 the domain to the Yandex service - pdd.yandex.ru/domains_add .
Next, we get a token for our domain, for this we need to go to the link of the form:
pddimp.yandex.ru/get_token.xml?domain_name=example-site.ru
(example-site.ru is the domain name).
They got a token - something like "gjkgwrth34wjh45kj2th234jkht34234lkj5."
Next, we follow the link of the form:
pddimp.yandex.ru/nsapi/get_domain_records.xml?token=gjkgwrth34wjh45kj2th234jkht34234lkj5&domain=example-site.ru
We get a list of DNS zones of our domain in XML format, something like the following:
example-site.ru 3.5.7.9 3.5.7.9 ok
From this response, we need the id attributes of the record tags.
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 = ;
$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 write a line like
* / 5 * * * * root perl /root/update_my_ip.pl in / etc / crontab
(for updating every 5 minutes, update_my_ip.pl is the name of our script).