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

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

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