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

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

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