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

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

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