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

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

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