[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.13 and 1.31

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

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