[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.4 and 1.24

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

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