[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.5 and 1.26

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

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