[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.6 and 1.24

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

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