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

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

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