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

version 1.5, 2005/11/16 21:39:51 version 1.20, 2007/01/31 22:19:07
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.19 2007/01/31 22:14:26 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 Net::TFTP;
 use Net::TFTP;  use lib '.';
 use Fcntl ':flock'; # import LOCK_* constants  use Net::Telnet::Trango;
 #use YAML;  
   my $config_file = shift || 'update_trango.conf';
   my $max_tries = 3;
 my $config_file = shift || 'update_trango.conf';  
 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 = read_conf($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("");
   
   
   
   
   
   
   foreach my $fox (@{ $conf->{'ips'} }) {
 foreach my $fox (@{ $conf->{'ips'} }) {    $l->sp("Updating: $fox");
   mylog("Updating: $fox", LOG_SAVE);  
     ## Connect and login.
   ## Connect and login.    my $t = new Net::Telnet::Trango (
   my $host = new Net::Telnet (Timeout => 5,      Timeout => 5,
                               Prompt => '/#> *$/');      Errmode => 'return',
   mylog("Connecting to $fox");    ) or die "Couldn't make new connection: $!";
   $host->open($fox);    $l->p("Connecting to $fox");
   $host->dump_log('dump.log');    unless ( $t->open($fox) ) {
       $l->sp("Error connecting: $!");
   ## Login to remote host.      next;
   $host->waitfor(    }
     -match => '/password: ?$/i',  
     -errmode => "return",    if ($t->host_type ne $conf->{'type'}) {
   ) or die "problem connecting to host ($fox): ", $host->lastline;      $l->sp("Wrong type of unit ('" . $t->host_type . "' should be '$conf->{'type'}')");
       $t->close;
   my $login_banner = $host->lastline;      next;
   my ($type, $version) = $login_banner =~    }
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;  
     if ($t->firmware_version eq $conf->{'ver'}) {
   if ($type ne $conf->{'type'}) {      $l->sp("Already up to date with firmware version '" . $t->firmware_version . "'");
     mylog("Wrong type of unit ('$type' should be '$conf->{'type'}')", LOG_SAVE);      $t->close;
     $host->close;      next;
     next;    }
   }  
     $l->p("Logging in");
   if ($version eq $conf->{'ver'}) {    $t->login($conf->{'password'}) || die "Couldn't login: $!";
     mylog("Already up to date with firmware version '$version'", LOG_SAVE);  
     $host->close;    $l->p("Sending commands");
     next;    ## Send commands
   }    if ( upload($t, $conf->{'file_name'}) ) {
        $l->p("Rebooting");
   mylog("Logging in");      $t->reboot;
   $host->print($conf->{'password'});    } else {
   unless ($host->waitfor(      $l->p("Exiting");
     -match => $host->prompt,      $t->exit;
     -errmode => "return",    }
   ) ) {  
     mylog("login ($fox) failed: " . $host->lastline);    $l->sp("");
     next;  }
   }  
   sub upload
   {
   mylog("Sending commands");    my $t    = shift;
   ## Send commands    my $file = shift;
   if ( upload($host, $fox, $conf->{'file_name'}) ) {  
     mylog("Rebooting");    my $fw_type = 'Firmware';
     $host->print("reboot\n");    if (uc($conf->{'firmware_type'}) eq 'FPGA') {
     $host->getline;      $fw_type = 'FPGA';
   } else {    }
     mylog("Exiting");  
     $host->print("exit\n");    my $ver = $t->ver;
     $host->getline;    $l->p("Current version '" . $ver->{$fw_type . ' Version'} . "'");
   }  
   $host->close;    if (
   mylog("", LOG_SAVE);      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
 }      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
     ) {
 sub upload      $l->sp("Already updated!");
 {      return 1;
   my $host = shift;    }
   my $fox  = shift;  
   my $file = shift;    my $try = 0;
     while (1) {
   mylog("Getting current version");      if ($try >= $max_tries) {
   my $ver = get_ver($host);        $l->sp("Couldn't update in $max_tries tries!");
         return undef;
   if (      }
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&      $try++;
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}  
   ) {      #sysinfo($self->{'_host'});
     mylog("Already updated!", LOG_SAVE);  
     return 1;      $l->p("Enabling TFTPd");
   }      $t->enable_tftpd || die "Couldn't enable tftpd";
   
   my $try = 0;      $l->p("Uploading file ($file)");
   while (1) {      # use tftp to push the file up
     if ($try >= $max_tries) {      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
       mylog("Couldn't update in $max_tries tries!");  
       return undef;      $tftp->put($file, $file)
     }        or die "Error uploading: " . $tftp->error;
     $try++;  
       # waitfor some sort of output
     #sysinfo($host);      # make sure it says 'Success.' otherwise error
           #print "LAST: " . $self->lastline;
     mylog("Enabling TFTPd");      #my @lines = $self->getlines;
     enable_tftpd($host) || die "Couldn't enable tftpd";      #print Dump \@lines;
   
     mylog("Uploading file ($file)");      $l->p("Checking upload ($conf->{'file_cksum'})");
     # use tftp to push the file up      my $results = $t->tftpd;
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');      # check the 'File Length' against ???
       if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
     $tftp->put($file, $file)        $l->sp(
       or die "Error uploading: " . $tftp->error;          "File checksum '" . $results->{'File Checksum'} .
           " does not match config file '" . $conf->{'file_cksum'} . "'!"
     # waitfor some sort of output        );
     # make sure it says 'Success.' otherwise error        next;
     #print "LAST: " . $host->lastline;      }
     #my @lines = $host->getlines;      $l->p("File checksum matches . . . ");
     #print Dump \@lines;  
           if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
     mylog("Checking upload ($conf->{'file_cksum'})");        $l->sp(
     my $results = check_tftpd($host);          "File length '" . $results->{'File Length'} .
     # check the 'File Length' against ???          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {        );
       mylog("File checksum does not match config file!", LOG_SAVE);        next;
       next;      }
     }      $l->p("File length matches . . . ");
     mylog("File checksum ($results->{'File Checksum'}) " .  
           "matches ($conf->{'file_cksum'})");      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
         $l->sp(
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {          "File name '" . $results->{'File Name'} .
       mylog("File length does not match config file!", LOG_SAVE);          "' does not match config file '" . $conf->{'file_name'} . "'!"
       next;        );
     }        next;
     mylog("File length ($results->{'File Length'}) " .      }
           "matches ($conf->{'file_size'})");      $l->p("File name  matches . . . ");
   
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {      my $image_type = 'mainimage';
       mylog("File name does not match config file!", LOG_SAVE);      if ($fw_type eq 'FPGA') {
       next;        $image_type = 'fpgaimage';
     }      }
     mylog("File name ($results->{'File Name'}) " .      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
           "matches ($conf->{'file_name'})");      unless ($results = $t->updateflash(
         args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
     mylog("Updating flash (new checksum '$conf->{'cksum'}')");                ' '          . $conf->{'cksum'},
     $results = updateflash(        Timeout => 90,
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}      ) ) {
     ) or die "Couldn't update flash: " . $host->lastline;        $l->sp("Couldn't update flash: $!");
     unless (        next;
       defined $results->{'Checksum'} &&      }
       $results->{'Checksum'} eq $conf->{'cksum'}  
     ) {      unless (
       mylog("Saved checksum does not match config file!", LOG_SAVE);        defined $results->{'Checksum'} &&
       next;        $results->{'Checksum'} eq $conf->{'cksum'}
     }      ) {
     mylog("Uploaded checksum ($results->{'Checksum'}) " .        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
           "matches ($conf->{'cksum'})");        next;
           }
     mylog("Successfully updated!", LOG_SAVE);      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
     return 1;            "matches ($conf->{'cksum'})");
   }  
 }      $l->sp("Successfully updated!");
       return 1;
 sub get_ver    }
 {  }
   my $host = shift;  
   return cmd($host, 'ver');  sub read_conf
 }  {
     my $file = shift;
 sub enable_tftpd    my %conf;
 {    my $in_ip_list = 0;
   my $host = shift;    open my $fh, $file or die "Couldn't open file $file: $!";
     while (<$fh>) {
   my $vals = cmd($host, 'tftpd on', 'Success.');      chomp;
       next if /^#/;
   if ($vals->{'Tftpd'} eq 'listen') {      next if /^$/;
     return $vals;      if ($in_ip_list) {
   } else {        s/\s+//g; # Whitespace is a no no
     return undef;  
   }        if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
 }          push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
         } else {
 sub check_tftpd          push @{ $conf{'ips'} }, $_;
 {        }
   my $host = shift;      } else {
   return cmd($host, 'tftpd', 'Success.');        my ($key, $val) = split /\s+/, $_, 2;
 }  
         if (lc($key) eq 'ips') {
 sub sysinfo          $in_ip_list = 1;
 {          next;
   my $host = shift;        }
   return cmd($host, 'sysinfo', 'Success.');  
 }        $key =~ s/^\s+//;
         $key =~ s/\s+$//;
 sub updateflash        $val =~ s/^\s+//;
 {        $val =~ s/\s+$//;
   my $host = shift;  
   my $old = shift;        $conf{ lc($key) } = $val;
   my $new = shift;      }
     }
   return undef unless $new;    close $fh;
   
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);    #print Dump \%conf;
 }    foreach (
       'password',
 sub cmd      'file_name', 'file_size', 'file_cksum',
 {      'ver', 'cksum', 'ips',
   my $host   = shift;    ) {
   my $string = shift;      die "No $_ specified in config file!"
   my $expect_last = shift;        if (not exists $conf{$_});
   my $timeout = shift || 5;    }
   
   my @lines = $host->cmd(String => $string, Timeout => $timeout);    #print Dump \%conf;
     #exit;
   my $vals = decode_lines(@lines);    return \%conf;
   }
   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.5  
changed lines
  Added in v.1.20

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