自家製のダイナミックDNS

Perl、Yandex DNS API、およびD-Linkルーターを使用して動的DNSを数分で作成する方法に関する記事。



多くのD-Linkルーターは、組み込みのダイナミックDNS機能をサポートしています。

残念ながら、無料で利用できるのはexample.dlinkddns.comのようなドメインのみです。



Yandexの非常に便利なDNS APIもあります



この組み合わせを使用します。



まず、ドメインをYandexサービス( pdd.yandex.ru/domains_add)に接続する必要があります。

次に、ドメインのトークンを取得します。これには、フォームのリンクをクリックする必要があります。

pddimp.yandex.ru/get_token.xml?domain_name=example-site.ru

(example-site.ru-ドメイン名)。



「gjkgwrth34wjh45kj2th234jkht34234lkj5」のようなトークンを受け取りました。

次-フォームのリンクをたどってください:

pddimp.yandex.ru/nsapi/get_domain_records.xml?token=gjkgwrth34wjh45kj2th234jkht34234lkj5&domain=example-site.ru

ドメインのDNSゾーンレコードのリストを次のようなXML形式で取得します。



<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>
      
      







この応答から、レコードタグのid属性が必要です。



これらすべてを使用して、perlスクリプトを記述して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; } }
      
      







/ etc / crontabのような行を書くだけです

* / 5 * * * * root perl /root/update_my_ip.pl

(5分ごとに更新する場合、update_my_ip.plはスクリプトの名前です)。



All Articles