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

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

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