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

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

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