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

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

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