ダイナミックDo!jpのIPアドレスを更新するスクリプト
#!/usr/bin/perl
# -w ワーニングを出す
# E:\DATA\bsd\dynamic-dns\ddo-jp\test-ddns-ddo-pl-211228.txt
# E:\DATA\bsd\dynamic-dns\ddo-jp\test-ddns-ddo-pl-211228.txt
# 2021/12/28---> ok
# ddnsNowのスクリプトを作ったので、合わせてddo.jpも簡素化
#
#
#
#
use strict; # 宣言した変数以外を使えなくする
my $ddoid="UserID";
my $ddopass="Password";
my $old_ip="/home/User/crontab/ddnsDDo/DDo-G_ip_old.txt";
my $update="/home/User/crontab/ddnsDDo/DDo-update.log";
my $no_update="/home/User/crontab/ddnsDDo/DDo-no_update.log";
my $ip_change_log="/home/User/crontab/ddnsDDo/DDo-ip_change_log.txt";
my $ip_change_status="/home/User/crontab/ddnsDDo/DDo-ip_change_status";
my $ip_kari="/home/User/crontab/ddnsDDo/DDo-G_ip_now.txt";
my $ipaddr ;
my $null="";
my $line;
my $wget;
my $oldipaddr;
my $dmy="/home/User/crontab/ddnsDDo/DDodmy";
$| = 1; # ゼロ以外が代入されると出力をバッファリングしなくなる。
# ########################### メインプログラム ###############################
open FH, "<",$old_ip || die "Error: ipaddr.txt $!\n" ; # 以前のIPアドレスの入ったファイルを入力で開く
$oldipaddr = <FH> ; # 記録されているIPアドレスを入力
close FH;
$ipaddr=&newip(); # 新しいipアドレスを求める
# print "Debug 2\n";
######## IPアドレスを記録した前回と比較 ################
if ( $ipaddr ne $null ){ # IPアドレスがヌルでない場合
#print "debug 4 ipaddr=$ipaddr , oldipaddr=$oldipaddr \n" ;
if ( $ipaddr eq $oldipaddr ) {
&noipchange( $no_update , $oldipaddr); # IPアドレスに変更なし ---------------------------------------------------------------
#print "debug 5 \n" ;
}
else { # IPアドレス変更する
#print "debug 6 \n" ;
$wget=`wget -O $ip_change_status -o $dmy "http://free.ddo.jp/dnsupdate.php?dn=$ddoid&pw=$ddopass"`;
#print "debug 7 \n" ;
&ipchange( $update ,$ipaddr , $old_ip ,$ip_change_log ,$ip_change_status); # IPアドレスを追加書込
}
#print "debug 8 \n" ;
}
else { # 新しいIPアドレスが得られなかった場合
&no_new_ip($no_update);
}
#print "debug 9 \n" ;
exit;
########## メインプログラム end #########################################
# ----------------------------- 新しいIPアドレスが得られなかった --------------------------
sub no_new_ip {
my $no_up=$_[0];
&time_prt($no_up); # 時刻の呼び出しとプリント
open (OUT,">>$no_up");
print OUT "新しいIPアドレスが得られませんでした。\n" ; # 新しいIPアドレスが得られないエラー
close(OUT);
}
# ----------------------------- IPアドレスに変更なし ---------------------------------------
sub noipchange {
my $no_up=@_[0]; #output file name # 引数が一つの場合は$_[0]を、引数が複数の場合は@_[0],@_[1],----@_[n]を使う
my $ip_address=@_[1]; # ip address
# print "Debug no_update file :$no_up\n";
&time_prt($no_up); # 時刻の呼び出しとプリント
open (OUT,">>$no_up");
print OUT "IP address is not changed: $ip_address\n" ; #IPアドレスに変更無し
print OUT "" ;
close(OUT);
}
# ----------------------------- 時刻のプリント ----------------------------------------------
# 2021/12/27 outoput file 名をわかりやすく変更した
# 引数 1つ 出力ファイル名 メインから持ち込む
#
#
sub time_prt {
my $out_file=$_[0]; # output file name # 引数が一つの場合は$_[0]を、引数が複数の場合は@_[0],@_[1],----@_[n]を使う
my $sec;
my $min;
my $hour;
my $mday;
my $mon;
my $year;
my $wday;
my $yday;
my $isdst;
my $tt;
$tt=time();
($sec, $min, $hour, $mday, $mon, $year,
$wday, $yday, $isdst) = localtime($tt);
$year=$year+1900;
$mon=$mon+1;
open (OUT,">> $out_file");
printf OUT "%02d/%02d/%02d %02d:%02d:%02d ", $year,$mon,$mday,$hour,$min,$sec ;
close(OUT);
}
# ------------------------------- 新しいipアドレスを求める ----------------------------
#
# 2021/12/27 curlを使って自分のグローバルIPアドレスを求める
# 引数なし 関数として ipaddress 持ち帰るため
#
#
sub newip {
my $ip;
my $curl_ip = `curl -s inet-ip.info` ; # 2021/12/27 グローバルIPを求める
$curl_ip =~/[0-9]+(\.[0-9]+){3}/; # IPアドレスとのマッチパターン
$ip = $&; # マッチしたIPアドレスを入力
# print "あなたのグローバルIPアドレスは $ip です\n";
return $ip ;
}
# ----------------------------- IPアドレス変更あり ---------------------------------------
sub ipchange {
# 引数が一つの場合は$_[0]を、引数が複数の場合は@_[0],@_[1],----@_[n]を使う
my $ip_change_file=@_[0]; # 変更された時刻と変更されたIPアドレスを書き込むファイル
my $ipaddr_2=@_[1]; # new ip address
my $ip_old_file=@_[2]; # 新しいIPアドレスを書き込むファイル名−−>oldip
my $ip_change_log_txt=@_[3]; # IP更新のログ
my $ip_change_sta=@_[4]; # IP更新のstatus
my $line ; # 1行読み込み用
# print "Debug no_update file :$no_up\n";
#print "debug 31 \n";
&time_prt( $ip_change_file ); # 時刻の呼び出しとプリント
open (OUT,">>$ip_change_file");
print OUT "ip address is changed: $ipaddr_2 \n" ; #IPアドレスの変更を履歴に書き込む
close(OUT);
#print "debug 32 \n";
open (OUT,">$ip_old_file"); # IPaddressを書き込むファイル名
print OUT "$ipaddr_2" ; # 新しく変更したIPアドレスを書き込む
close(OUT);
#print "debug 33 \n";
open IN, "<",$ip_change_sta || die "Error: ipaddr.txt $!\n" ; # IP更新時のstatus入ったファイルを入力で開く
open (OUT,">>$ip_change_log_txt"); # IP変更の履歴ファイル名
while ( $line =<IN> ) {; # 記録されているstatusを1行読み込む
print OUT "$line" ; # 記録されているstatusを1行書き込み
}
close(OUT);
close IN;
}