[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.7 and 1.30

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

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