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

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

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