[BACK]Return to update_trango.pl CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / scripts

Diff for /trango/Net-Telnet-Trango/scripts/update_trango.pl between version 1.8 and 1.32

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

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.32

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