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

version 1.3, 2005/11/16 20:48:48 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.2 2005/11/16 05:26:37 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::Telnet;  # it under the same terms as Perl itself.
 use Net::TFTP;  ########################################################################
 #use YAML;  use strict;
   use warnings;
   
 my $config_file = shift || 'fox53.conf';  use YAML qw/ LoadFile /;
 my $conf = read_conf($config_file);  use Net::TFTP;
   use Net::Telnet::Trango;
 my $max_tries = 1;  
   my $config_file = shift || 'update_trango.yaml';
   my $max_tries = 3;
   
   my $l = Mylogger->new( { log_prefix => 'UT' } );
   
   $l->sp("Reading config file '$config_file'");
 foreach my $fox (@{ $conf->{'ips'} }) {  my $conf = LoadFile($config_file);
   print STDERR "Updating: $fox\n";  
   my $hosts;
   ## Connect and login.  if (@ARGV) {
   my $host = new Net::Telnet (Timeout => 5,      @{$hosts} = map { { name => $_, group => 'Trango-Client' } } @ARGV;
                               Prompt => '/#> *$/');  }
   print "Connecting to $fox\n";  else {
   $host->open($fox);      $hosts = parse_hosts( $conf->{hosts} );
   $host->dump_log('dump.log');  }
   
   ## Login to remote host.  #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
   $host->waitfor(  
     -match => '/password: ?$/i',  my $global_tries = $max_tries * 2;
     -errmode => "return",  while ( $global_tries > 0 ) {
   ) or die "problem connecting to host ($fox): ", $host->lastline;      $global_tries--;
       my $processed = 0;
   my $login_banner = $host->lastline;  
   my ($type, $version) = $login_banner =~      foreach my $host ( @{$hosts} ) {
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;  
           if ( !exists $host->{retry} ) {
   if ($type ne $conf->{'type'}) {              $host->{tries} = 0;
     print STDERR "Wrong type of unit ('$type' should be '$conf->{'type'}')\n";              $host->{retry} = 1;
     $host->close;          }
     next;  
   }          if ( $host->{tries} >= $max_tries ) {
               $host->{retry} = 0;
   if ($version eq $conf->{'ver'}) {          }
     print STDERR "Already up to date with firmware version '$version'\n";  
     $host->close;          if ( $host->{retry} <= 0 ) {
     next;              next;
   }          }
   
   print "Logging in\n";          $host->{tries}++;
   $host->print($conf->{'password'});          $processed++;
   $host->waitfor(  
     -match => $host->prompt,          $l->sp("");
     -errmode => "return",          $l->sp("Checking: $host->{name} (try $host->{tries})");
   ) or die "login ($fox) failed: ", $host->lastline;          my $needs_reboot = 0;
   
           ## Connect and login.
   print "Sending commands\n";          my $t = new Net::Telnet::Trango(
   ## Send commands              Timeout => 5,
   if ( upload($host, $fox, $conf->{'file_name'}) ) {              Errmode => 'return',
     print "Rebooting\n";          ) or die "Couldn't make new connection: $!";
     $host->print("reboot\n");          $l->p("Connecting to $host->{name}");
     $host->getline;          unless ( $t->open( $host->{name} ) ) {
   } else {              $l->sp("Error connecting: $!");
     print "Exiting\n";              next;
     $host->print("exit\n");          }
     $host->getline;  
   }          my $password = $host->{Telnet_Password} || $conf->{general}->{password};
   $host->close;  
 }          $l->p("Logging in");
           $t->login($password);
 sub upload          unless ($t->logged_in) {
 {              $l->p('Failed!');
   my $host = shift;              $t->close;
   my $fox  = shift;              next;
   my $file = shift;          }
   
   print "Getting current version\n";          $l->sp("Getting sudb");
   my $ver = get_ver($host);          my $sudb = $t->sudb_view;
           if ($sudb) {
   if (              foreach my $su (@{ $sudb }) {
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&                  $l->p("Getting su info $su->{suid}");
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}                  my $su_info = $t->su_info( $su->{suid} );
   ) {                  if ($su_info->{ip}) {
     print STDERR "Already updated!";                      if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
     return 1;                          $l->p("Already have $su_info->{ip}");
   }                          next;
                       }
   my $try = 0;                      $l->sp("Adding host $su_info->{ip}");
   while (1) {                      my $new_host = {
     if ($try >= $max_tries) {                          password => $host->{password},
       print STDERR "Couldn't update in $max_tries tries!";                          name     => $su_info->{ip},
       return undef;                          remarks  => $su_info->{remarks},
     }                      };
     $try++;                      push @{ $hosts }, $new_host;
                   } else {
     #sysinfo($host);                      $l->sp("Couldn't get su info for $su->{suid}");
                           $l->sp("ERR: " . $t->last_error);
     print "Enabling TFTPd\n";                  }
     enable_tftpd($host) || die "Couldn't enable tftpd";              }
           }
     print "Uploading file\n";  
     # use tftp to push the file up          foreach my $firmware_type ( 'Firmware', 'FPGA' ) {
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');  
               if ( !exists $conf->{$firmware_type} ) {
     $tftp->put($file, $file)                  $l->s("No configs for '$firmware_type'");
       or die "Error uploading: " . $tftp->error;                  next;
               }
     # waitfor some sort of output  
     # make sure it says 'Success.' otherwise error              my $host_type = $t->host_type;
     #print "LAST: " . $host->lastline;              if ( $firmware_type eq 'FPGA' ) {
     #my @lines = $host->getlines;                  $host_type =~ s/\s.*$//;
     #print Dump \@lines;              }
       
     print "Checking upload\n";              if ( !exists $conf->{$firmware_type}->{$host_type} ) {
     my $results = check_tftpd($host);                  $l->sp("No '$firmware_type' config for type $host_type");
     # check the 'File Length' against ???                  next;
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {              }
       print STDERR "File checksum does not match config file!";  
       next;              if (   $firmware_type eq 'Firmware'
     }                  && $t->firmware_version eq
                   $conf->{$firmware_type}->{$host_type}->{ver} )
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {              {
       print STDERR "File length does not match config file!";                  $l->sp("Firmware already up to date");
       next;                  next;
     }              }
   
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {              if ( !$t->logged_in ) {
       print STDERR "File name does not match config file!";                  $l->p("Logging in");
       next;                  $t->login($password);
     }                  unless ($t->logged_in) {
                       $l->p('Failed!');
     print "Updating flash\n";                      $t->close;
     $results = updateflash(                      last;
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}                  }
     ) or die "Couldn't update flash: " . $host->lastline;              }
     unless (  
       defined $results->{'Checksum'} &&              foreach my $k ( keys %{ $conf->{general} } ) {
       $results->{'Checksum'} eq $conf->{'cksum'}                  $conf->{$firmware_type}->{$host_type}->{$k} ||=
     ) {                    $conf->{general}->{$k};
       print STDERR "Saved checksum does not match config file!";              }
       next;              $conf->{$firmware_type}->{$host_type}->{firmware_type} ||=
     }                $firmware_type;
                   $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
     print STDERR "Successfully updated!\n";  
     return 1;              $l->sp("$host_type $firmware_type");
   }              ## Send commands
 }              my $rc = upload( $t, $conf->{$firmware_type}->{$host_type} );
               if ($rc) {
 sub get_ver                  $l->sp("Successfull!");
 {                  $host->{retry}--;
   my $host = shift;                  $needs_reboot = 1;
   return cmd($host, 'ver');              }
 }              elsif ( defined $rc ) {
                   $l->sp("Already up to date");
 sub enable_tftpd                  $host->{retry}--;
 {              }
   my $host = shift;              else {
                   $l->sp("Failed! - Bye $host->{name}");
   my $vals = cmd($host, 'tftpd on', 'Success.');                  $l->e("Error updating $firmware_type on $host->{name}" .
                       "(try $host->{tries})");
   if ($vals->{'Tftpd'} eq 'listen') {                  $t->bye;
     return $vals;                  # don't try any other firmware, don't want to reboot
   } else {                  last;
     return undef;              }
   }  
 }          }
   
 sub check_tftpd          if ($needs_reboot) {
 {              $l->sp("Rebooting $host->{name}");
   my $host = shift;              $t->reboot;
   return cmd($host, 'tftpd', 'Success.');          }
 }          else {
               $l->sp("Bye $host->{name}");
 sub sysinfo              $t->bye();
 {          }
   my $host = shift;      }
   return cmd($host, 'sysinfo', 'Success.');  
 }      if ( !$processed ) {
           $l->sp("");
 sub updateflash          $l->sp("Finished.  No more hosts.");
 {          last;
   my $host = shift;      }
   my $old = shift;  }
   my $new = shift;  
   sub upload {
   return undef unless $new;      my $t    = shift;
       my $conf = shift;
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);  
 }      my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
   
 sub cmd      my $fw_type = $conf->{firmware_type};
 {  
   my $host   = shift;      my $ver = $t->ver;
   my $string = shift;  
   my $expect_last = shift;      if (
   my $timeout = shift || 5;          !(
               $ver->{ $fw_type . ' Version' } && $ver->{ $fw_type . ' Checksum' }
   my @lines = $host->cmd(String => $string, Timeout => $timeout);          )
         )
   my $vals = decode_lines(@lines);      {
           $l->sp("Error getting current version numbers");
   my $last = $host->lastline;          return;
       }
   unless ($expect_last) {  
     return $vals;      if (   $ver->{ $fw_type . ' Version' } eq $conf->{'ver'}
   }          && $ver->{ $fw_type . ' Checksum' } eq $conf->{'cksum'} )
       {
   if ($last =~ /$expect_last$/) {          return 0;
     return $vals;      }
   } else {  
     warn "Error with command ($string): $last";      $l->sp("Updating $fw_type");
     return undef;      $l->sp("Config information:");
   }      $l->sp("  Hardware Type: $conf->{'type'}");
 }      $l->sp("  File Name:     $conf->{'file_name'}");
       $l->sp("  File Size:     $conf->{'file_size'}");
 sub decode_lines      $l->sp("  File Checksum: $conf->{'file_cksum'}");
 {      $l->sp("  Conf Version:  $conf->{'ver'}");
   my @lines = @_;      $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
       $l->sp("  Conf Checksum: $conf->{'cksum'}");
   my %conf;      $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
   
   my $key = '';      my $try = 0;
   my $val = '';      while (1) {
   my $in_key = 0;          if ( $try >= $max_tries ) {
   my $in_val = 0;              $l->sp("Couldn't update in $max_tries tries!");
               return;
   foreach my $line (@lines) {          }
     my @chars = split //, $line;          $try++;
   
     my $last_key = '';          $l->p("Enabling TFTPd");
     foreach my $c (@chars) {          unless ( $t->enable_tftpd ) {
               $l->sp("Couldn't enable tftpd");
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {              next;
         if ($c eq '[') {          }
           $in_key = 1;  
           $in_val = 0;          $l->p("Uploading file ($conf->{file_name})");
         } else {  
           $in_key = 0;          # use tftp to push the file up
           $in_val = 0;          my $tftp = Net::TFTP->new( $t->Host, Mode => 'octet' );
         }  
           unless ( $tftp->put( $file, $file ) ) {
         if ($key) {              $l->sp( "Error uploading: " . $tftp->error );
           $key =~ s/^\s+//;              next;
           $key =~ s/\s+$//;          }
   
           $val =~ s/^\s+//;          $l->p("Checking upload ($conf->{'file_cksum'})");
           $val =~ s/\s+$//;          my $results = $t->tftpd;
   
           if ($key eq 'Checksum' && $last_key) {          # check the 'File Length' against ???
             # Special case for these bastids.          if (
             my $new = $last_key;              !(
             $new =~ s/\s+\S+$//;                     $results->{'File Checksum'}
             $key = $new . " " . $key;                  && $results->{'File Length'}
           }                  && $results->{'File Name'}
               )
           $last_key = $key;            )
           $conf{$key} = $val;          {
           $key = '';              $l->sp("Unable to get results of upload");
           $val = '';              next;
         }          }
           if ( $results->{'File Checksum'} ne $conf->{'file_cksum'} ) {
       } elsif ($c eq ']') {              $l->sp( "File checksum ("
         $in_val = 1;                    . $results->{'File Checksum'}
         $in_key = 0;                    . ") does not match config file ("
         $c = shift @chars;                    . $conf->{'file_cksum'}
                     . ")!" );
       } elsif ($in_key) {              next;
         $key .= $c;          }
           $l->p("File checksum matches . . . ");
       } elsif ($in_val) {  
         $val .= $c;          if ( $results->{'File Length'} !~ /^$conf->{'file_size'} bytes/ ) {
       }              $l->sp( "File length ("
     }                    . $results->{'File Length'}
   }                    . ") does not match config file ("
   #print Dump \%conf;                    . $conf->{'file_size'}
                     . " bytes)!" );
   if (%conf) {              next;
     return \%conf;          }
   } else {          $l->p("File length matches . . . ");
     return \@lines;  
   }          if ( uc( $results->{'File Name'} ) ne uc($file) ) {
 }              $l->sp( "File name ("
                     . $results->{'File Name'}
 sub read_conf                    . ") does not match config file ("
 {                    . $file
   my $file = shift;                    . ")!" );
   my %conf;              next;
   my $in_ip_list = 0;          }
   open my $fh, $file or die "Couldn't open file $file: $!";          $l->p("File name matches . . . ");
   while (<$fh>) {  
     chomp;          my $image_type = 'mainimage';
     next if /^#/;          if ( $fw_type eq 'FPGA' ) {
     next if /^$/;              $image_type = 'fpgaimage';
     if ($in_ip_list) {          }
       push @{ $conf{'ips'} }, $_;          $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
     } else {          unless (
       my ($key, $val) = split /\s+/, $_, 2;              $results = $t->updateflash(
                   args => $image_type . ' '
       if (lc($key) eq 'ips') {                    . $ver->{ $fw_type . ' Checksum' } . ' '
         $in_ip_list = 1;                    . $conf->{'cksum'},
         next;                  Timeout => 90,
       }              )
             )
       $conf{ lc($key) } = $val;          {
     }              $l->sp("Couldn't update flash: $!");
   }              next;
   close $fh;          }
   
   #print Dump \%conf;          unless ( defined $results->{'Checksum'}
   foreach (              && $results->{'Checksum'} eq $conf->{'cksum'} )
     'password',          {
     'file_name', 'file_size', 'file_cksum',              $l->sp( "Saved checksum "
     'ver', 'cksum', 'ips',                    . $results->{'Checksum'}
   ) {                    . " does not match config file "
     die "No $_ specified in config file!"                    . $conf->{'cksum'}
       if (not exists $conf{$_});                    . "!" );
   }              next;
           }
   return \%conf;  
 }          $l->p("$fw_type saved checksum matches . . . ");
   
           return 1;
       }
   }
   
   sub parse_hosts {
       my $src = shift;
   
       my @hosts;
       foreach my $h ( @{$src} ) {
           if ( $h->{name} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/ )
           {
               for ( $2 .. $3 ) {
                   my %cur_host;
                   foreach my $k ( keys %{$h} ) {
                       $cur_host{$k} = $h->{$k};
                   }
                   $cur_host{name} = $1 . $_;
                   if ( !grep { $cur_host{name} eq $h->{name} } @hosts ) {
                       push @hosts, \%cur_host;
                   }
               }
           }
           else {
               push @hosts, $h;
           }
       }
   
       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.3  
changed lines
  Added in v.1.34

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