[BACK]Return to update_trango.pl CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / scripts

Diff for /trango/Net-Telnet-Trango/scripts/update_trango.pl between version 1.3 and 1.4

version 1.3, 2005/11/16 20:48:48 version 1.4, 2005/11/16 21:13:39
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.2 2005/11/16 05:26:37 andrew Exp $  # $RedRiver: update_trango.pl,v 1.3 2005/11/16 20:48:48 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango foxes with a new firmware
 #  #
Line 10 
Line 10 
   
 use Net::Telnet;  use Net::Telnet;
 use Net::TFTP;  use Net::TFTP;
   use Fcntl ':flock'; # import LOCK_* constants
 #use YAML;  #use YAML;
   
   
 my $config_file = shift || 'fox53.conf';  my $config_file = shift || 'update_trango.conf';
 my $conf = read_conf($config_file);  my $conf = read_conf($config_file);
   
 my $max_tries = 1;  my $max_tries = 1;
   
   my $log_file =  GetLogName('UT');
   
   
   my $MYLOG;  # file handle for logging so we don't have to close and open it all the time
   END {
     if ($MYLOG) {
       mylog("Closing log ($log_file)");
       close $MYLOG;
     }
   }
   
   
   
   
   
   
   
   
 foreach my $fox (@{ $conf->{'ips'} }) {  foreach my $fox (@{ $conf->{'ips'} }) {
   print STDERR "Updating: $fox\n";    mylog("Updating: $fox");
   
   ## Connect and login.    ## Connect and login.
   my $host = new Net::Telnet (Timeout => 5,    my $host = new Net::Telnet (Timeout => 5,
                               Prompt => '/#> *$/');                                Prompt => '/#> *$/');
   print "Connecting to $fox\n";    mylog("Connecting to $fox");
   $host->open($fox);    $host->open($fox);
   $host->dump_log('dump.log');    $host->dump_log('dump.log');
   
Line 44 
Line 58 
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;      /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;
   
   if ($type ne $conf->{'type'}) {    if ($type ne $conf->{'type'}) {
     print STDERR "Wrong type of unit ('$type' should be '$conf->{'type'}')\n";      mylog("Wrong type of unit ('$type' should be '$conf->{'type'}')");
     $host->close;      $host->close;
     next;      next;
   }    }
   
   if ($version eq $conf->{'ver'}) {    if ($version eq $conf->{'ver'}) {
     print STDERR "Already up to date with firmware version '$version'\n";      mylog("Already up to date with firmware version '$version'");
     $host->close;      $host->close;
     next;      next;
   }    }
   
   print "Logging in\n";    mylog("Logging in");
   $host->print($conf->{'password'});    $host->print($conf->{'password'});
   $host->waitfor(    $host->waitfor(
     -match => $host->prompt,      -match => $host->prompt,
Line 63 
Line 77 
   ) or die "login ($fox) failed: ", $host->lastline;    ) or die "login ($fox) failed: ", $host->lastline;
   
   
   print "Sending commands\n";    mylog("Sending commands");
   ## Send commands    ## Send commands
   if ( upload($host, $fox, $conf->{'file_name'}) ) {    if ( upload($host, $fox, $conf->{'file_name'}) ) {
     print "Rebooting\n";      mylog("Rebooting");
     $host->print("reboot\n");      $host->print("reboot\n");
     $host->getline;      $host->getline;
   } else {    } else {
     print "Exiting\n";      mylog("Exiting");
     $host->print("exit\n");      $host->print("exit\n");
     $host->getline;      $host->getline;
   }    }
   $host->close;    $host->close;
     mylog("");
 }  }
   
 sub upload  sub upload
Line 83 
Line 98 
   my $fox  = shift;    my $fox  = shift;
   my $file = shift;    my $file = shift;
   
   print "Getting current version\n";    mylog("Getting current version");
   my $ver = get_ver($host);    my $ver = get_ver($host);
   
   if (    if (
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&      $ver->{'Firmware Version'}  eq $conf->{'ver'} &&
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}      $ver->{'Firmware Checksum'} eq $conf->{'cksum'}
   ) {    ) {
     print STDERR "Already updated!";      mylog("Already updated!");
     return 1;      return 1;
   }    }
   
   my $try = 0;    my $try = 0;
   while (1) {    while (1) {
     if ($try >= $max_tries) {      if ($try >= $max_tries) {
       print STDERR "Couldn't update in $max_tries tries!";        mylog("Couldn't update in $max_tries tries!");
       return undef;        return undef;
     }      }
     $try++;      $try++;
   
     #sysinfo($host);      #sysinfo($host);
           
     print "Enabling TFTPd\n";      mylog("Enabling TFTPd");
     enable_tftpd($host) || die "Couldn't enable tftpd";      enable_tftpd($host) || die "Couldn't enable tftpd";
   
     print "Uploading file\n";      mylog("Uploading file");
     # use tftp to push the file up      # use tftp to push the file up
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');      my $tftp = Net::TFTP->new($fox, Mode => 'octet');
   
Line 120 
Line 135 
     #my @lines = $host->getlines;      #my @lines = $host->getlines;
     #print Dump \@lines;      #print Dump \@lines;
           
     print "Checking upload\n";      mylog("Checking upload");
     my $results = check_tftpd($host);      my $results = check_tftpd($host);
     # check the 'File Length' against ???      # check the 'File Length' against ???
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
       print STDERR "File checksum does not match config file!";        mylog("File checksum does not match config file!");
       next;        next;
     }      }
   
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
       print STDERR "File length does not match config file!";        mylog("File length does not match config file!");
       next;        next;
     }      }
   
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
       print STDERR "File name does not match config file!";        mylog("File name does not match config file!");
       next;        next;
     }      }
   
     print "Updating flash\n";      mylog("Updating flash");
     $results = updateflash(      $results = updateflash(
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}        $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}
     ) or die "Couldn't update flash: " . $host->lastline;      ) or die "Couldn't update flash: " . $host->lastline;
Line 146 
Line 161 
       defined $results->{'Checksum'} &&        defined $results->{'Checksum'} &&
       $results->{'Checksum'} eq $conf->{'cksum'}        $results->{'Checksum'} eq $conf->{'cksum'}
     ) {      ) {
       print STDERR "Saved checksum does not match config file!";        mylog("Saved checksum does not match config file!");
       next;        next;
     }      }
           
     print STDERR "Successfully updated!\n";      mylog("Successfully updated!");
     return 1;      return 1;
   }    }
 }  }
Line 326 
Line 341 
   }    }
   
   return \%conf;    return \%conf;
   }
   
   sub mylog
   {
           my $thing = shift;
       chomp $thing;
   
           unless ($MYLOG) {
                   open ($MYLOG, '>>', $log_file)
                           or die "Couldn't open logfile!\n";
                   my $ofh = select $MYLOG;
                   $|=1;
                   select $ofh;
           mylog("Opened log ($log_file)");
           }
   
       print $thing, "\n";
           flock($MYLOG, LOCK_EX);
           print $MYLOG (scalar gmtime), "\t", $thing, "\n"
                   or die "Couldn't print to MYLOG: $!";
           flock($MYLOG, LOCK_UN);
   }
   
   sub GetLogName
   {
     my $prefix  = shift || die "Invalid prefix passed for log";
   
     my $logdate = GetLogDate();
     my $logver  = 0;
     my $logname;
   
     do {
       $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
       $logver++;
     } until (not -e $logname);
   
     return $logname;
   }
   
   sub GetLogDate
   {
     my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   
     $mon++;
     $year += 1900;
   
     if ($min  < 10) { $min  = "0$min"  }
     if ($sec  < 10) { $sec  = "0$sec"  }
     if ($hour < 10) { $hour = "0$hour" }
     if ($mday < 10) { $mday = "0$mday" }
     if ($mon  < 10) { $mon  = "0$mon"  }
   
     my $time = $year . $mon . $mday;
   
     return $time;
 }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>