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

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

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