[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.14 and 1.34

version 1.14, 2005/12/30 19:26:06 version 1.34, 2007/02/07 23:24:39
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.13 2005/12/30 01:02:41 andrew Exp $  # $RedRiver: update_trango.pl,v 1.33 2007/02/07 22:07:35 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango hosts with a new firmware
 #  #
 # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>  # 2005.11.15 #*#*# andrew fresh <andrew@mad-techies.org>
 ########################################################################  ########################################################################
 use strict;  # Copyright (C) 2005, 2006, 2007 by Andrew Fresh
 use warnings;  #
   # This program is free software; you can redistribute it and/or modify
 use Net::TFTP;  # it under the same terms as Perl itself.
 use YAML;  ########################################################################
 use lib '.';  use strict;
 use Net::Telnet::Trango;  use warnings;
   
 my $config_file = shift || 'update_trango.conf';  use YAML qw/ LoadFile /;
 my $max_tries = 3;  use Net::TFTP;
   use Net::Telnet::Trango;
   
   my $config_file = shift || 'update_trango.yaml';
 my $l = Mylogger->new( { log_prefix => 'UT' } );  my $max_tries = 3;
   
   my $l = Mylogger->new( { log_prefix => 'UT' } );
 $l->sp("Reading config file '$config_file'");  
 my $conf = read_conf($config_file);  $l->sp("Reading config file '$config_file'");
   my $conf = LoadFile($config_file);
 $l->sp("  Hardware Type: $conf->{'type'}");  
 $l->sp("  File Name:     $conf->{'file_name'}");  my $hosts;
 $l->sp("  File Size:     $conf->{'file_size'}");  if (@ARGV) {
 $l->sp("  File Checksum: $conf->{'file_cksum'}");      @{$hosts} = map { { name => $_, group => 'Trango-Client' } } @ARGV;
 $l->sp("  FW Version:    $conf->{'ver'}");  }
 $l->sp("  FW Checksum:   $conf->{'cksum'}");  else {
 $l->sp("");      $hosts = parse_hosts( $conf->{hosts} );
   }
   
   #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
   
   my $global_tries = $max_tries * 2;
   while ( $global_tries > 0 ) {
 foreach my $fox (@{ $conf->{'ips'} }) {      $global_tries--;
   $l->sp("Updating: $fox");      my $processed = 0;
   
   ## Connect and login.      foreach my $host ( @{$hosts} ) {
   my $t = new Net::Telnet::Trango (  
     Timeout => 5,          if ( !exists $host->{retry} ) {
     Errmode => 'return',              $host->{tries} = 0;
   ) or die "Couldn't make new connection: $!";              $host->{retry} = 1;
           }
   $l->p("Connecting to $fox");  
   unless ( $t->open($fox) ) {          if ( $host->{tries} >= $max_tries ) {
     $l->sp("Error connecting: $!");              $host->{retry} = 0;
     next;          }
   }  
           if ( $host->{retry} <= 0 ) {
   if ($t->host_type ne $conf->{'type'}) {              next;
     $l->sp("Wrong type of unit ('$t->host_type' should be '$conf->{'type'}')");          }
     $t->close;  
     next;          $host->{tries}++;
   }          $processed++;
   
   if ($t->firmware_version eq $conf->{'ver'}) {          $l->sp("");
     $l->sp("Already up to date with firmware version '$t->firmware_version'");          $l->sp("Checking: $host->{name} (try $host->{tries})");
     $t->close;          my $needs_reboot = 0;
     next;  
   }          ## Connect and login.
           my $t = new Net::Telnet::Trango(
   $l->p("Logging in");              Timeout => 5,
   $t->login($conf->{'password'}) || die "Couldn't login: $!";              Errmode => 'return',
           ) or die "Couldn't make new connection: $!";
   $l->p("Sending commands");          $l->p("Connecting to $host->{name}");
   ## Send commands          unless ( $t->open( $host->{name} ) ) {
   #print Dump $t->login_banner;              $l->sp("Error connecting: $!");
   #print Dump $t->sysinfo;              next;
   #print Dump $t->last_lines;          }
   #print Dump $t->ver();  
   print Dump $t->tftpd();          my $password = $host->{Telnet_Password} || $conf->{general}->{password};
   print Dump $t->enable_tftpd();  
   #print Dump [ "Exit", $t->exit ];          $l->p("Logging in");
   print Dump $t->tftpd();          $t->login($password);
   print Dump $t->disable_tftpd();          unless ($t->logged_in) {
   print Dump $t->tftpd();              $l->p('Failed!');
   #if ( upload($t, $conf->{'file_name'}) ) {              $t->close;
   #   $l->p("Rebooting");              next;
   #  $t->reboot;          }
   #} else {  
     $l->p("Exiting");          $l->sp("Getting sudb");
     $t->exit;          my $sudb = $t->sudb_view;
   #}          if ($sudb) {
                 foreach my $su (@{ $sudb }) {
   $l->sp("");                  $l->p("Getting su info $su->{suid}");
 }                  my $su_info = $t->su_info( $su->{suid} );
                   if ($su_info->{ip}) {
 sub upload                      if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
 {                          $l->p("Already have $su_info->{ip}");
   my $t    = shift;                          next;
   my $file = shift;                      }
                       $l->sp("Adding host $su_info->{ip}");
   my $ver = $t->ver;                      my $new_host = {
   $l->p("Current version '$ver->{'Firmware Version'}'");                          password => $host->{password},
                           name     => $su_info->{ip},
   if (                          remarks  => $su_info->{remarks},
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&                      };
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}                      push @{ $hosts }, $new_host;
   ) {                  } else {
     $l->sp("Already updated!");                      $l->sp("Couldn't get su info for $su->{suid}");
     return 1;                      $l->sp("ERR: " . $t->last_error);
   }                  }
               }
   my $try = 0;          }
   while (1) {  
     if ($try >= $max_tries) {          foreach my $firmware_type ( 'Firmware', 'FPGA' ) {
       $l->sp("Couldn't update in $max_tries tries!");  
       return undef;              if ( !exists $conf->{$firmware_type} ) {
     }                  $l->s("No configs for '$firmware_type'");
     $try++;                  next;
               }
     #sysinfo($self->{'_host'});  
                   my $host_type = $t->host_type;
     $l->p("Enabling TFTPd");              if ( $firmware_type eq 'FPGA' ) {
     $t->enable_tftpd || die "Couldn't enable tftpd";                  $host_type =~ s/\s.*$//;
               }
     $l->p("Uploading file ($file)");  
     # use tftp to push the file up              if ( !exists $conf->{$firmware_type}->{$host_type} ) {
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');                  $l->sp("No '$firmware_type' config for type $host_type");
                   next;
     $tftp->put($file, $file)              }
       or die "Error uploading: " . $tftp->error;  
               if (   $firmware_type eq 'Firmware'
     # waitfor some sort of output                  && $t->firmware_version eq
     # make sure it says 'Success.' otherwise error                  $conf->{$firmware_type}->{$host_type}->{ver} )
     #print "LAST: " . $self->lastline;              {
     #my @lines = $self->getlines;                  $l->sp("Firmware already up to date");
     #print Dump \@lines;                  next;
                   }
     $l->p("Checking upload ($conf->{'file_cksum'})");  
     my $results = $t->tftpd;              if ( !$t->logged_in ) {
     # check the 'File Length' against ???                  $l->p("Logging in");
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {                  $t->login($password);
       $l->sp(                  unless ($t->logged_in) {
         "File checksum '" . $results->{'File Checksum'} .                      $l->p('Failed!');
         "does not match config file '" . $conf->{'file_cksum'} . "'!"                      $t->close;
       );                      last;
       next;                  }
     }              }
     $l->p("File checksum matches . . . ");  
               foreach my $k ( keys %{ $conf->{general} } ) {
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {                  $conf->{$firmware_type}->{$host_type}->{$k} ||=
       $l->sp(                    $conf->{general}->{$k};
         "File length '" . $results->{'File Length'} .              }
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"              $conf->{$firmware_type}->{$host_type}->{firmware_type} ||=
       );                $firmware_type;
       next;              $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
     }  
     $l->p("File length matches . . . ");              $l->sp("$host_type $firmware_type");
               ## Send commands
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {              my $rc = upload( $t, $conf->{$firmware_type}->{$host_type} );
       $l->sp(              if ($rc) {
         "File name '" . $results->{'File Name'} .                  $l->sp("Successfull!");
         "' does not match config file '" . $conf->{'file_name'} . "'!"                  $host->{retry}--;
       );                  $needs_reboot = 1;
       next;              }
     }              elsif ( defined $rc ) {
     $l->p("File name  matches . . . ");                  $l->sp("Already up to date");
                   $host->{retry}--;
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");              }
     unless ($results = $t->updateflash(              else {
       args => 'mainimage ' . $ver->{'Firmware Checksum'} .                  $l->sp("Failed! - Bye $host->{name}");
               ' '          . $conf->{'cksum'},                  $l->e("Error updating $firmware_type on $host->{name}" .
       Timeout => 90,                      "(try $host->{tries})");
     ) ) {                  $t->bye;
       $l->sp("Couldn't update flash: $!");                  # don't try any other firmware, don't want to reboot
       next;                  last;
     }              }
   
     unless (          }
       defined $results->{'Checksum'} &&  
       $results->{'Checksum'} eq $conf->{'cksum'}          if ($needs_reboot) {
     ) {              $l->sp("Rebooting $host->{name}");
       $l->sp("Saved checksum does not match config file!");              $t->reboot;
       next;          }
     }          else {
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .              $l->sp("Bye $host->{name}");
           "matches ($conf->{'cksum'})");              $t->bye();
               }
     $l->sp("Successfully updated!");      }
     return 1;  
   }      if ( !$processed ) {
 }          $l->sp("");
           $l->sp("Finished.  No more hosts.");
 sub read_conf          last;
 {      }
   my $file = shift;  }
   my %conf;  
   my $in_ip_list = 0;  sub upload {
   open my $fh, $file or die "Couldn't open file $file: $!";      my $t    = shift;
   while (<$fh>) {      my $conf = shift;
     chomp;  
     next if /^#/;      my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
     next if /^$/;  
     if ($in_ip_list) {      my $fw_type = $conf->{firmware_type};
       s/\s+//g; # Whitespace is a no no  
       my $ver = $t->ver;
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {  
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);      if (
       } else {          !(
         push @{ $conf{'ips'} }, $_;              $ver->{ $fw_type . ' Version' } && $ver->{ $fw_type . ' Checksum' }
       }          )
     } else {        )
       my ($key, $val) = split /\s+/, $_, 2;      {
           $l->sp("Error getting current version numbers");
       if (lc($key) eq 'ips') {          return;
         $in_ip_list = 1;      }
         next;  
       }      if (   $ver->{ $fw_type . ' Version' } eq $conf->{'ver'}
           && $ver->{ $fw_type . ' Checksum' } eq $conf->{'cksum'} )
       $key =~ s/^\s+//;      {
       $key =~ s/\s+$//;          return 0;
       $val =~ s/^\s+//;      }
       $val =~ s/\s+$//;  
       $l->sp("Updating $fw_type");
       $conf{ lc($key) } = $val;      $l->sp("Config information:");
     }      $l->sp("  Hardware Type: $conf->{'type'}");
   }      $l->sp("  File Name:     $conf->{'file_name'}");
   close $fh;      $l->sp("  File Size:     $conf->{'file_size'}");
       $l->sp("  File Checksum: $conf->{'file_cksum'}");
   #print Dump \%conf;      $l->sp("  Conf Version:  $conf->{'ver'}");
   foreach (      $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
     'password',      $l->sp("  Conf Checksum: $conf->{'cksum'}");
     'file_name', 'file_size', 'file_cksum',      $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
     'ver', 'cksum', 'ips',  
   ) {      my $try = 0;
     die "No $_ specified in config file!"      while (1) {
       if (not exists $conf{$_});          if ( $try >= $max_tries ) {
   }              $l->sp("Couldn't update in $max_tries tries!");
               return;
   #print Dump \%conf;          }
   #exit;          $try++;
   return \%conf;  
 }          $l->p("Enabling TFTPd");
           unless ( $t->enable_tftpd ) {
 package Mylogger;              $l->sp("Couldn't enable tftpd");
               next;
 use Fcntl ':flock'; # import LOCK_* constants          }
 #use YAML;  
 use constant LOG_PRINT => 128;          $l->p("Uploading file ($conf->{file_name})");
 use constant LOG_SAVE  =>  64;  
           # use tftp to push the file up
 DESTROY {          my $tftp = Net::TFTP->new( $t->Host, Mode => 'octet' );
   my $self = shift;  
   if ($self->{'MYLOG'}) {          unless ( $tftp->put( $file, $file ) ) {
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");              $l->sp( "Error uploading: " . $tftp->error );
     close $self->{'MYLOG'};              next;
   }          }
 }  
           $l->p("Checking upload ($conf->{'file_cksum'})");
 sub new {          my $results = $t->tftpd;
   my $package = shift;  
   my $self = shift || {};          # check the 'File Length' against ???
           if (
   $self->{'base_path'}  ||= '.';              !(
   $self->{'log_path'}   ||= $self->{'base_path'};                     $results->{'File Checksum'}
   $self->{'log_prefix'} ||= 'LOG';                  && $results->{'File Length'}
   $self->{'log_file'}   ||= GetLogName(                  && $results->{'File Name'}
     $self->{'log_prefix'},              )
     $self->{'log_path'}            )
   );          {
   bless $self, $package;              $l->sp("Unable to get results of upload");
 }              next;
           }
 sub s          if ( $results->{'File Checksum'} ne $conf->{'file_cksum'} ) {
 {              $l->sp( "File checksum ("
   my $self = shift;                    . $results->{'File Checksum'}
   my $m = shift;                    . ") does not match config file ("
   return $self->mylog($m, LOG_SAVE);                    . $conf->{'file_cksum'}
 }                    . ")!" );
               next;
 sub p          }
 {          $l->p("File checksum matches . . . ");
   my $self = shift;  
   my $m = shift;          if ( $results->{'File Length'} !~ /^$conf->{'file_size'} bytes/ ) {
   return $self->mylog($m, LOG_PRINT);              $l->sp( "File length ("
 }                    . $results->{'File Length'}
                     . ") does not match config file ("
 sub sp                    . $conf->{'file_size'}
 {                    . " bytes)!" );
   my $self = shift;              next;
   my $m = shift;          }
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);          $l->p("File length matches . . . ");
 }  
           if ( uc( $results->{'File Name'} ) ne uc($file) ) {
 sub mylog              $l->sp( "File name ("
 {                    . $results->{'File Name'}
   my $self = shift;                    . ") does not match config file ("
                     . $file
   my $thing = shift;                    . ")!" );
   chomp $thing;              next;
           }
   my $which = shift;          $l->p("File name matches . . . ");
   
   my $MYLOG;          my $image_type = 'mainimage';
   if ($which & LOG_PRINT) {          if ( $fw_type eq 'FPGA' ) {
     print $thing, "\n";              $image_type = 'fpgaimage';
   }          }
           $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   if ($which & LOG_SAVE) {          unless (
     if ($self->{'MYLOG'}) {              $results = $t->updateflash(
       $MYLOG = $self->{'MYLOG'};                  args => $image_type . ' '
     } else {                    . $ver->{ $fw_type . ' Checksum' } . ' '
       unless ($MYLOG) {                    . $conf->{'cksum'},
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})                  Timeout => 90,
           or die "Couldn't open logfile!\n";              )
         my $ofh = select $MYLOG;            )
         $|=1;          {
         select $ofh;              $l->sp("Couldn't update flash: $!");
         $self->{'MYLOG'} = $MYLOG;              next;
           }
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");  
       }          unless ( defined $results->{'Checksum'}
     }              && $results->{'Checksum'} eq $conf->{'cksum'} )
     flock($MYLOG, LOCK_EX);          {
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"              $l->sp( "Saved checksum "
       or die "Couldn't print to MYLOG: $!";                    . $results->{'Checksum'}
     flock($MYLOG, LOCK_UN);                    . " does not match config file "
   }                    . $conf->{'cksum'}
 }                    . "!" );
               next;
 sub GetLogName          }
 {  
   my $prefix  = shift || die "Invalid prefix passed for log";          $l->p("$fw_type saved checksum matches . . . ");
   
   my $logdate = GetLogDate();          return 1;
   my $logver  = 0;      }
   my $logname;  }
   
   do {  sub parse_hosts {
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';      my $src = shift;
     $logver++;  
   } until (not -e $logname);      my @hosts;
       foreach my $h ( @{$src} ) {
   return $logname;          if ( $h->{name} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/ )
 }          {
               for ( $2 .. $3 ) {
 sub GetLogDate                  my %cur_host;
 {                  foreach my $k ( keys %{$h} ) {
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();                      $cur_host{$k} = $h->{$k};
                   }
   $mon++;                  $cur_host{name} = $1 . $_;
   $year += 1900;                  if ( !grep { $cur_host{name} eq $h->{name} } @hosts ) {
                       push @hosts, \%cur_host;
   if ($min  < 10) { $min  = "0$min"  }                  }
   if ($sec  < 10) { $sec  = "0$sec"  }              }
   if ($hour < 10) { $hour = "0$hour" }          }
   if ($mday < 10) { $mday = "0$mday" }          else {
   if ($mon  < 10) { $mon  = "0$mon"  }              push @hosts, $h;
           }
   my $time = $year . $mon . $mday;      }
   
   return $time;      return \@hosts;
 }  }
   
   package Mylogger;
   
   use Fcntl ':flock';    # import LOCK_* constants
   
   #use YAML;
   use constant LOG_PRINT => 128;
   use constant LOG_SAVE  => 64;
   use constant LOG_ERR   => 1;
   
   DESTROY {
       my $self = shift;
       if ( $self->{'MYLOG'} ) {
           $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
           close $self->{'MYLOG'};
       }
   }
   
   sub new {
       my $package = shift;
       my $self = shift || {};
   
       $self->{'base_path'}  ||= '.';
       $self->{'log_path'}   ||= $self->{'base_path'};
       $self->{'log_prefix'} ||= 'LOG';
       $self->{'log_file'} ||=
         GetLogName( $self->{'log_prefix'}, $self->{'log_path'} );
       bless $self, $package;
   }
   
   sub s {
       my $self = shift;
       my $m    = shift;
       return $self->mylog( $m, LOG_SAVE );
   }
   
   sub p {
       my $self = shift;
       my $m    = shift;
       return $self->mylog( $m, LOG_PRINT );
   }
   
   sub sp {
       my $self = shift;
       my $m    = shift;
       return $self->mylog( $m, LOG_SAVE | LOG_PRINT );
   }
   
   sub e {
       my $self = shift;
       my $m    = shift;
       return $self->mylog( $m, LOG_ERR );
   }
   
   sub mylog {
       my $self = shift;
   
       my $thing = shift;
       chomp $thing;
   
       my $which = shift;
   
       my $MYLOG;
       if ( $which & LOG_PRINT ) {
           print $thing, "\n";
       }
   
       if ( $which & LOG_SAVE ) {
           if ( $self->{'MYLOG'} ) {
               $MYLOG = $self->{'MYLOG'};
           }
           else {
               unless ($MYLOG) {
                   open( $MYLOG, '>>',
                       $self->{'log_path'} . '/' . $self->{'log_file'} )
                     or die "Couldn't open logfile!\n";
                   my $ofh = select $MYLOG;
                   $| = 1;
                   select $ofh;
                   $self->{'MYLOG'} = $MYLOG;
   
                   $self->p(
                       "Opened log ($self->{'log_path'}/$self->{'log_file'})");
               }
           }
           flock( $MYLOG, LOCK_EX );
           print $MYLOG ( scalar gmtime ), "\t", $thing, "\n"
             or die "Couldn't print to MYLOG: $!";
           flock( $MYLOG, LOCK_UN );
       }
   
       if ( $which & LOG_ERR ) {
           # XXX Could tie in here to handle some sort of notifications.
           print STDERR $thing, "\n";
       }
   }
   
   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.14  
changed lines
  Added in v.1.34

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