[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.12 and 1.23

version 1.12, 2005/12/29 18:41:17 version 1.23, 2007/02/01 18:44:30
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.11 2005/12/21 02:03:30 andrew Exp $  # $RedRiver: update_trango.pl,v 1.22 2007/02/01 17:58:33 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango foxes with a new firmware
 #  #
 # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>  # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>
 ########################################################################  ########################################################################
 use strict;  use strict;
 use warnings;  use warnings;
   
 use Net::TFTP;  use YAML qw/ LoadFile /;
 use YAML;  use Net::TFTP;
 use lib '.';  use lib '.';
 use Net::Telnet::Trango;  use Net::Telnet::Trango;
   
 my $config_file = shift || 'update_trango.conf';  my $config_file = shift || 'update_trango.yaml';
 my $max_tries = 3;  my $max_tries = 3;
   
   my $l = Mylogger->new( { log_prefix => 'UT' } );
   
 my $l = Mylogger->new( { log_prefix => 'UT' } );  $l->sp("Reading config file '$config_file'");
   my $conf = LoadFile($config_file);
   
 $l->sp("Reading config file '$config_file'");  my @foxes = (
 my $conf = read_conf($config_file);  '10.100.1.2',
   '10.100.2.2',
 $l->sp("  Hardware Type: $conf->{'type'}");  '10.100.3.2',
 $l->sp("  File Name:     $conf->{'file_name'}");  '10.100.4.2',
 $l->sp("  File Size:     $conf->{'file_size'}");  );
 $l->sp("  File Checksum: $conf->{'file_cksum'}");  
 $l->sp("  FW Version:    $conf->{'ver'}");  foreach my $fox (@foxes) {
 $l->sp("  FW Checksum:   $conf->{'cksum'}");    $l->sp("Checking: $fox");
 $l->sp("");    my $needs_reboot = 0;
   
     ## Connect and login.
     my $t = new Net::Telnet::Trango (
       Timeout => 5,
       Errmode => 'return',
     ) or die "Couldn't make new connection: $!";
 foreach my $fox (@{ $conf->{'ips'} }) {    $l->p("Connecting to $fox");
   $l->sp("Updating: $fox");    unless ( $t->open($fox) ) {
       $l->sp("Error connecting: $!");
   ## Connect and login.      next;
   my $t = new Net::Telnet::Trango ({    }
     Host    => $fox,  
     Timeout => 5,    foreach my $firmware_type ('Firmware', 'FPGA') {
   });  
       if (! exists $conf->{$firmware_type}) {
   $l->p("Connecting to $fox");            $l->s("No configs for '$firmware_type'");
   my ($type, $version) = $t->connect();            $t->close();
             next;
   unless (defined $type && defined $version) {          }
     $l->sp("Error connecting!");  
     next;      my $host_type = $t->host_type;
   }      if ($firmware_type eq 'FPGA') {
         $host_type =~ s/\s.*$//;
   if ($type ne $conf->{'type'}) {      }
     $l->sp("Wrong type of unit ('$type' should be '$conf->{'type'}')");  
     $t->close;      if (! exists $conf->{$firmware_type}->{$host_type}) {
     next;            $l->sp("No '$firmware_type' config for type $host_type");
   }            $t->close();
             next;
   if ($version eq $conf->{'ver'}) {          }
     $l->sp("Already up to date with firmware version '$version'");  
     $t->close;      if (! $t->logged_in) {
     next;        $l->p("Logging in");
   }        $t->login($conf->{general}->{'password'});
         unless ($t->logged_in) {
   $l->p("Logging in");          $l->p('Failed!');
   $t->login($conf->{'password'}) || die "Couldn't login: $!";          $t->bye();
           next;
   $l->p("Sending commands");        }
   ## Send commands      }
   #print Dump $t->ver();  
   #print Dump $t->tftpd();          foreach my $k (keys %{ $conf->{general} }) {
   if ( upload($t, $conf->{'file_name'}) ) {            $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
     $l->p("Rebooting");      }
     $t->reboot;          $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
   } else {          $conf->{$firmware_type}->{$host_type}->{type} ||= $host_type;
     $l->p("Exiting");  
     $t->exit;      $l->sp("$host_type $firmware_type");
   }      $l->p("Sending commands");
         ## Send commands
   $t->close;      my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
   $l->sp("");          if ($rc) {
 }        $l->sp("Successfull!");
             $needs_reboot = 1;
 sub upload      } elsif (defined $rc) {
 {            $l->sp("Already up to date");
   my $t    = shift;      } else {
   my $file = shift;            $l->sp("Failed");
             $t->bye;
   my $ver = $t->ver;            next;
   $l->p("Current version '$ver->{'Firmware Version'}'");      }
   
   if (    }
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&  
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}    if ($needs_reboot) {
   ) {      $l->sp("Rebooting $fox");
     $l->sp("Already updated!");      $t->reboot;
     return 1;    } else {
   }      $l->sp("Bye $fox");
       $t->bye();
   my $try = 0;    }
   while (1) {    $l->sp("");
     if ($try >= $max_tries) {  }
       $l->sp("Couldn't update in $max_tries tries!");  
       return undef;  sub upload
     }  {
     $try++;    my $t    = shift;
     my $conf = shift;
     #sysinfo($self->{'_host'});  
         my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
     $l->p("Enabling TFTPd");  
     $t->enable_tftpd || die "Couldn't enable tftpd";    my $fw_type = $conf->{firmware_type};
   
     $l->p("Uploading file ($file)");    my $ver = $t->ver;
     # use tftp to push the file up  
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');    if (
       $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
     $tftp->put($file, $file)      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
       or die "Error uploading: " . $tftp->error;    ) {
       return 0;
     # waitfor some sort of output    }
     # make sure it says 'Success.' otherwise error  
     #print "LAST: " . $self->lastline;    $l->sp("Config information:");
     #my @lines = $self->getlines;    $l->sp("  Hardware Type: $conf->{'type'}");
     #print Dump \@lines;    $l->sp("  File Name:     $conf->{'file_name'}");
         $l->sp("  File Size:     $conf->{'file_size'}");
     $l->p("Checking upload ($conf->{'file_cksum'})");    $l->sp("  File Checksum: $conf->{'file_cksum'}");
     my $results = $t->tftpd;    $l->sp("  Conf Version:  $conf->{'ver'}");
     # check the 'File Length' against ???    $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {    $l->sp("  Conf Checksum: $conf->{'cksum'}");
       $l->sp(    $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
         "File checksum '" . $results->{'File Checksum'} .  
         "does not match config file '" . $conf->{'file_cksum'} . "'!"    $l->sp("Updating");
       );    my $try = 0;
       next;    while (1) {
     }      if ($try >= $max_tries) {
     $l->p("File checksum matches . . . ");        $l->sp("Couldn't update in $max_tries tries!");
         return;
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {      }
       $l->sp(      $try++;
         "File length '" . $results->{'File Length'} .  
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"      #sysinfo($self->{'_host'});
       );  
       next;      $l->p("Enabling TFTPd");
     }      unless ($t->enable_tftpd) {
     $l->p("File length matches . . . ");        $l->sp("Couldn't enable tftpd");
         next;
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {      }
       $l->sp(  
         "File name '" . $results->{'File Name'} .      $l->p("Uploading file ($file)");
         "' does not match config file '" . $conf->{'file_name'} . "'!"      # use tftp to push the file up
       );      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
       next;  
     }      unless ($tftp->put($file, $file)) {
     $l->p("File name  matches . . . ");        $l->sp("Error uploading: " . $tftp->error);
         next;
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");      }
     unless ($results = $t->updateflash(  
       $ver->{'Firmware Checksum'}, $conf->{'cksum'}      $l->p("Checking upload ($conf->{'file_cksum'})");
     ) ) {      my $results = $t->tftpd;
       $l->sp("Couldn't update flash: $!");      # check the 'File Length' against ???
       next;      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
     }        $l->sp(
           "File checksum '" . $results->{'File Checksum'} .
     unless (          " does not match config file '" . $conf->{'file_cksum'} . "'!"
       defined $results->{'Checksum'} &&        );
       $results->{'Checksum'} eq $conf->{'cksum'}        next;
     ) {      }
       $l->sp("Saved checksum does not match config file!");      $l->p("File checksum matches . . . ");
       next;  
     }      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .        $l->sp(
           "matches ($conf->{'cksum'})");          "File length '" . $results->{'File Length'} .
               "does not match config file '" . $conf->{'file_size'} . " bytes'!"
     $l->sp("Successfully updated!");        );
     return 1;        next;
   }      }
 }      $l->p("File length matches . . . ");
   
 sub read_conf      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
 {        $l->sp(
   my $file = shift;          "File name '" . $results->{'File Name'} .
   my %conf;          "' does not match config file '" . $conf->{'file_name'} . "'!"
   my $in_ip_list = 0;        );
   open my $fh, $file or die "Couldn't open file $file: $!";        next;
   while (<$fh>) {      }
     chomp;      $l->p("File name  matches . . . ");
     next if /^#/;  
     next if /^$/;      my $image_type = 'mainimage';
     if ($in_ip_list) {      if ($fw_type eq 'FPGA') {
       s/\s+//g; # Whitespace is a no no        $image_type = 'fpgaimage';
       }
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);      unless ($results = $t->updateflash(
       } else {        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
         push @{ $conf{'ips'} }, $_;                ' '          . $conf->{'cksum'},
       }        Timeout => 90,
     } else {      ) ) {
       my ($key, $val) = split /\s+/, $_, 2;        $l->sp("Couldn't update flash: $!");
         next;
       if (lc($key) eq 'ips') {      }
         $in_ip_list = 1;  
         next;      unless (
       }        defined $results->{'Checksum'} &&
         $results->{'Checksum'} eq $conf->{'cksum'}
       $key =~ s/^\s+//;      ) {
       $key =~ s/\s+$//;        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
       $val =~ s/^\s+//;        next;
       $val =~ s/\s+$//;      }
       $l->p("Uploaded checksum ($results->{'Checksum'}) " .
       $conf{ lc($key) } = $val;            "matches ($conf->{'cksum'})");
     }  
   }      $l->sp("Successfully updated!");
   close $fh;      return 1;
     }
   #print Dump \%conf;  }
   foreach (  
     'password',  package Mylogger;
     'file_name', 'file_size', 'file_cksum',  
     'ver', 'cksum', 'ips',  use Fcntl ':flock'; # import LOCK_* constants
   ) {  #use YAML;
     die "No $_ specified in config file!"  use constant LOG_PRINT => 128;
       if (not exists $conf{$_});  use constant LOG_SAVE  =>  64;
   }  
   DESTROY {
   #print Dump \%conf;    my $self = shift;
   #exit;    if ($self->{'MYLOG'}) {
   return \%conf;      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
 }      close $self->{'MYLOG'};
     }
 package Mylogger;  }
   
 use Fcntl ':flock'; # import LOCK_* constants  sub new {
 #use YAML;    my $package = shift;
 use constant LOG_PRINT => 128;    my $self = shift || {};
 use constant LOG_SAVE  =>  64;  
     $self->{'base_path'}  ||= '.';
 DESTROY {    $self->{'log_path'}   ||= $self->{'base_path'};
   my $self = shift;    $self->{'log_prefix'} ||= 'LOG';
   if ($self->{'MYLOG'}) {    $self->{'log_file'}   ||= GetLogName(
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");      $self->{'log_prefix'},
     close $self->{'MYLOG'};      $self->{'log_path'}
   }    );
 }    bless $self, $package;
   }
 sub new {  
   my $package = shift;  sub s
   my $self = shift || {};  {
     my $self = shift;
   $self->{'base_path'}  ||= '.';    my $m = shift;
   $self->{'log_path'}   ||= $self->{'base_path'};    return $self->mylog($m, LOG_SAVE);
   $self->{'log_prefix'} ||= 'LOG';  }
   $self->{'log_file'}   ||= GetLogName(  
     $self->{'log_prefix'},  sub p
     $self->{'log_path'}  {
   );    my $self = shift;
   bless $self, $package;    my $m = shift;
 }    return $self->mylog($m, LOG_PRINT);
   }
 sub s  
 {  sub sp
   my $self = shift;  {
   my $m = shift;    my $self = shift;
   return $self->mylog($m, LOG_SAVE);    my $m = shift;
 }    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
   }
 sub p  
 {  sub mylog
   my $self = shift;  {
   my $m = shift;    my $self = shift;
   return $self->mylog($m, LOG_PRINT);  
 }    my $thing = shift;
     chomp $thing;
 sub sp  
 {    my $which = shift;
   my $self = shift;  
   my $m = shift;    my $MYLOG;
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);    if ($which & LOG_PRINT) {
 }      print $thing, "\n";
     }
 sub mylog  
 {    if ($which & LOG_SAVE) {
   my $self = shift;      if ($self->{'MYLOG'}) {
         $MYLOG = $self->{'MYLOG'};
   my $thing = shift;      } else {
   chomp $thing;        unless ($MYLOG) {
                   open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
   my $which = shift;            or die "Couldn't open logfile!\n";
           my $ofh = select $MYLOG;
   my $MYLOG;          $|=1;
   if ($which & LOG_PRINT) {          select $ofh;
     print $thing, "\n";          $self->{'MYLOG'} = $MYLOG;
   }  
           $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
   if ($which & LOG_SAVE) {        }
     if ($self->{'MYLOG'}) {      }
       $MYLOG = $self->{'MYLOG'};      flock($MYLOG, LOCK_EX);
     } else {      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
       unless ($MYLOG) {        or die "Couldn't print to MYLOG: $!";
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})      flock($MYLOG, LOCK_UN);
           or die "Couldn't open logfile!\n";    }
         my $ofh = select $MYLOG;  }
         $|=1;  
         select $ofh;  sub GetLogName
         $self->{'MYLOG'} = $MYLOG;  {
     my $prefix  = shift || die "Invalid prefix passed for log";
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");  
       }    my $logdate = GetLogDate();
     }    my $logver  = 0;
     flock($MYLOG, LOCK_EX);    my $logname;
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"  
       or die "Couldn't print to MYLOG: $!";    do {
     flock($MYLOG, LOCK_UN);      $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
   }      $logver++;
 }    } until (not -e $logname);
   
 sub GetLogName    return $logname;
 {  }
   my $prefix  = shift || die "Invalid prefix passed for log";  
   sub GetLogDate
   my $logdate = GetLogDate();  {
   my $logver  = 0;    my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   my $logname;  
     $mon++;
   do {    $year += 1900;
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';  
     $logver++;    if ($min  < 10) { $min  = "0$min"  }
   } until (not -e $logname);    if ($sec  < 10) { $sec  = "0$sec"  }
     if ($hour < 10) { $hour = "0$hour" }
   return $logname;    if ($mday < 10) { $mday = "0$mday" }
 }    if ($mon  < 10) { $mon  = "0$mon"  }
   
 sub GetLogDate    my $time = $year . $mon . $mday;
 {  
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();    return $time;
   }
   $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.12  
changed lines
  Added in v.1.23

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