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

version 1.3, 2005/11/16 20:48:48 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.2 2005/11/16 05:26:37 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 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'}  eq $conf->{'ver'} &&
 sub get_ver      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
 {    ) {
   my $host = shift;      return 0;
   return cmd($host, 'ver');    }
 }  
     $l->sp("Config information:");
 sub enable_tftpd    $l->sp("  Hardware Type: $conf->{'type'}");
 {    $l->sp("  File Name:     $conf->{'file_name'}");
   my $host = shift;    $l->sp("  File Size:     $conf->{'file_size'}");
     $l->sp("  File Checksum: $conf->{'file_cksum'}");
   my $vals = cmd($host, 'tftpd on', 'Success.');    $l->sp("  Conf Version:  $conf->{'ver'}");
     $l->sp("  Cur  Version:  $ver->{$fw_type . ' Version'}");
   if ($vals->{'Tftpd'} eq 'listen') {    $l->sp("  Conf Checksum: $conf->{'cksum'}");
     return $vals;    $l->sp("  Cur  Checksum: $ver->{$fw_type . ' Checksum'}");
   } else {  
     return undef;    $l->sp("Updating");
   }    my $try = 0;
 }    while (1) {
       if ($try >= $max_tries) {
 sub check_tftpd        $l->sp("Couldn't update in $max_tries tries!");
 {        return;
   my $host = shift;      }
   return cmd($host, 'tftpd', 'Success.');      $try++;
 }  
       #sysinfo($self->{'_host'});
 sub sysinfo  
 {      $l->p("Enabling TFTPd");
   my $host = shift;      unless ($t->enable_tftpd) {
   return cmd($host, 'sysinfo', 'Success.');        $l->sp("Couldn't enable tftpd");
 }        next;
       }
 sub updateflash  
 {      $l->p("Uploading file ($file)");
   my $host = shift;      # use tftp to push the file up
   my $old = shift;      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   my $new = shift;  
       unless ($tftp->put($file, $file)) {
   return undef unless $new;        $l->sp("Error uploading: " . $tftp->error);
         next;
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);      }
 }  
       $l->p("Checking upload ($conf->{'file_cksum'})");
 sub cmd      my $results = $t->tftpd;
 {      # check the 'File Length' against ???
   my $host   = shift;      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
   my $string = shift;        $l->sp(
   my $expect_last = shift;          "File checksum '" . $results->{'File Checksum'} .
   my $timeout = shift || 5;          " does not match config file '" . $conf->{'file_cksum'} . "'!"
         );
   my @lines = $host->cmd(String => $string, Timeout => $timeout);        next;
       }
   my $vals = decode_lines(@lines);      $l->p("File checksum matches . . . ");
   
   my $last = $host->lastline;      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
         $l->sp(
   unless ($expect_last) {          "File length '" . $results->{'File Length'} .
     return $vals;          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
   }        );
         next;
   if ($last =~ /$expect_last$/) {      }
     return $vals;      $l->p("File length matches . . . ");
   } else {  
     warn "Error with command ($string): $last";      if ( uc($results->{'File Name'}) ne uc($file) ) {
     return undef;        $l->sp(
   }          "File name '" . $results->{'File Name'} .
 }          "' does not match config file '" . $file . "'!"
         );
 sub decode_lines        next;
 {      }
   my @lines = @_;      $l->p("File name  matches . . . ");
   
   my %conf;      my $image_type = 'mainimage';
       if ($fw_type eq 'FPGA') {
   my $key = '';        $image_type = 'fpgaimage';
   my $val = '';      }
   my $in_key = 0;      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
   my $in_val = 0;      unless ($results = $t->updateflash(
         args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
   foreach my $line (@lines) {                ' '          . $conf->{'cksum'},
     my @chars = split //, $line;        Timeout => 90,
       ) ) {
     my $last_key = '';        $l->sp("Couldn't update flash: $!");
     foreach my $c (@chars) {        next;
       }
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {  
         if ($c eq '[') {      unless (
           $in_key = 1;        defined $results->{'Checksum'} &&
           $in_val = 0;        $results->{'Checksum'} eq $conf->{'cksum'}
         } else {      ) {
           $in_key = 0;        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
           $in_val = 0;        next;
         }      }
       $l->p("Uploaded checksum ($results->{'Checksum'}) " .
         if ($key) {            "matches ($conf->{'cksum'})");
           $key =~ s/^\s+//;  
           $key =~ s/\s+$//;      $l->sp("Successfully updated!");
       return 1;
           $val =~ s/^\s+//;    }
           $val =~ s/\s+$//;  }
   
           if ($key eq 'Checksum' && $last_key) {  package Mylogger;
             # Special case for these bastids.  
             my $new = $last_key;  use Fcntl ':flock'; # import LOCK_* constants
             $new =~ s/\s+\S+$//;  #use YAML;
             $key = $new . " " . $key;  use constant LOG_PRINT => 128;
           }  use constant LOG_SAVE  =>  64;
   
           $last_key = $key;  DESTROY {
           $conf{$key} = $val;    my $self = shift;
           $key = '';    if ($self->{'MYLOG'}) {
           $val = '';      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
         }      close $self->{'MYLOG'};
     }
       } elsif ($c eq ']') {  }
         $in_val = 1;  
         $in_key = 0;  sub new {
         $c = shift @chars;    my $package = shift;
     my $self = shift || {};
       } elsif ($in_key) {  
         $key .= $c;    $self->{'base_path'}  ||= '.';
     $self->{'log_path'}   ||= $self->{'base_path'};
       } elsif ($in_val) {    $self->{'log_prefix'} ||= 'LOG';
         $val .= $c;    $self->{'log_file'}   ||= GetLogName(
       }      $self->{'log_prefix'},
     }      $self->{'log_path'}
   }    );
   #print Dump \%conf;    bless $self, $package;
   }
   if (%conf) {  
     return \%conf;  sub s
   } else {  {
     return \@lines;    my $self = shift;
   }    my $m = shift;
 }    return $self->mylog($m, LOG_SAVE);
   }
 sub read_conf  
 {  sub p
   my $file = shift;  {
   my %conf;    my $self = shift;
   my $in_ip_list = 0;    my $m = shift;
   open my $fh, $file or die "Couldn't open file $file: $!";    return $self->mylog($m, LOG_PRINT);
   while (<$fh>) {  }
     chomp;  
     next if /^#/;  sub sp
     next if /^$/;  {
     if ($in_ip_list) {    my $self = shift;
       push @{ $conf{'ips'} }, $_;    my $m = shift;
     } else {    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
       my ($key, $val) = split /\s+/, $_, 2;  }
   
       if (lc($key) eq 'ips') {  sub mylog
         $in_ip_list = 1;  {
         next;    my $self = shift;
       }  
     my $thing = shift;
       $conf{ lc($key) } = $val;    chomp $thing;
     }  
   }    my $which = shift;
   close $fh;  
     my $MYLOG;
   #print Dump \%conf;    if ($which & LOG_PRINT) {
   foreach (      print $thing, "\n";
     'password',    }
     'file_name', 'file_size', 'file_cksum',  
     'ver', 'cksum', 'ips',    if ($which & LOG_SAVE) {
   ) {      if ($self->{'MYLOG'}) {
     die "No $_ specified in config file!"        $MYLOG = $self->{'MYLOG'};
       if (not exists $conf{$_});      } else {
   }        unless ($MYLOG) {
                   open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
   return \%conf;            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.25

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