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

version 1.3, 2005/11/16 20:48:48 version 1.24, 2007/02/02 16:29:58
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.23 2007/02/01 18:44:30 andrew Exp $
 ########################################################################  ########################################################################
 # update_trango.pl *** Updates trango foxes with a new firmware  # update_trango.pl *** Updates trango foxes 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;
   
 my $config_file = shift || 'fox53.conf';  my $config_file = shift || 'update_trango.yaml';
 my $conf = read_conf($config_file);  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 @foxes = (
   '10.100.1.2',
 foreach my $fox (@{ $conf->{'ips'} }) {  '10.100.2.2',
   print STDERR "Updating: $fox\n";  '10.100.3.2',
   '10.100.3.3',
   ## Connect and login.  '10.100.3.4',
   my $host = new Net::Telnet (Timeout => 5,  '10.100.3.8',
                               Prompt => '/#> *$/');  '10.100.4.2',
   print "Connecting to $fox\n";  '10.100.60.5',
   $host->open($fox);  '10.100.80.5',
   $host->dump_log('dump.log');  '10.100.150.5',
   '10.100.5.2',
   ## Login to remote host.  '10.100.6.2',
   $host->waitfor(  '10.100.7.2',
     -match => '/password: ?$/i',  '10.100.8.2',
     -errmode => "return",  '10.100.9.2',
   ) or die "problem connecting to host ($fox): ", $host->lastline;  '10.100.21.2',
   '10.100.22.2',
   my $login_banner = $host->lastline;  '10.100.23.2',
   my ($type, $version) = $login_banner =~  '10.100.32.2',
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;  '10.100.33.2',
   '10.100.41.2',
   if ($type ne $conf->{'type'}) {  '10.100.51.2',
     print STDERR "Wrong type of unit ('$type' should be '$conf->{'type'}')\n";  '10.100.52.2',
     $host->close;  '10.100.53.2',
     next;  '10.100.54.2',
   }  '10.100.61.2',
   '10.100.62.2',
   if ($version eq $conf->{'ver'}) {  );
     print STDERR "Already up to date with firmware version '$version'\n";  
     $host->close;  foreach my $fox (@foxes) {
     next;    $l->sp("Checking: $fox");
   }    my $needs_reboot = 0;
   
   print "Logging in\n";    ## Connect and login.
   $host->print($conf->{'password'});    my $t = new Net::Telnet::Trango (
   $host->waitfor(      Timeout => 5,
     -match => $host->prompt,      Errmode => 'return',
     -errmode => "return",    ) or die "Couldn't make new connection: $!";
   ) or die "login ($fox) failed: ", $host->lastline;    $l->p("Connecting to $fox");
     unless ( $t->open($fox) ) {
       $l->sp("Error connecting: $!");
   print "Sending commands\n";      next;
   ## Send commands    }
   if ( upload($host, $fox, $conf->{'file_name'}) ) {  
     print "Rebooting\n";    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'");
     print "Exiting\n";            $t->close();
     $host->print("exit\n");            next;
     $host->getline;          }
   }  
   $host->close;      my $host_type = $t->host_type;
 }      if ($firmware_type eq 'FPGA') {
         $host_type =~ s/\s.*$//;
 sub upload      }
 {  
   my $host = shift;      if (! exists $conf->{$firmware_type}->{$host_type}) {
   my $fox  = shift;            $l->sp("No '$firmware_type' config for type $host_type");
   my $file = shift;            $t->close();
             next;
   print "Getting current version\n";          }
   my $ver = get_ver($host);  
       if (! $t->logged_in) {
   if (        $l->p("Logging in");
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&        $t->login($conf->{general}->{'password'});
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}        unless ($t->logged_in) {
   ) {          $l->p('Failed!');
     print STDERR "Already updated!";          $t->bye();
     return 1;          next;
   }        }
       }
   my $try = 0;  
   while (1) {          foreach my $k (keys %{ $conf->{general} }) {
     if ($try >= $max_tries) {            $conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k};
       print STDERR "Couldn't update in $max_tries tries!";      }
       return undef;          $conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type;
     }          $conf->{$firmware_type}->{$host_type}->{type} = $host_type;
     $try++;  
       $l->sp("$host_type $firmware_type");
     #sysinfo($host);      $l->p("Sending commands");
           ## Send commands
     print "Enabling TFTPd\n";      my $rc = upload($t, $conf->{$firmware_type}->{$host_type});
     enable_tftpd($host) || die "Couldn't enable tftpd";          if ($rc) {
         $l->sp("Successfull!");
     print "Uploading file\n";            $needs_reboot = 1;
     # use tftp to push the file up      } elsif (defined $rc) {
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');            $l->sp("Already up to date");
       } else {
     $tftp->put($file, $file)            $l->sp("Failed");
       or die "Error uploading: " . $tftp->error;            $t->bye;
             next;
     # waitfor some sort of output      }
     # make sure it says 'Success.' otherwise error  
     #print "LAST: " . $host->lastline;    }
     #my @lines = $host->getlines;  
     #print Dump \@lines;    if ($needs_reboot) {
           $l->sp("Rebooting $fox");
     print "Checking upload\n";      $t->reboot;
     my $results = check_tftpd($host);    } else {
     # check the 'File Length' against ???      $l->sp("Bye $fox");
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {      $t->bye();
       print STDERR "File checksum does not match config file!";    }
       next;    $l->sp("");
     }  }
   
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {  sub upload
       print STDERR "File length does not match config file!";  {
       next;    my $t    = shift;
     }    my $conf = shift;
   
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {    my $file = $conf->{firmware_path} . '/' . $conf->{file_name};
       print STDERR "File name does not match config file!";  
       next;    my $fw_type = $conf->{firmware_type};
     }  
     my $ver = $t->ver;
     print "Updating flash\n";  
     $results = updateflash(    if (
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
     ) or die "Couldn't update flash: " . $host->lastline;      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
     unless (    ) {
       defined $results->{'Checksum'} &&      return 0;
       $results->{'Checksum'} eq $conf->{'cksum'}    }
     ) {  
       print STDERR "Saved checksum does not match config file!";    $l->sp("Config information:");
       next;    $l->sp("  Hardware Type: $conf->{'type'}");
     }    $l->sp("  File Name:     $conf->{'file_name'}");
         $l->sp("  File Size:     $conf->{'file_size'}");
     print STDERR "Successfully updated!\n";    $l->sp("  File Checksum: $conf->{'file_cksum'}");
     return 1;    $l->sp("  Conf Version:  $conf->{'ver'}");
   }    $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
 }    $l->sp("  Conf Checksum: $conf->{'cksum'}");
     $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
 sub get_ver  
 {    $l->sp("Updating");
   my $host = shift;    my $try = 0;
   return cmd($host, 'ver');    while (1) {
 }      if ($try >= $max_tries) {
         $l->sp("Couldn't update in $max_tries tries!");
 sub enable_tftpd        return;
 {      }
   my $host = shift;      $try++;
   
   my $vals = cmd($host, 'tftpd on', 'Success.');      #sysinfo($self->{'_host'});
   
   if ($vals->{'Tftpd'} eq 'listen') {      $l->p("Enabling TFTPd");
     return $vals;      unless ($t->enable_tftpd) {
   } else {        $l->sp("Couldn't enable tftpd");
     return undef;        next;
   }      }
 }  
       $l->p("Uploading file ($file)");
 sub check_tftpd      # use tftp to push the file up
 {      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   my $host = shift;  
   return cmd($host, 'tftpd', 'Success.');      unless ($tftp->put($file, $file)) {
 }        $l->sp("Error uploading: " . $tftp->error);
         next;
 sub sysinfo      }
 {  
   my $host = shift;      $l->p("Checking upload ($conf->{'file_cksum'})");
   return cmd($host, 'sysinfo', 'Success.');      my $results = $t->tftpd;
 }      # check the 'File Length' against ???
       if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
 sub updateflash        $l->sp(
 {          "File checksum '" . $results->{'File Checksum'} .
   my $host = shift;          " does not match config file '" . $conf->{'file_cksum'} . "'!"
   my $old = shift;        );
   my $new = shift;        next;
       }
   return undef unless $new;      $l->p("File checksum matches . . . ");
   
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
 }        $l->sp(
           "File length '" . $results->{'File Length'} .
 sub cmd          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
 {        );
   my $host   = shift;        next;
   my $string = shift;      }
   my $expect_last = shift;      $l->p("File length matches . . . ");
   my $timeout = shift || 5;  
       if ( uc($results->{'File Name'}) ne uc($file) ) {
   my @lines = $host->cmd(String => $string, Timeout => $timeout);        $l->sp(
           "File name '" . $results->{'File Name'} .
   my $vals = decode_lines(@lines);          "' does not match config file '" . $file . "'!"
         );
   my $last = $host->lastline;        next;
       }
   unless ($expect_last) {      $l->p("File name  matches . . . ");
     return $vals;  
   }      my $image_type = 'mainimage';
       if ($fw_type eq 'FPGA') {
   if ($last =~ /$expect_last$/) {        $image_type = 'fpgaimage';
     return $vals;      }
   } else {      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
     warn "Error with command ($string): $last";      unless ($results = $t->updateflash(
     return undef;        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
   }                ' '          . $conf->{'cksum'},
 }        Timeout => 90,
       ) ) {
 sub decode_lines        $l->sp("Couldn't update flash: $!");
 {        next;
   my @lines = @_;      }
   
   my %conf;      unless (
         defined $results->{'Checksum'} &&
   my $key = '';        $results->{'Checksum'} eq $conf->{'cksum'}
   my $val = '';      ) {
   my $in_key = 0;        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
   my $in_val = 0;        next;
       }
   foreach my $line (@lines) {      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
     my @chars = split //, $line;            "matches ($conf->{'cksum'})");
   
     my $last_key = '';      $l->sp("Successfully updated!");
     foreach my $c (@chars) {      return 1;
     }
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {  }
         if ($c eq '[') {  
           $in_key = 1;  package Mylogger;
           $in_val = 0;  
         } else {  use Fcntl ':flock'; # import LOCK_* constants
           $in_key = 0;  #use YAML;
           $in_val = 0;  use constant LOG_PRINT => 128;
         }  use constant LOG_SAVE  =>  64;
   
         if ($key) {  DESTROY {
           $key =~ s/^\s+//;    my $self = shift;
           $key =~ s/\s+$//;    if ($self->{'MYLOG'}) {
       $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
           $val =~ s/^\s+//;      close $self->{'MYLOG'};
           $val =~ s/\s+$//;    }
   }
           if ($key eq 'Checksum' && $last_key) {  
             # Special case for these bastids.  sub new {
             my $new = $last_key;    my $package = shift;
             $new =~ s/\s+\S+$//;    my $self = shift || {};
             $key = $new . " " . $key;  
           }    $self->{'base_path'}  ||= '.';
     $self->{'log_path'}   ||= $self->{'base_path'};
           $last_key = $key;    $self->{'log_prefix'} ||= 'LOG';
           $conf{$key} = $val;    $self->{'log_file'}   ||= GetLogName(
           $key = '';      $self->{'log_prefix'},
           $val = '';      $self->{'log_path'}
         }    );
     bless $self, $package;
       } elsif ($c eq ']') {  }
         $in_val = 1;  
         $in_key = 0;  sub s
         $c = shift @chars;  {
     my $self = shift;
       } elsif ($in_key) {    my $m = shift;
         $key .= $c;    return $self->mylog($m, LOG_SAVE);
   }
       } elsif ($in_val) {  
         $val .= $c;  sub p
       }  {
     }    my $self = shift;
   }    my $m = shift;
   #print Dump \%conf;    return $self->mylog($m, LOG_PRINT);
   }
   if (%conf) {  
     return \%conf;  sub sp
   } else {  {
     return \@lines;    my $self = shift;
   }    my $m = shift;
 }    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
   }
 sub read_conf  
 {  sub mylog
   my $file = shift;  {
   my %conf;    my $self = shift;
   my $in_ip_list = 0;  
   open my $fh, $file or die "Couldn't open file $file: $!";    my $thing = shift;
   while (<$fh>) {    chomp $thing;
     chomp;  
     next if /^#/;    my $which = shift;
     next if /^$/;  
     if ($in_ip_list) {    my $MYLOG;
       push @{ $conf{'ips'} }, $_;    if ($which & LOG_PRINT) {
     } else {      print $thing, "\n";
       my ($key, $val) = split /\s+/, $_, 2;    }
   
       if (lc($key) eq 'ips') {    if ($which & LOG_SAVE) {
         $in_ip_list = 1;      if ($self->{'MYLOG'}) {
         next;        $MYLOG = $self->{'MYLOG'};
       }      } else {
         unless ($MYLOG) {
       $conf{ lc($key) } = $val;                  open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
     }            or die "Couldn't open logfile!\n";
   }          my $ofh = select $MYLOG;
   close $fh;          $|=1;
           select $ofh;
   #print Dump \%conf;          $self->{'MYLOG'} = $MYLOG;
   foreach (  
     'password',          $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
     'file_name', 'file_size', 'file_cksum',        }
     'ver', 'cksum', 'ips',      }
   ) {      flock($MYLOG, LOCK_EX);
     die "No $_ specified in config file!"      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
       if (not exists $conf{$_});        or die "Couldn't print to MYLOG: $!";
   }      flock($MYLOG, LOCK_UN);
     }
   return \%conf;  }
 }  
   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.24

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