[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.31

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

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