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

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

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