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

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

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