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

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

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