[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.3 and 1.27

version 1.3, 2005/11/16 20:48:48 version 1.27, 2007/02/02 21:36:00
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.2 2005/11/16 05:26:37 andrew Exp $  # $RedRiver: update_trango.pl,v 1.25 2007/02/02 17:50:09 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 YAML;  use lib '.';
   use Net::Telnet::Trango;
   use RedRiver::Wireless;
 my $config_file = shift || 'fox53.conf';  
 my $conf = read_conf($config_file);  my $config_file = shift || 'update_trango.yaml';
   my $max_tries = 3;
 my $max_tries = 1;  
   my $l = Mylogger->new( { log_prefix => 'UT' } );
   
   $l->sp("Reading config file '$config_file'");
   my $conf = LoadFile($config_file);
   
   my $hosts;
 foreach my $fox (@{ $conf->{'ips'} }) {  if (@ARGV) {
   print STDERR "Updating: $fox\n";    @{ $hosts } = map { { name => $_, group => 'Trango-Client' } } @ARGV
   } else {
   ## Connect and login.    $l->sp("Reading hosts");
   my $host = new Net::Telnet (Timeout => 5,    $hosts = RedRiver::Wireless::Read_Hosts();
                               Prompt => '/#> *$/');  }
   print "Connecting to $fox\n";  
   $host->open($fox);  #@{ $hosts } = grep { $_->{name} eq '10.100.7.2' } @{ $hosts };
   $host->dump_log('dump.log');  
   my $global_tries = $max_tries * 2;
   ## Login to remote host.  while ($global_tries > 0) {
   $host->waitfor(    $global_tries--;
     -match => '/password: ?$/i',    my $processed = 0;
     -errmode => "return",  
   ) or die "problem connecting to host ($fox): ", $host->lastline;  
   foreach my $host (@{ $hosts }) {
   my $login_banner = $host->lastline;    next if $host->{group} !~ /^Trango/;
   my ($type, $version) = $login_banner =~  
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;    if (! exists $host->{retry}) {
       $host->{tries} = 0;
   if ($type ne $conf->{'type'}) {      $host->{retry} = 1;
     print STDERR "Wrong type of unit ('$type' should be '$conf->{'type'}')\n";    }
     $host->close;  
     next;    if ($host->{tries} >= $max_tries) {
   }      $host->{retry} = 0;
     }
   if ($version eq $conf->{'ver'}) {  
     print STDERR "Already up to date with firmware version '$version'\n";    if ($host->{retry} <= 0) {
     $host->close;      next;
     next;    }
   }  
     $host->{tries}++;
   print "Logging in\n";    $processed++;
   $host->print($conf->{'password'});  
   $host->waitfor(    $l->sp("");
     -match => $host->prompt,    $l->sp("Checking: $host->{name} (try $host->{tries})");
     -errmode => "return",    my $needs_reboot = 0;
   ) or die "login ($fox) failed: ", $host->lastline;  
     ## Connect and login.
     my $t = new Net::Telnet::Trango (
   print "Sending commands\n";      Timeout => 5,
   ## Send commands      Errmode => 'return',
   if ( upload($host, $fox, $conf->{'file_name'}) ) {    ) or die "Couldn't make new connection: $!";
     print "Rebooting\n";    $l->p("Connecting to $host->{name}");
     $host->print("reboot\n");    unless ( $t->open($host->{name}) ) {
     $host->getline;      $l->sp("Error connecting: $!");
   } else {      next;
     print "Exiting\n";    }
     $host->print("exit\n");  
     $host->getline;    my $password = $host->{Telnet_Password} || $conf->{general}->{password};
   }  
   $host->close;    # XXX I am not sure this is the best way to check if we should look for
 }    # XXX associated stations.
     if ($host->{group} !~ /Client$/) {
 sub upload      $l->p("Logging in");
 {      $t->login($password);
   my $host = shift;      unless ($t->logged_in) {
   my $fox  = shift;        $l->p('Failed!');
   my $file = shift;        $t->close;
         next;
   print "Getting current version\n";      }
   my $ver = get_ver($host);  
       $l->sp("Getting sudb");
   if (      my $sudb = $t->sudb_view;
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&      if ($sudb) {
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}        foreach my $su (@{ $sudb }) {
   ) {          $l->p("Getting su info $su->{suid}");
     print STDERR "Already updated!";          my $su_info = $t->su_info( $su->{suid} );
     return 1;          if ($su_info->{ip}) {
   }            if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
               $l->p("Already have $su_info->{ip}");
   my $try = 0;              next;
   while (1) {            }
     if ($try >= $max_tries) {            $l->sp("Adding host $su_info->{ip}");
       print STDERR "Couldn't update in $max_tries tries!";            my $new_host = {
       return undef;              Telnet_Password => $host->{Telnet_Password},
     }              group   => $host->{group} . '-Client',
     $try++;              name    => $su_info->{ip},
               remarks => $su_info->{remarks},
     #sysinfo($host);            };
                 push @{ $hosts }, $new_host;
     print "Enabling TFTPd\n";          } else {
     enable_tftpd($host) || die "Couldn't enable tftpd";            $l->sp("Couldn't get su info for $su->{suid}");
             if ($su_info->{ERR}) {
     print "Uploading file\n";              $l->sp("ERR: $su_info->{ERR}");
     # use tftp to push the file up            }
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');            next;
           }
     $tftp->put($file, $file)        }
       or die "Error uploading: " . $tftp->error;      }
     }
     # waitfor some sort of output  
     # make sure it says 'Success.' otherwise error    foreach my $firmware_type ('Firmware', 'FPGA') {
     #print "LAST: " . $host->lastline;  
     #my @lines = $host->getlines;      if (! exists $conf->{$firmware_type}) {
     #print Dump \@lines;        $l->s("No configs for '$firmware_type'");
             $t->close;
     print "Checking upload\n";        next;
     my $results = check_tftpd($host);      }
     # check the 'File Length' against ???  
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {      my $host_type = $t->host_type;
       print STDERR "File checksum does not match config file!";      if ($firmware_type eq 'FPGA') {
       next;        $host_type =~ s/\s.*$//;
     }      }
   
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {      if (! exists $conf->{$firmware_type}->{$host_type}) {
       print STDERR "File length does not match config file!";        $l->sp("No '$firmware_type' config for type $host_type");
       next;        $t->close;
     }        next;
       }
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {  
       print STDERR "File name does not match config file!";      if (! $t->logged_in) {
       next;        $l->p("Logging in");
     }        $t->login($password);
         unless ($t->logged_in) {
     print "Updating flash\n";          $l->p('Failed!');
     $results = updateflash(          $t->close;
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}          next;
     ) or die "Couldn't update flash: " . $host->lastline;        }
     unless (      }
       defined $results->{'Checksum'} &&  
       $results->{'Checksum'} eq $conf->{'cksum'}      foreach my $k (keys %{ $conf->{general} }) {
     ) {        $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
       print STDERR "Saved checksum does not match config file!";      }
       next;      $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
     }      $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
       
     print STDERR "Successfully updated!\n";      $l->sp("$host_type $firmware_type");
     return 1;      ## Send commands
   }      my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
 }      if ($rc) {
         $l->sp("Successfull!");
 sub get_ver        $host->{retry}--;
 {        $needs_reboot = 1;
   my $host = shift;      } elsif (defined $rc) {
   return cmd($host, 'ver');        $l->sp("Already up to date");
 }        $host->{retry}--;
       } else {
 sub enable_tftpd        $l->sp("Failed");
 {        $t->bye;
   my $host = shift;        next;
       }
   my $vals = cmd($host, 'tftpd on', 'Success.');  
     }
   if ($vals->{'Tftpd'} eq 'listen') {  
     return $vals;    if ($needs_reboot) {
   } else {      $l->sp("Rebooting $host->{name}");
     return undef;      $t->reboot;
   }    } else {
 }      $l->sp("Bye $host->{name}");
       $t->bye();
 sub check_tftpd    }
 {  }
   my $host = shift;  
   return cmd($host, 'tftpd', 'Success.');    if (! $processed) {
 }      $l->sp("");
       $l->sp("Finished.  No more hosts.");
 sub sysinfo      last;
 {    }
   my $host = shift;  }
   return cmd($host, 'sysinfo', 'Success.');  
 }  sub upload
   {
 sub updateflash    my $t    = shift;
 {    my $conf = shift;
   my $host = shift;  
   my $old = shift;    my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
   my $new = shift;  
     my $fw_type = $conf->{firmware_type};
   return undef unless $new;  
     my $ver = $t->ver;
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);  
 }    if (! (
       $ver->{$fw_type . ' Version'}  &&
 sub cmd      $ver->{$fw_type . ' Checksum'}
 {    )) {
   my $host   = shift;      $l->sp("Error getting current version numbers");
   my $string = shift;      return;
   my $expect_last = shift;    }
   my $timeout = shift || 5;  
     if (
   my @lines = $host->cmd(String => $string, Timeout => $timeout);      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
       $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
   my $vals = decode_lines(@lines);    ) {
       return 0;
   my $last = $host->lastline;    }
   
   unless ($expect_last) {    $l->sp("Updating $fw_type");
     return $vals;    $l->sp("Config information:");
   }    $l->sp("  Hardware Type: $conf->{'type'}");
     $l->sp("  File Name:     $conf->{'file_name'}");
   if ($last =~ /$expect_last$/) {    $l->sp("  File Size:     $conf->{'file_size'}");
     return $vals;    $l->sp("  File Checksum: $conf->{'file_cksum'}");
   } else {    $l->sp("  Conf Version:  $conf->{'ver'}");
     warn "Error with command ($string): $last";    $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
     return undef;    $l->sp("  Conf Checksum: $conf->{'cksum'}");
   }    $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
 }  
     my $try = 0;
 sub decode_lines    while (1) {
 {      if ($try >= $max_tries) {
   my @lines = @_;        $l->sp("Couldn't update in $max_tries tries!");
         return;
   my %conf;      }
       $try++;
   my $key = '';  
   my $val = '';      $l->p("Enabling TFTPd");
   my $in_key = 0;      unless ($t->enable_tftpd) {
   my $in_val = 0;        $l->sp("Couldn't enable tftpd");
         next;
   foreach my $line (@lines) {      }
     my @chars = split //, $line;  
       $l->p("Uploading file ($conf->{file_name})");
     my $last_key = '';      # use tftp to push the file up
     foreach my $c (@chars) {      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {      unless ($tftp->put($file, $file)) {
         if ($c eq '[') {        $l->sp("Error uploading: " . $tftp->error);
           $in_key = 1;        next;
           $in_val = 0;      }
         } else {  
           $in_key = 0;      $l->p("Checking upload ($conf->{'file_cksum'})");
           $in_val = 0;      my $results = $t->tftpd;
         }      # check the 'File Length' against ???
       if (! (
         if ($key) {        $results->{'File Checksum'} &&
           $key =~ s/^\s+//;        $results->{'File Length'}   &&
           $key =~ s/\s+$//;        $results->{'File Name'}
       )) {
           $val =~ s/^\s+//;        $l->sp("Unable to get results of upload");
           $val =~ s/\s+$//;        next;
       }
           if ($key eq 'Checksum' && $last_key) {      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
             # Special case for these bastids.        $l->sp(
             my $new = $last_key;          "File checksum (" . $results->{'File Checksum'} .
             $new =~ s/\s+\S+$//;          ") does not match config file (" . $conf->{'file_cksum'} . ")!"
             $key = $new . " " . $key;        );
           }        next;
       }
           $last_key = $key;      $l->p("File checksum matches . . . ");
           $conf{$key} = $val;  
           $key = '';      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
           $val = '';        $l->sp(
         }          "File length (" . $results->{'File Length'} .
           ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
       } elsif ($c eq ']') {        );
         $in_val = 1;        next;
         $in_key = 0;      }
         $c = shift @chars;      $l->p("File length matches . . . ");
   
       } elsif ($in_key) {      if ( uc($results->{'File Name'}) ne uc($file) ) {
         $key .= $c;        $l->sp(
           "File name (" . $results->{'File Name'} .
       } elsif ($in_val) {          ") does not match config file (" . $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 . ' ' . $ver->{$fw_type . ' Checksum'} .
                 ' '          . $conf->{'cksum'},
 sub read_conf        Timeout => 90,
 {      ) ) {
   my $file = shift;        $l->sp("Couldn't update flash: $!");
   my %conf;        next;
   my $in_ip_list = 0;      }
   open my $fh, $file or die "Couldn't open file $file: $!";  
   while (<$fh>) {      unless (
     chomp;        defined $results->{'Checksum'} &&
     next if /^#/;        $results->{'Checksum'} eq $conf->{'cksum'}
     next if /^$/;      ) {
     if ($in_ip_list) {        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
       push @{ $conf{'ips'} }, $_;        next;
     } else {      }
       my ($key, $val) = split /\s+/, $_, 2;  
       $l->p("$fw_type saved checksum matches . . . ");
       if (lc($key) eq 'ips') {  
         $in_ip_list = 1;      return 1;
         next;    }
       }  }
   
       $conf{ lc($key) } = $val;  package Mylogger;
     }  
   }  use Fcntl ':flock'; # import LOCK_* constants
   close $fh;  #use YAML;
   use constant LOG_PRINT => 128;
   #print Dump \%conf;  use constant LOG_SAVE  =>  64;
   foreach (  
     'password',  DESTROY {
     'file_name', 'file_size', 'file_cksum',    my $self = shift;
     'ver', 'cksum', 'ips',    if ($self->{'MYLOG'}) {
   ) {      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
     die "No $_ specified in config file!"      close $self->{'MYLOG'};
       if (not exists $conf{$_});    }
   }  }
   
   return \%conf;  sub new {
 }    my $package = shift;
     my $self = shift || {};
   
     $self->{'base_path'}  ||= '.';
     $self->{'log_path'}   ||= $self->{'base_path'};
     $self->{'log_prefix'} ||= 'LOG';
     $self->{'log_file'}   ||= GetLogName(
       $self->{'log_prefix'},
       $self->{'log_path'}
     );
     bless $self, $package;
   }
   
   sub s
   {
     my $self = shift;
     my $m = shift;
     return $self->mylog($m, LOG_SAVE);
   }
   
   sub p
   {
     my $self = shift;
     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 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);
     }
   }
   
   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.3  
changed lines
  Added in v.1.27

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