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

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

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