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

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

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