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

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

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