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

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

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