[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.8 and 1.33

version 1.8, 2005/11/17 20:20:59 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.7 2005/11/17 20:09:45 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 Fcntl ':flock'; # import LOCK_* constants  use strict;
 #use YAML;  use warnings;
 use constant LOG_SAVE => 1;  
   use YAML qw/ LoadFile /;
   use Net::TFTP;
 my $config_file = shift || 'update_trango.conf';  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'");
   my $conf = LoadFile($config_file);
 my $log_file =  GetLogName('UT');  
 my $MYLOG;  # file handle for logging so we don't have to close and open it all the time  my $hosts;
 END {  if (@ARGV) {
   if ($MYLOG) {      @{$hosts} = map { { name => $_, group => 'Trango-Client' } } @ARGV;
     mylog("Closing log ($log_file)");  }
     close $MYLOG;  else {
   }      $hosts = parse_hosts( $conf->{hosts} );
 }  }
   
   #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
 mylog("Reading config file '$config_file'", LOG_SAVE);  
 my $conf = read_conf($config_file);  my $global_tries = $max_tries * 2;
   while ( $global_tries > 0 ) {
 mylog("  Hardware Type: $conf->{'type'}", LOG_SAVE);      $global_tries--;
 mylog("  File Name:     $conf->{'file_name'}", LOG_SAVE);      my $processed = 0;
 mylog("  File Size:     $conf->{'file_size'}", LOG_SAVE);  
 mylog("  File Checksum: $conf->{'file_cksum'}", LOG_SAVE);      foreach my $host ( @{$hosts} ) {
 mylog("  FW Version:    $conf->{'ver'}", LOG_SAVE);  
 mylog("  FW Checksum:   $conf->{'cksum'}", LOG_SAVE);          if ( !exists $host->{retry} ) {
 mylog("", LOG_SAVE);              $host->{tries} = 0;
               $host->{retry} = 1;
           }
   
           if ( $host->{tries} >= $max_tries ) {
               $host->{retry} = 0;
           }
 foreach my $fox (@{ $conf->{'ips'} }) {  
   mylog("Updating: $fox", LOG_SAVE);          if ( $host->{retry} <= 0 ) {
               next;
   ## Connect and login.          }
   my $host = new Net::Telnet (Timeout => 5,  
                               Prompt => '/#> *$/');          $host->{tries}++;
   mylog("Connecting to $fox");          $processed++;
   unless ( $host->open( Host => $fox, Errmode => 'return') ) {  
     mylog("Couldn't connect to $fox.  Connection timed out.", LOG_SAVE);          $l->sp("");
     next;          $l->sp("Checking: $host->{name} (try $host->{tries})");
   }          my $needs_reboot = 0;
   $host->dump_log('dump.log');  
           ## Connect and login.
   ## Login to remote host.          my $t = new Net::Telnet::Trango(
   unless ($host->waitfor(              Timeout => 5,
     -match => '/password: ?$/i',              Errmode => 'return',
     -errmode => "return",          ) or die "Couldn't make new connection: $!";
   ) ) {          $l->p("Connecting to $host->{name}");
     mylog("problem connecting to host ($fox): " . $host->lastline, LOG_SAVE);          unless ( $t->open( $host->{name} ) ) {
     next;              $l->sp("Error connecting: $!");
   }              next;
           }
   my $login_banner = $host->lastline;  
   my ($type, $version) = $login_banner =~          my $password = $host->{Telnet_Password} || $conf->{general}->{password};
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;  
           $l->p("Logging in");
   if ($type ne $conf->{'type'}) {          $t->login($password);
     mylog("Wrong type of unit ('$type' should be '$conf->{'type'}')", LOG_SAVE);          unless ($t->logged_in) {
     $host->close;              $l->p('Failed!');
     next;              $t->close;
   }              next;
           }
   if ($version eq $conf->{'ver'}) {  
     mylog("Already up to date with firmware version '$version'", LOG_SAVE);          $l->sp("Getting sudb");
     $host->close;          my $sudb = $t->sudb_view;
     next;          if ($sudb) {
   }              foreach my $su (@{ $sudb }) {
                   $l->p("Getting su info $su->{suid}");
   mylog("Logging in");                  my $su_info = $t->su_info( $su->{suid} );
   $host->print($conf->{'password'});                  if ($su_info->{ip}) {
   unless ($host->waitfor(                      if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
     -match => $host->prompt,                          $l->p("Already have $su_info->{ip}");
     -errmode => "return",                          next;
   ) ) {                      }
     mylog("login ($fox) failed: " . $host->lastline);                      $l->sp("Adding host $su_info->{ip}");
     next;                      my $new_host = {
   }                          password => $host->{password},
                           name     => $su_info->{ip},
                           remarks  => $su_info->{remarks},
   mylog("Sending commands");                      };
   ## Send commands                      push @{ $hosts }, $new_host;
   if ( upload($host, $fox, $conf->{'file_name'}) ) {                  } else {
     mylog("Rebooting");                      $l->sp("Couldn't get su info for $su->{suid}");
     $host->print("reboot\n");                      $l->sp("ERR: " . $t->last_error);
     $host->getline;                  }
   } else {              }
     mylog("Exiting");          }
     $host->print("exit\n");  
     $host->getline;          foreach my $firmware_type ( 'Firmware', 'FPGA' ) {
   }  
   $host->close;              if ( !exists $conf->{$firmware_type} ) {
   mylog("", LOG_SAVE);                  $l->s("No configs for '$firmware_type'");
 }                  next;
               }
 sub upload  
 {              my $host_type = $t->host_type;
   my $host = shift;              if ( $firmware_type eq 'FPGA' ) {
   my $fox  = shift;                  $host_type =~ s/\s.*$//;
   my $file = shift;              }
   
   mylog("Getting current version");              if ( !exists $conf->{$firmware_type}->{$host_type} ) {
   my $ver = get_ver($host);                  $l->sp("No '$firmware_type' config for type $host_type");
                   next;
   if (              }
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&  
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}              if (   $firmware_type eq 'Firmware'
   ) {                  && $t->firmware_version eq
     mylog("Already updated!", LOG_SAVE);                  $conf->{$firmware_type}->{$host_type}->{ver} )
     return 1;              {
   }                  $l->sp("Firmware already up to date");
                   next;
   my $try = 0;              }
   while (1) {  
     if ($try >= $max_tries) {              if ( !$t->logged_in ) {
       mylog("Couldn't update in $max_tries tries!");                  $l->p("Logging in");
       return undef;                  $t->login($password);
     }                  unless ($t->logged_in) {
     $try++;                      $l->p('Failed!');
                       $t->close;
     #sysinfo($host);                      last;
                       }
     mylog("Enabling TFTPd");              }
     enable_tftpd($host) || die "Couldn't enable tftpd";  
               foreach my $k ( keys %{ $conf->{general} } ) {
     mylog("Uploading file ($file)");                  $conf->{$firmware_type}->{$host_type}->{$k} ||=
     # use tftp to push the file up                    $conf->{general}->{$k};
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');              }
               $conf->{$firmware_type}->{$host_type}->{firmware_type} ||=
     $tftp->put($file, $file)                $firmware_type;
       or die "Error uploading: " . $tftp->error;              $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
   
     # waitfor some sort of output              $l->sp("$host_type $firmware_type");
     # make sure it says 'Success.' otherwise error              ## Send commands
     #print "LAST: " . $host->lastline;              my $rc = upload( $t, $conf->{$firmware_type}->{$host_type} );
     #my @lines = $host->getlines;              if ($rc) {
     #print Dump \@lines;                  $l->sp("Successfull!");
                       $host->{retry}--;
     mylog("Checking upload ($conf->{'file_cksum'})");                  $needs_reboot = 1;
     my $results = check_tftpd($host);              }
     # check the 'File Length' against ???              elsif ( defined $rc ) {
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {                  $l->sp("Already up to date");
       mylog("File checksum does not match config file!", LOG_SAVE);                  $host->{retry}--;
       next;              }
     }              else {
     mylog("File checksum ($results->{'File Checksum'}) " .                  $l->sp("Failed");
           "matches ($conf->{'file_cksum'})");                  $t->bye;
                   next;
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {              }
       mylog("File length does not match config file!", LOG_SAVE);  
       next;          }
     }  
     mylog("File length ($results->{'File Length'}) " .          if ($needs_reboot) {
           "matches ($conf->{'file_size'})");              $l->sp("Rebooting $host->{name}");
               $t->reboot;
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {          }
       mylog("File name does not match config file!", LOG_SAVE);          else {
       next;              $l->sp("Bye $host->{name}");
     }              $t->bye();
     mylog("File name ($results->{'File Name'}) " .          }
           "matches ($conf->{'file_name'})");      }
   
     mylog("Updating flash (new checksum '$conf->{'cksum'}')");      if ( !$processed ) {
     $results = updateflash(          $l->sp("");
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}          $l->sp("Finished.  No more hosts.");
     ) or die "Couldn't update flash: " . $host->lastline;          last;
     unless (      }
       defined $results->{'Checksum'} &&  }
       $results->{'Checksum'} eq $conf->{'cksum'}  
     ) {  sub upload {
       mylog("Saved checksum does not match config file!", LOG_SAVE);      my $t    = shift;
       next;      my $conf = shift;
     }  
     mylog("Uploaded checksum ($results->{'Checksum'}) " .      my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
           "matches ($conf->{'cksum'})");  
           my $fw_type = $conf->{firmware_type};
     mylog("Successfully updated!", LOG_SAVE);  
     return 1;      my $ver = $t->ver;
   }  
 }      if (
           !(
 sub get_ver              $ver->{ $fw_type . ' Version' } && $ver->{ $fw_type . ' Checksum' }
 {          )
   my $host = shift;        )
   return cmd($host, 'ver');      {
 }          $l->sp("Error getting current version numbers");
           return;
 sub enable_tftpd      }
 {  
   my $host = shift;      if (   $ver->{ $fw_type . ' Version' } eq $conf->{'ver'}
           && $ver->{ $fw_type . ' Checksum' } eq $conf->{'cksum'} )
   my $vals = cmd($host, 'tftpd on', 'Success.');      {
           return 0;
   if ($vals->{'Tftpd'} eq 'listen') {      }
     return $vals;  
   } else {      $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 check_tftpd      $l->sp("  File Checksum: $conf->{'file_cksum'}");
 {      $l->sp("  Conf Version:  $conf->{'ver'}");
   my $host = shift;      $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
   return cmd($host, 'tftpd', 'Success.');      $l->sp("  Conf Checksum: $conf->{'cksum'}");
 }      $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
   
 sub sysinfo      my $try = 0;
 {      while (1) {
   my $host = shift;          if ( $try >= $max_tries ) {
   return cmd($host, 'sysinfo', 'Success.');              $l->sp("Couldn't update in $max_tries tries!");
 }              return;
           }
 sub updateflash          $try++;
 {  
   my $host = shift;          $l->p("Enabling TFTPd");
   my $old = shift;          unless ( $t->enable_tftpd ) {
   my $new = shift;              $l->sp("Couldn't enable tftpd");
               next;
   return undef unless $new;          }
   
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);          $l->p("Uploading file ($conf->{file_name})");
 }  
           # use tftp to push the file up
 sub cmd          my $tftp = Net::TFTP->new( $t->Host, Mode => 'octet' );
 {  
   my $host   = shift;          unless ( $tftp->put( $file, $file ) ) {
   my $string = shift;              $l->sp( "Error uploading: " . $tftp->error );
   my $expect_last = shift;              next;
   my $timeout = shift || 5;          }
   
   my @lines = $host->cmd(String => $string, Timeout => $timeout);          $l->p("Checking upload ($conf->{'file_cksum'})");
           my $results = $t->tftpd;
   my $vals = decode_lines(@lines);  
           # check the 'File Length' against ???
   my $last = $host->lastline;          if (
               !(
   unless ($expect_last) {                     $results->{'File Checksum'}
     return $vals;                  && $results->{'File Length'}
   }                  && $results->{'File Name'}
               )
   if ($last =~ /$expect_last$/) {            )
     return $vals;          {
   } else {              $l->sp("Unable to get results of upload");
     warn "Error with command ($string): $last";              next;
     return undef;          }
   }          if ( $results->{'File Checksum'} ne $conf->{'file_cksum'} ) {
 }              $l->sp( "File checksum ("
                     . $results->{'File Checksum'}
 sub decode_lines                    . ") does not match config file ("
 {                    . $conf->{'file_cksum'}
   my @lines = @_;                    . ")!" );
               next;
   my %conf;          }
           $l->p("File checksum matches . . . ");
   my $key = '';  
   my $val = '';          if ( $results->{'File Length'} !~ /^$conf->{'file_size'} bytes/ ) {
   my $in_key = 0;              $l->sp( "File length ("
   my $in_val = 0;                    . $results->{'File Length'}
                     . ") does not match config file ("
   foreach my $line (@lines) {                    . $conf->{'file_size'}
     my @chars = split //, $line;                    . " bytes)!" );
               next;
     my $last_key = '';          }
     foreach my $c (@chars) {          $l->p("File length matches . . . ");
   
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {          if ( uc( $results->{'File Name'} ) ne uc($file) ) {
         if ($c eq '[') {              $l->sp( "File name ("
           $in_key = 1;                    . $results->{'File Name'}
           $in_val = 0;                    . ") does not match config file ("
         } else {                    . $file
           $in_key = 0;                    . ")!" );
           $in_val = 0;              next;
         }          }
           $l->p("File name matches . . . ");
         if ($key) {  
           $key =~ s/^\s+//;          my $image_type = 'mainimage';
           $key =~ s/\s+$//;          if ( $fw_type eq 'FPGA' ) {
               $image_type = 'fpgaimage';
           $val =~ s/^\s+//;          }
           $val =~ s/\s+$//;          $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
           unless (
           if ($key eq 'Checksum' && $last_key) {              $results = $t->updateflash(
             # Special case for these bastids.                  args => $image_type . ' '
             my $new = $last_key;                    . $ver->{ $fw_type . ' Checksum' } . ' '
             $new =~ s/\s+\S+$//;                    . $conf->{'cksum'},
             $key = $new . " " . $key;                  Timeout => 90,
           }              )
             )
           $last_key = $key;          {
           $conf{$key} = $val;              $l->sp("Couldn't update flash: $!");
           $key = '';              next;
           $val = '';          }
         }  
           unless ( defined $results->{'Checksum'}
       } elsif ($c eq ']') {              && $results->{'Checksum'} eq $conf->{'cksum'} )
         $in_val = 1;          {
         $in_key = 0;              $l->sp( "Saved checksum "
         $c = shift @chars;                    . $results->{'Checksum'}
                     . " does not match config file "
       } elsif ($in_key) {                    . $conf->{'cksum'}
         $key .= $c;                    . "!" );
               next;
       } elsif ($in_val) {          }
         $val .= $c;  
       }          $l->p("$fw_type saved checksum matches . . . ");
     }  
   }          return 1;
   #print Dump \%conf;      }
   }
   if (%conf) {  
     return \%conf;  sub parse_hosts {
   } else {      my $src = shift;
     return \@lines;  
   }      my @hosts;
 }      foreach my $h ( @{$src} ) {
           if ( $h->{name} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/ )
 sub read_conf          {
 {              for ( $2 .. $3 ) {
   my $file = shift;                  my %cur_host;
   my %conf;                  foreach my $k ( keys %{$h} ) {
   my $in_ip_list = 0;                      $cur_host{$k} = $h->{$k};
   open my $fh, $file or die "Couldn't open file $file: $!";                  }
   while (<$fh>) {                  $cur_host{name} = $1 . $_;
     chomp;                  if ( !grep { $cur_host{name} eq $h->{name} } @hosts ) {
     next if /^#/;                      push @hosts, \%cur_host;
     next if /^$/;                  }
     if ($in_ip_list) {              }
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {          }
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);          else {
       } else {              push @hosts, $h;
         push @{ $conf{'ips'} }, $_;          }
       }      }
     } else {  
       my ($key, $val) = split /\s+/, $_, 2;      return \@hosts;
   }
       if (lc($key) eq 'ips') {  
         $in_ip_list = 1;  package Mylogger;
         next;  
       }  use Fcntl ':flock';    # import LOCK_* constants
   
       $conf{ lc($key) } = $val;  #use YAML;
     }  use constant LOG_PRINT => 128;
   }  use constant LOG_SAVE  => 64;
   close $fh;  
   DESTROY {
   #print Dump \%conf;      my $self = shift;
   foreach (      if ( $self->{'MYLOG'} ) {
     'password',          $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
     'file_name', 'file_size', 'file_cksum',          close $self->{'MYLOG'};
     'ver', 'cksum', 'ips',      }
   ) {  }
     die "No $_ specified in config file!"  
       if (not exists $conf{$_});  sub new {
   }      my $package = shift;
       my $self = shift || {};
   #print Dump \%conf;  
   #exit;      $self->{'base_path'}  ||= '.';
   return \%conf;      $self->{'log_path'}   ||= $self->{'base_path'};
 }      $self->{'log_prefix'} ||= 'LOG';
       $self->{'log_file'} ||=
 sub mylog        GetLogName( $self->{'log_prefix'}, $self->{'log_path'} );
 {      bless $self, $package;
   my $thing = shift;  }
   chomp $thing;  
   my $save = shift;  sub s {
       my $self = shift;
   print $thing, "\n";      my $m    = shift;
       return $self->mylog( $m, LOG_SAVE );
   if (defined $save && $save == LOG_SAVE) {  }
     unless ($MYLOG) {  
       open ($MYLOG, '>>', $log_file)  sub p {
         or die "Couldn't open logfile!\n";      my $self = shift;
       my $ofh = select $MYLOG;      my $m    = shift;
       $|=1;      return $self->mylog( $m, LOG_PRINT );
       select $ofh;  }
       mylog("Opened log ($log_file)");  
     }  sub sp {
       my $self = shift;
     flock($MYLOG, LOCK_EX);      my $m    = shift;
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"      return $self->mylog( $m, LOG_SAVE | LOG_PRINT );
       or die "Couldn't print to MYLOG: $!";  }
     flock($MYLOG, LOCK_UN);  
   }  sub mylog {
 }      my $self = shift;
   
 sub GetLogName      my $thing = shift;
 {      chomp $thing;
   my $prefix  = shift || die "Invalid prefix passed for log";  
       my $which = shift;
   my $logdate = GetLogDate();  
   my $logver  = 0;      my $MYLOG;
   my $logname;      if ( $which & LOG_PRINT ) {
           print $thing, "\n";
   do {      }
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';  
     $logver++;      if ( $which & LOG_SAVE ) {
   } until (not -e $logname);          if ( $self->{'MYLOG'} ) {
               $MYLOG = $self->{'MYLOG'};
   return $logname;          }
 }          else {
               unless ($MYLOG) {
 sub GetLogDate                  open( $MYLOG, '>>',
 {                      $self->{'log_path'} . '/' . $self->{'log_file'} )
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();                    or die "Couldn't open logfile!\n";
                   my $ofh = select $MYLOG;
   $mon++;                  $| = 1;
   $year += 1900;                  select $ofh;
                   $self->{'MYLOG'} = $MYLOG;
   if ($min  < 10) { $min  = "0$min"  }  
   if ($sec  < 10) { $sec  = "0$sec"  }                  $self->p(
   if ($hour < 10) { $hour = "0$hour" }                      "Opened log ($self->{'log_path'}/$self->{'log_file'})");
   if ($mday < 10) { $mday = "0$mday" }              }
   if ($mon  < 10) { $mon  = "0$mon"  }          }
           flock( $MYLOG, LOCK_EX );
   my $time = $year . $mon . $mday;          print $MYLOG ( scalar gmtime ), "\t", $thing, "\n"
             or die "Couldn't print to MYLOG: $!";
   return $time;          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.8  
changed lines
  Added in v.1.33

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