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

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

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