[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.8 and 1.23

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

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