[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.5 and 1.29

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

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