[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.4 and 1.25

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

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