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

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

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