[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.8 and 1.28

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

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.28

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