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

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

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