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

version 1.3, 2005/11/16 20:48:48 version 1.33, 2007/02/07 22:07:35
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.32 2007/02/07 19:25:05 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");
   my $vals = cmd($host, 'tftpd on', 'Success.');                  $t->bye;
                   next;
   if ($vals->{'Tftpd'} eq 'listen') {              }
     return $vals;  
   } else {          }
     return undef;  
   }          if ($needs_reboot) {
 }              $l->sp("Rebooting $host->{name}");
               $t->reboot;
 sub check_tftpd          }
 {          else {
   my $host = shift;              $l->sp("Bye $host->{name}");
   return cmd($host, 'tftpd', 'Success.');              $t->bye();
 }          }
       }
 sub sysinfo  
 {      if ( !$processed ) {
   my $host = shift;          $l->sp("");
   return cmd($host, 'sysinfo', 'Success.');          $l->sp("Finished.  No more hosts.");
 }          last;
       }
 sub updateflash  }
 {  
   my $host = shift;  sub upload {
   my $old = shift;      my $t    = shift;
   my $new = shift;      my $conf = shift;
   
   return undef unless $new;      my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
   
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);      my $fw_type = $conf->{firmware_type};
 }  
       my $ver = $t->ver;
 sub cmd  
 {      if (
   my $host   = shift;          !(
   my $string = shift;              $ver->{ $fw_type . ' Version' } && $ver->{ $fw_type . ' Checksum' }
   my $expect_last = shift;          )
   my $timeout = shift || 5;        )
       {
   my @lines = $host->cmd(String => $string, Timeout => $timeout);          $l->sp("Error getting current version numbers");
           return;
   my $vals = decode_lines(@lines);      }
   
   my $last = $host->lastline;      if (   $ver->{ $fw_type . ' Version' } eq $conf->{'ver'}
           && $ver->{ $fw_type . ' Checksum' } eq $conf->{'cksum'} )
   unless ($expect_last) {      {
     return $vals;          return 0;
   }      }
   
   if ($last =~ /$expect_last$/) {      $l->sp("Updating $fw_type");
     return $vals;      $l->sp("Config information:");
   } else {      $l->sp("  Hardware Type: $conf->{'type'}");
     warn "Error with command ($string): $last";      $l->sp("  File Name:     $conf->{'file_name'}");
     return undef;      $l->sp("  File Size:     $conf->{'file_size'}");
   }      $l->sp("  File Checksum: $conf->{'file_cksum'}");
 }      $l->sp("  Conf Version:  $conf->{'ver'}");
       $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
 sub decode_lines      $l->sp("  Conf Checksum: $conf->{'cksum'}");
 {      $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
   my @lines = @_;  
       my $try = 0;
   my %conf;      while (1) {
           if ( $try >= $max_tries ) {
   my $key = '';              $l->sp("Couldn't update in $max_tries tries!");
   my $val = '';              return;
   my $in_key = 0;          }
   my $in_val = 0;          $try++;
   
   foreach my $line (@lines) {          $l->p("Enabling TFTPd");
     my @chars = split //, $line;          unless ( $t->enable_tftpd ) {
               $l->sp("Couldn't enable tftpd");
     my $last_key = '';              next;
     foreach my $c (@chars) {          }
   
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {          $l->p("Uploading file ($conf->{file_name})");
         if ($c eq '[') {  
           $in_key = 1;          # use tftp to push the file up
           $in_val = 0;          my $tftp = Net::TFTP->new( $t->Host, Mode => 'octet' );
         } else {  
           $in_key = 0;          unless ( $tftp->put( $file, $file ) ) {
           $in_val = 0;              $l->sp( "Error uploading: " . $tftp->error );
         }              next;
           }
         if ($key) {  
           $key =~ s/^\s+//;          $l->p("Checking upload ($conf->{'file_cksum'})");
           $key =~ s/\s+$//;          my $results = $t->tftpd;
   
           $val =~ s/^\s+//;          # check the 'File Length' against ???
           $val =~ s/\s+$//;          if (
               !(
           if ($key eq 'Checksum' && $last_key) {                     $results->{'File Checksum'}
             # Special case for these bastids.                  && $results->{'File Length'}
             my $new = $last_key;                  && $results->{'File Name'}
             $new =~ s/\s+\S+$//;              )
             $key = $new . " " . $key;            )
           }          {
               $l->sp("Unable to get results of upload");
           $last_key = $key;              next;
           $conf{$key} = $val;          }
           $key = '';          if ( $results->{'File Checksum'} ne $conf->{'file_cksum'} ) {
           $val = '';              $l->sp( "File checksum ("
         }                    . $results->{'File Checksum'}
                     . ") does not match config file ("
       } elsif ($c eq ']') {                    . $conf->{'file_cksum'}
         $in_val = 1;                    . ")!" );
         $in_key = 0;              next;
         $c = shift @chars;          }
           $l->p("File checksum matches . . . ");
       } elsif ($in_key) {  
         $key .= $c;          if ( $results->{'File Length'} !~ /^$conf->{'file_size'} bytes/ ) {
               $l->sp( "File length ("
       } elsif ($in_val) {                    . $results->{'File Length'}
         $val .= $c;                    . ") does not match config file ("
       }                    . $conf->{'file_size'}
     }                    . " bytes)!" );
   }              next;
   #print Dump \%conf;          }
           $l->p("File length matches . . . ");
   if (%conf) {  
     return \%conf;          if ( uc( $results->{'File Name'} ) ne uc($file) ) {
   } else {              $l->sp( "File name ("
     return \@lines;                    . $results->{'File Name'}
   }                    . ") does not match config file ("
 }                    . $file
                     . ")!" );
 sub read_conf              next;
 {          }
   my $file = shift;          $l->p("File name matches . . . ");
   my %conf;  
   my $in_ip_list = 0;          my $image_type = 'mainimage';
   open my $fh, $file or die "Couldn't open file $file: $!";          if ( $fw_type eq 'FPGA' ) {
   while (<$fh>) {              $image_type = 'fpgaimage';
     chomp;          }
     next if /^#/;          $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
     next if /^$/;          unless (
     if ($in_ip_list) {              $results = $t->updateflash(
       push @{ $conf{'ips'} }, $_;                  args => $image_type . ' '
     } else {                    . $ver->{ $fw_type . ' Checksum' } . ' '
       my ($key, $val) = split /\s+/, $_, 2;                    . $conf->{'cksum'},
                   Timeout => 90,
       if (lc($key) eq 'ips') {              )
         $in_ip_list = 1;            )
         next;          {
       }              $l->sp("Couldn't update flash: $!");
               next;
       $conf{ lc($key) } = $val;          }
     }  
   }          unless ( defined $results->{'Checksum'}
   close $fh;              && $results->{'Checksum'} eq $conf->{'cksum'} )
           {
   #print Dump \%conf;              $l->sp( "Saved checksum "
   foreach (                    . $results->{'Checksum'}
     'password',                    . " does not match config file "
     'file_name', 'file_size', 'file_cksum',                    . $conf->{'cksum'}
     'ver', 'cksum', 'ips',                    . "!" );
   ) {              next;
     die "No $_ specified in config file!"          }
       if (not exists $conf{$_});  
   }          $l->p("$fw_type saved checksum matches . . . ");
   
   return \%conf;          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;
   
   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 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.3  
changed lines
  Added in v.1.33

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