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

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

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