[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.4 and 1.34

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

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