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

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

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