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

version 1.3, 2005/11/16 20:48:48 version 1.29, 2007/02/05 16:37:20
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.28 2007/02/02 22:37:24 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'}        $t->su_password($password);
   ) {        foreach my $su (@{ $sudb }) {
     print STDERR "Already updated!";          $l->p("Getting su info $su->{suid}");
     return 1;          my $su_info = $t->su_info( $su->{suid} );
   }          if ($su_info->{ip}) {
             if (grep { $_->{name} eq $su_info->{'ip'} } @{ $hosts }) {
   my $try = 0;              $l->p("Already have $su_info->{ip}");
   while (1) {              next;
     if ($try >= $max_tries) {            }
       print STDERR "Couldn't update in $max_tries tries!";            $l->sp("Adding host $su_info->{ip}");
       return undef;            my $new_host = {
     }              Telnet_Password => $host->{Telnet_Password},
     $try++;              group   => $host->{group} . '-Client',
               name    => $su_info->{ip},
     #sysinfo($host);              remarks => $su_info->{remarks},
                 };
     print "Enabling TFTPd\n";            push @{ $hosts }, $new_host;
     enable_tftpd($host) || die "Couldn't enable tftpd";          } else {
             $l->sp("Couldn't get su info for $su->{suid}");
     print "Uploading file\n";            if ($su_info->{ERR}) {
     # use tftp to push the file up              $l->sp("ERR: $su_info->{ERR}");
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');            }
           }
     $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 ($firmware_type eq 'Firmware' &&
       next;        $t->firmware_version eq
     }        $conf->{$firmware_type}->{$host_type}->{ver}
       ) {
     print "Updating flash\n";        $l->sp("Firmware already up to date");
     $results = updateflash(        next;
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}      }
     ) or die "Couldn't update flash: " . $host->lastline;  
     unless (      if (! $t->logged_in) {
       defined $results->{'Checksum'} &&        $l->p("Logging in");
       $results->{'Checksum'} eq $conf->{'cksum'}        $t->login($password);
     ) {        unless ($t->logged_in) {
       print STDERR "Saved checksum does not match config file!";          $l->p('Failed!');
       next;          $t->close;
     }          next;
             }
     print STDERR "Successfully updated!\n";      }
     return 1;  
   }      foreach my $k (keys %{ $conf->{general} }) {
 }        $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
       }
 sub get_ver      $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
 {      $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
   my $host = shift;  
   return cmd($host, 'ver');      $l->sp("$host_type $firmware_type");
 }      ## Send commands
       my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
 sub enable_tftpd      if ($rc) {
 {        $l->sp("Successfull!");
   my $host = shift;        $host->{retry}--;
         $needs_reboot = 1;
   my $vals = cmd($host, 'tftpd on', 'Success.');      } elsif (defined $rc) {
         $l->sp("Already up to date");
   if ($vals->{'Tftpd'} eq 'listen') {        $host->{retry}--;
     return $vals;      } else {
   } else {        $l->sp("Failed");
     return undef;        $t->bye;
   }        next;
 }      }
   
 sub check_tftpd    }
 {  
   my $host = shift;    if ($needs_reboot) {
   return cmd($host, 'tftpd', 'Success.');      $l->sp("Rebooting $host->{name}");
 }      $t->reboot;
     } else {
 sub sysinfo      $l->sp("Bye $host->{name}");
 {      $t->bye();
   my $host = shift;    }
   return cmd($host, 'sysinfo', 'Success.');  }
 }  
     if (! $processed) {
 sub updateflash      $l->sp("");
 {      $l->sp("Finished.  No more hosts.");
   my $host = shift;      last;
   my $old = shift;    }
   my $new = shift;  }
   
   return undef unless $new;  sub upload
   {
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);    my $t    = shift;
 }    my $conf = shift;
   
 sub cmd    my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
 {  
   my $host   = shift;    my $fw_type = $conf->{firmware_type};
   my $string = shift;  
   my $expect_last = shift;    my $ver = $t->ver;
   my $timeout = shift || 5;  
     if (! (
   my @lines = $host->cmd(String => $string, Timeout => $timeout);      $ver->{$fw_type . ' Version'}  &&
       $ver->{$fw_type . ' Checksum'}
   my $vals = decode_lines(@lines);    )) {
       $l->sp("Error getting current version numbers");
   my $last = $host->lastline;      return;
     }
   unless ($expect_last) {  
     return $vals;    if (
   }      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
       $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
   if ($last =~ /$expect_last$/) {    ) {
     return $vals;      return 0;
   } else {    }
     warn "Error with command ($string): $last";  
     return undef;    $l->sp("Updating $fw_type");
   }    $l->sp("Config information:");
 }    $l->sp("  Hardware Type: $conf->{'type'}");
     $l->sp("  File Name:     $conf->{'file_name'}");
 sub decode_lines    $l->sp("  File Size:     $conf->{'file_size'}");
 {    $l->sp("  File Checksum: $conf->{'file_cksum'}");
   my @lines = @_;    $l->sp("  Conf Version:  $conf->{'ver'}");
     $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
   my %conf;    $l->sp("  Conf Checksum: $conf->{'cksum'}");
     $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
   my $key = '';  
   my $val = '';    my $try = 0;
   my $in_key = 0;    while (1) {
   my $in_val = 0;      if ($try >= $max_tries) {
         $l->sp("Couldn't update in $max_tries tries!");
   foreach my $line (@lines) {        return;
     my @chars = split //, $line;      }
       $try++;
     my $last_key = '';  
     foreach my $c (@chars) {      $l->p("Enabling TFTPd");
       unless ($t->enable_tftpd) {
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {        $l->sp("Couldn't enable tftpd");
         if ($c eq '[') {        next;
           $in_key = 1;      }
           $in_val = 0;  
         } else {      $l->p("Uploading file ($conf->{file_name})");
           $in_key = 0;      # use tftp to push the file up
           $in_val = 0;      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
         }  
       unless ($tftp->put($file, $file)) {
         if ($key) {        $l->sp("Error uploading: " . $tftp->error);
           $key =~ s/^\s+//;        next;
           $key =~ s/\s+$//;      }
   
           $val =~ s/^\s+//;      $l->p("Checking upload ($conf->{'file_cksum'})");
           $val =~ s/\s+$//;      my $results = $t->tftpd;
       # check the 'File Length' against ???
           if ($key eq 'Checksum' && $last_key) {      if (! (
             # Special case for these bastids.        $results->{'File Checksum'} &&
             my $new = $last_key;        $results->{'File Length'}   &&
             $new =~ s/\s+\S+$//;        $results->{'File Name'}
             $key = $new . " " . $key;      )) {
           }        $l->sp("Unable to get results of upload");
         next;
           $last_key = $key;      }
           $conf{$key} = $val;      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
           $key = '';        $l->sp(
           $val = '';          "File checksum (" . $results->{'File Checksum'} .
         }          ") does not match config file (" . $conf->{'file_cksum'} . ")!"
         );
       } elsif ($c eq ']') {        next;
         $in_val = 1;      }
         $in_key = 0;      $l->p("File checksum matches . . . ");
         $c = shift @chars;  
       if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
       } elsif ($in_key) {        $l->sp(
         $key .= $c;          "File length (" . $results->{'File Length'} .
           ") does not match config file (" . $conf->{'file_size'} . " bytes)!"
       } elsif ($in_val) {        );
         $val .= $c;        next;
       }      }
     }      $l->p("File length matches . . . ");
   }  
   #print Dump \%conf;      if ( uc($results->{'File Name'}) ne uc($file) ) {
         $l->sp(
   if (%conf) {          "File name (" . $results->{'File Name'} .
     return \%conf;          ") does not match config file (" . $file . ")!"
   } else {        );
     return \@lines;        next;
   }      }
 }      $l->p("File name matches . . . ");
   
 sub read_conf      my $image_type = 'mainimage';
 {      if ($fw_type eq 'FPGA') {
   my $file = shift;        $image_type = 'fpgaimage';
   my %conf;      }
   my $in_ip_list = 0;      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   open my $fh, $file or die "Couldn't open file $file: $!";      unless ($results = $t->updateflash(
   while (<$fh>) {        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
     chomp;                ' '          . $conf->{'cksum'},
     next if /^#/;        Timeout => 90,
     next if /^$/;      ) ) {
     if ($in_ip_list) {        $l->sp("Couldn't update flash: $!");
       push @{ $conf{'ips'} }, $_;        next;
     } else {      }
       my ($key, $val) = split /\s+/, $_, 2;  
       unless (
       if (lc($key) eq 'ips') {        defined $results->{'Checksum'} &&
         $in_ip_list = 1;        $results->{'Checksum'} eq $conf->{'cksum'}
         next;      ) {
       }        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
         next;
       $conf{ lc($key) } = $val;      }
     }  
   }      $l->p("$fw_type saved checksum matches . . . ");
   close $fh;  
       return 1;
   #print Dump \%conf;    }
   foreach (  }
     'password',  
     'file_name', 'file_size', 'file_cksum',  package Mylogger;
     'ver', 'cksum', 'ips',  
   ) {  use Fcntl ':flock'; # import LOCK_* constants
     die "No $_ specified in config file!"  #use YAML;
       if (not exists $conf{$_});  use constant LOG_PRINT => 128;
   }  use constant LOG_SAVE  =>  64;
   
   return \%conf;  DESTROY {
 }    my $self = shift;
     if ($self->{'MYLOG'}) {
       $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
       close $self->{'MYLOG'};
     }
   }
   
   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.29

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