<フレッツISDN自動接続スクリプト>

#!/usr/bin/perl
# Script for Auto_connecting to Rimnet

$i=$j=0;
$FletsNo="06-6424-0440"; #RimnetのフレッツISDN接続番号

# MN128-SOHO-SL11 にログインする
print "Connecting to MN128-SOHO-SL11 by crond.\n";

use Net::Telnet ();
$t = new Net::Telnet(Timeout => 10,Prompt =>'/MN128-SOHO-SL11% $/');
$t->open("192.168.0.1");
$t->login("admin","○○○○");   # ○○○○はパスワード

print "Welcome to MN128-SOHO-SL11!\n";

#現在の接続状況データの読み込み
@lines = $t->cmd("show status");
&MN128(@lines);

#前回取得したIPアドレスの読み込み
open (IN,"/root/flets.txt"); # 一番最初は空ファイルで可
while (<IN>) {
chomp;
$Previous[$j] = $_;
++$j;
}
print "PreviousIP = $Previous[2]\n";
close IN;

#接続状況及びIPアドレスのチェック
if ($i > 1) {
print "The Line was disconnected.\n";
&Rimnet;
} else {
if ($Previous[2] eq $Current[2]) {
print "The line is still connecting.\n";
} else {
print "The line is connecting. But CurrentIP is NOT same as PreviousIP.\n";
&Rimnet;
}
}

#CurrentDataのセット
open (OUT,">./flets.txt");
for ($j=0;$j<=3;++$j) {
print OUT "$Current[$j]\n";
}
close OUT;

#WWW用データファイルの作成 ←HP上にIPアドレスと接続時間
open (WWW,">/SERVER/mn128.js");
print WWW "START=\'".$Current[0]."\'\n";
print WWW "IP=\'".$Current[2]."\'\n";
&Date_set($Current[0]);
print WWW "DAY=\'".$t_day."\'\n";
print WWW "HOUR=\'".$t_hour."\'\n";
print WWW "MIN=\'".$t_min."\'\n";
print WWW "SEC=\'".$t_sec."\'\n";
close WWW;

$t->close;
print "Connection closed by crond.\n";

########## Sub Routine ##########

########## MN128から接続状況のデータを取得 ##########
sub MN128 {
   my (@mn128)=@_;
   foreach (@mn128) {
    if (/B2チャネル/) {
      $i=1;
    } elsif (/接続時刻/) {
      $StartTime[$i] = substr($',1,length($')-3);
    } elsif (/相手先電話番号/) {
      $TelephonNo[$i] = substr($',1,length($')-3);
    } elsif (/割り当てIPアドレス/) {
      $CurrentIP[$i] = substr($',1,length($')-3);
    } elsif (/経過時間\/最大接続時間\(分\)/) {
      $ConnectingTime[$i] = substr($',1,length($')-8);
    }
}

########## フレッツの接続確認 ##########
   if ($TelephonNo[0] eq $FletsNo) {
    $i=0; #B1Channelで接続している
   } elsif ($TelephonNO[1] eq $FletsNo) {
    $i=1; #B2Channelで接続している
   } else {
    $i=2; #切断されている
   }

   print "StartTime = $StartTime[$i]\n";
   print "TelephonNo = $TelephonNo[$i]\n";
   print "CurrentIP = $CurrentIP[$i]\n";
   print "ConnectingTime = $ConnectingTime[$i]\n";

   $Current[0] = $StartTime[$i];
   $Current[1] = $TelephonNo[$i];
   $Current[2] = $CurrentIP[$i];
   $Current[3] = $ConnectingTime[$i];
}


########## Rimnetに再接続 ##########
sub Rimnet {
   print "Connecting to Rimnet by crond.\n";
   @lines = $t->cmd("connect 0");
   @lines = $t->cmd("show status");
   &MN128(@lines);
}


########## WWW表示用データのセット ##########
sub Date_set {
   my ($start_time) = @_;
   use Time::Local;
   ($s_date, $s_time)=split(/ /,$start_time);

   ($s_YY, $s_MM, $s_DD) = split(/\//,$s_date);
   ($s_hh, $s_mm, $s_ss) = split(/\:/,$s_time);
   $starttime = timelocal($s_ss, $s_mm, $s_hh, $s_DD, $s_MM-1, $s_YY);
   $now = time;
   $term = $now - $starttime;
   $t_day = $term / (24*60*60);
   ($t_day, $t_rest) = split(/\./, $t_day);
   $t_hour = (($term - $t_day * (24*60*60)) / (60*60));
   ($t_hour, $t_rest) = split(/\./, $t_hour);
   $t_min = (($term - $t_day * (24*60*60) - $t_hour * (60*60)) / 60);
   ($t_min, $t_rest) = split(/\./, $t_min);
   $t_sec = ($term - $t_day * (24*60*60) - $t_hour * (60*60) - $t_min * 60);
}