mydns.jp 用に IP更新 スクリプトを作成した。
IPv4 と IPv6に対応した perl スクリプトです。
#!/usr/bin/perl
use Net::INET6Glue::INET_is_INET6;
use LWP::UserAgent;
use strict;
use warnings;
use Socket;
use Socket6;
use Time::Piece;
#MyDNS アカウント ご自分のアカウントに修正して下さい。
my $user = 'user';
my $pass = 'pass';
my $host = 'xxx.server-on.net';
my $ua = LWP::UserAgent->new;
#最終更新日取得(7日更新無しで強制更新)
if( -e "mydnslastmodify.dat" )
{
open(FH, "<mydnslastmodify.dat");
my $lastnow = localtime(<FH> + 3600*24*7 );
close(FH);
my $now = localtime(time);
if( $now > $lastnow )
{
&updateipv4;
&updateipv6;
exit;
}
}
else
{
&updateipv4;
&updateipv6;
exit;
}
#登録 DNSアドレス取得(ipv4)
my ($name, $aliases, $addrtype, $length, @ipaddr) = gethostbyname($host);
my $address;
foreach (@ipaddr) {
$address = join '.', unpack('C4', $_);
}
sub updateipv4 {
#IPv4
my $req = HTTP::Request->new(GET =>
'http://ipv4.mydns.jp/login.html');
$req->authorization_basic($
$ua->request($req)->as_string;
print "IPv4 update \n";
#更新日保存
open(OUT, ">mydnslastmodify.dat");
print OUT time;
close(OUT);
}
sub updateipv6 {
#IPv6
my $req = HTTP::Request->new(GET =>
'http://ipv6.mydns.jp/login.html');
$req->authorization_basic($user, $pass);
$ua->request($req)->as_string;
print "IPv6 update \n";
#更新日保存
open(OUT, ">mydnslastmodify.dat");
print OUT time;
close(OUT);
}
#実態アドレス取得更新(ipv4)
my $reqA = HTTP::Request->new(GET => 'http://ipv4.mydns.jp/?MENU=400');
my $respA = $ua->request($reqA)->as_string;
my $remote_address;
if($respA =~ /(You access from : )(.*?)(<\/)/s)
{
$remote_address = $2;
if( $address eq $remote_address )
{
print "equal |$address| |$remote_address|\n";
}
else
{
print "not equal |$address| |$remote_address|\n";
&updateipv4;
}
}
else
{
print "Can't find IPv4 Address!!\n";
}
#print "--------------------ipv6----------------------\n";
#登録 DNSアドレス取得(ipv6)
my @getaddr = getaddrinfo($host, 0);
my $netaddr6 = (unpack_sockaddr_in6($getaddr[3]))[1];
my $address6 = inet_ntop($getaddr[0], $netaddr6);
#実態アドレス取得更新(ipv6)
my $reqB = HTTP::Request->new(GET => 'http://ipv6.mydns.jp/?MENU=400');
my $respB = $ua->request($reqB)->as_string;
if($respB =~ /(You access from : )(.*?)(<\/)/s)
{
$remote_address = $2;
if( $address6 eq $remote_address )
{
print "equal |$address6| |$remote_address|\n";
}
else
{
print "not equal |$address6| |$remote_address|\n";
&updateipv6;
}
}
else
{
print "Can't find IPv6 Address!!\n";
}
#
# Needs perl UserAgent
# ports install
# /usr/ports/www/p5-libwww
# /usr/ports/net/p5-Socket6
# /usr/ports/net/p5-Net-INET6Glue
#