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

version 1.5, 2005/11/16 21:39:51 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.4 2005/11/16 21:13:39 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 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! - Bye $host->{name}");
     ) or die "Couldn't update flash: " . $host->lastline;                  $l->e("Error updating $firmware_type on $host->{name}" .
     unless (                      "(try $host->{tries})");
       defined $results->{'Checksum'} &&                  $t->bye;
       $results->{'Checksum'} eq $conf->{'cksum'}                  # don't try any other firmware, don't want to reboot
     ) {                  last;
       mylog("Saved checksum does not match config file!", LOG_SAVE);              }
       next;  
     }          }
     mylog("Uploaded checksum ($results->{'Checksum'}) " .  
           "matches ($conf->{'cksum'})");          if ($needs_reboot) {
                   $l->sp("Rebooting $host->{name}");
     mylog("Successfully updated!", LOG_SAVE);              $t->reboot;
     return 1;          }
   }          else {
 }              $l->sp("Bye $host->{name}");
               $t->bye();
 sub get_ver          }
 {      }
   my $host = shift;  
   return cmd($host, 'ver');      if ( !$processed ) {
 }          $l->sp("");
           $l->sp("Finished.  No more hosts.");
 sub enable_tftpd          last;
 {      }
   my $host = shift;  }
   
   my $vals = cmd($host, 'tftpd on', 'Success.');  sub upload {
       my $t    = shift;
   if ($vals->{'Tftpd'} eq 'listen') {      my $conf = shift;
     return $vals;  
   } else {      my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
     return undef;  
   }      my $fw_type = $conf->{firmware_type};
 }  
       my $ver = $t->ver;
 sub check_tftpd  
 {      if (
   my $host = shift;          !(
   return cmd($host, 'tftpd', 'Success.');              $ver->{ $fw_type . ' Version' } && $ver->{ $fw_type . ' Checksum' }
 }          )
         )
 sub sysinfo      {
 {          $l->sp("Error getting current version numbers");
   my $host = shift;          return;
   return cmd($host, 'sysinfo', 'Success.');      }
 }  
       if (   $ver->{ $fw_type . ' Version' } eq $conf->{'ver'}
 sub updateflash          && $ver->{ $fw_type . ' Checksum' } eq $conf->{'cksum'} )
 {      {
   my $host = shift;          return 0;
   my $old = shift;      }
   my $new = shift;  
       $l->sp("Updating $fw_type");
   return undef unless $new;      $l->sp("Config information:");
       $l->sp("  Hardware Type: $conf->{'type'}");
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);      $l->sp("  File Name:     $conf->{'file_name'}");
 }      $l->sp("  File Size:     $conf->{'file_size'}");
       $l->sp("  File Checksum: $conf->{'file_cksum'}");
 sub cmd      $l->sp("  Conf Version:  $conf->{'ver'}");
 {      $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
   my $host   = shift;      $l->sp("  Conf Checksum: $conf->{'cksum'}");
   my $string = shift;      $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
   my $expect_last = shift;  
   my $timeout = shift || 5;      my $try = 0;
       while (1) {
   my @lines = $host->cmd(String => $string, Timeout => $timeout);          if ( $try >= $max_tries ) {
               $l->sp("Couldn't update in $max_tries tries!");
   my $vals = decode_lines(@lines);              return;
           }
   my $last = $host->lastline;          $try++;
   
   unless ($expect_last) {          $l->p("Enabling TFTPd");
     return $vals;          unless ( $t->enable_tftpd ) {
   }              $l->sp("Couldn't enable tftpd");
               next;
   if ($last =~ /$expect_last$/) {          }
     return $vals;  
   } else {          $l->p("Uploading file ($conf->{file_name})");
     warn "Error with command ($string): $last";  
     return undef;          # use tftp to push the file up
   }          my $tftp = Net::TFTP->new( $t->Host, Mode => 'octet' );
 }  
           unless ( $tftp->put( $file, $file ) ) {
 sub decode_lines              $l->sp( "Error uploading: " . $tftp->error );
 {              next;
   my @lines = @_;          }
   
   my %conf;          $l->p("Checking upload ($conf->{'file_cksum'})");
           my $results = $t->tftpd;
   my $key = '';  
   my $val = '';          # check the 'File Length' against ???
   my $in_key = 0;          if (
   my $in_val = 0;              !(
                      $results->{'File Checksum'}
   foreach my $line (@lines) {                  && $results->{'File Length'}
     my @chars = split //, $line;                  && $results->{'File Name'}
               )
     my $last_key = '';            )
     foreach my $c (@chars) {          {
               $l->sp("Unable to get results of upload");
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {              next;
         if ($c eq '[') {          }
           $in_key = 1;          if ( $results->{'File Checksum'} ne $conf->{'file_cksum'} ) {
           $in_val = 0;              $l->sp( "File checksum ("
         } else {                    . $results->{'File Checksum'}
           $in_key = 0;                    . ") does not match config file ("
           $in_val = 0;                    . $conf->{'file_cksum'}
         }                    . ")!" );
               next;
         if ($key) {          }
           $key =~ s/^\s+//;          $l->p("File checksum matches . . . ");
           $key =~ s/\s+$//;  
           if ( $results->{'File Length'} !~ /^$conf->{'file_size'} bytes/ ) {
           $val =~ s/^\s+//;              $l->sp( "File length ("
           $val =~ s/\s+$//;                    . $results->{'File Length'}
                     . ") does not match config file ("
           if ($key eq 'Checksum' && $last_key) {                    . $conf->{'file_size'}
             # Special case for these bastids.                    . " bytes)!" );
             my $new = $last_key;              next;
             $new =~ s/\s+\S+$//;          }
             $key = $new . " " . $key;          $l->p("File length matches . . . ");
           }  
           if ( uc( $results->{'File Name'} ) ne uc($file) ) {
           $last_key = $key;              $l->sp( "File name ("
           $conf{$key} = $val;                    . $results->{'File Name'}
           $key = '';                    . ") does not match config file ("
           $val = '';                    . $file
         }                    . ")!" );
               next;
       } elsif ($c eq ']') {          }
         $in_val = 1;          $l->p("File name matches . . . ");
         $in_key = 0;  
         $c = shift @chars;          my $image_type = 'mainimage';
           if ( $fw_type eq 'FPGA' ) {
       } elsif ($in_key) {              $image_type = 'fpgaimage';
         $key .= $c;          }
           $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
       } elsif ($in_val) {          unless (
         $val .= $c;              $results = $t->updateflash(
       }                  args => $image_type . ' '
     }                    . $ver->{ $fw_type . ' Checksum' } . ' '
   }                    . $conf->{'cksum'},
   #print Dump \%conf;                  Timeout => 90,
               )
   if (%conf) {            )
     return \%conf;          {
   } else {              $l->sp("Couldn't update flash: $!");
     return \@lines;              next;
   }          }
 }  
           unless ( defined $results->{'Checksum'}
 sub read_conf              && $results->{'Checksum'} eq $conf->{'cksum'} )
 {          {
   my $file = shift;              $l->sp( "Saved checksum "
   my %conf;                    . $results->{'Checksum'}
   my $in_ip_list = 0;                    . " does not match config file "
   open my $fh, $file or die "Couldn't open file $file: $!";                    . $conf->{'cksum'}
   while (<$fh>) {                    . "!" );
     chomp;              next;
     next if /^#/;          }
     next if /^$/;  
     if ($in_ip_list) {          $l->p("$fw_type saved checksum matches . . . ");
       push @{ $conf{'ips'} }, $_;  
     } else {          return 1;
       my ($key, $val) = split /\s+/, $_, 2;      }
   }
       if (lc($key) eq 'ips') {  
         $in_ip_list = 1;  sub parse_hosts {
         next;      my $src = shift;
       }  
       my @hosts;
       $conf{ lc($key) } = $val;      foreach my $h ( @{$src} ) {
     }          if ( $h->{name} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/ )
   }          {
   close $fh;              for ( $2 .. $3 ) {
                   my %cur_host;
   #print Dump \%conf;                  foreach my $k ( keys %{$h} ) {
   foreach (                      $cur_host{$k} = $h->{$k};
     'password',                  }
     'file_name', 'file_size', 'file_cksum',                  $cur_host{name} = $1 . $_;
     'ver', 'cksum', 'ips',                  if ( !grep { $cur_host{name} eq $h->{name} } @hosts ) {
   ) {                      push @hosts, \%cur_host;
     die "No $_ specified in config file!"                  }
       if (not exists $conf{$_});              }
   }          }
           else {
   return \%conf;              push @hosts, $h;
 }          }
       }
 sub mylog  
 {      return \@hosts;
   my $thing = shift;  }
   chomp $thing;  
   my $save = shift;  package Mylogger;
   
   print $thing, "\n";  use Fcntl ':flock';    # import LOCK_* constants
   
   if (defined $save && $save == LOG_SAVE) {  #use YAML;
     unless ($MYLOG) {  use constant LOG_PRINT => 128;
       open ($MYLOG, '>>', $log_file)  use constant LOG_SAVE  => 64;
         or die "Couldn't open logfile!\n";  use constant LOG_ERR   => 1;
       my $ofh = select $MYLOG;  
       $|=1;  DESTROY {
       select $ofh;      my $self = shift;
       mylog("Opened log ($log_file)");      if ( $self->{'MYLOG'} ) {
     }          $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
           close $self->{'MYLOG'};
     flock($MYLOG, LOCK_EX);      }
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"  }
       or die "Couldn't print to MYLOG: $!";  
     flock($MYLOG, LOCK_UN);  sub new {
   }      my $package = shift;
 }      my $self = shift || {};
   
 sub GetLogName      $self->{'base_path'}  ||= '.';
 {      $self->{'log_path'}   ||= $self->{'base_path'};
   my $prefix  = shift || die "Invalid prefix passed for log";      $self->{'log_prefix'} ||= 'LOG';
       $self->{'log_file'} ||=
   my $logdate = GetLogDate();        GetLogName( $self->{'log_prefix'}, $self->{'log_path'} );
   my $logver  = 0;      bless $self, $package;
   my $logname;  }
   
   do {  sub s {
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';      my $self = shift;
     $logver++;      my $m    = shift;
   } until (not -e $logname);      return $self->mylog( $m, LOG_SAVE );
   }
   return $logname;  
 }  sub p {
       my $self = shift;
 sub GetLogDate      my $m    = shift;
 {      return $self->mylog( $m, LOG_PRINT );
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();  }
   
   $mon++;  sub sp {
   $year += 1900;      my $self = shift;
       my $m    = shift;
   if ($min  < 10) { $min  = "0$min"  }      return $self->mylog( $m, LOG_SAVE | LOG_PRINT );
   if ($sec  < 10) { $sec  = "0$sec"  }  }
   if ($hour < 10) { $hour = "0$hour" }  
   if ($mday < 10) { $mday = "0$mday" }  sub e {
   if ($mon  < 10) { $mon  = "0$mon"  }      my $self = shift;
       my $m    = shift;
   my $time = $year . $mon . $mday;      return $self->mylog( $m, LOG_ERR );
   }
   return $time;  
 }  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.5  
changed lines
  Added in v.1.34

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