[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.4 and 1.20

version 1.4, 2005/11/16 21:13:39 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.3 2005/11/16 20:48:48 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');  
   $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");  
     ## 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'}')");      $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'");  
     $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 {
   $host->waitfor(      $l->p("Exiting");
     -match => $host->prompt,      $t->exit;
     -errmode => "return",    }
   ) or die "login ($fox) failed: ", $host->lastline;  
     $l->sp("");
   }
   mylog("Sending commands");  
   ## Send commands  sub upload
   if ( upload($host, $fox, $conf->{'file_name'}) ) {  {
     mylog("Rebooting");    my $t    = shift;
     $host->print("reboot\n");    my $file = shift;
     $host->getline;  
   } else {    my $fw_type = 'Firmware';
     mylog("Exiting");    if (uc($conf->{'firmware_type'}) eq 'FPGA') {
     $host->print("exit\n");      $fw_type = 'FPGA';
     $host->getline;    }
   }  
   $host->close;    my $ver = $t->ver;
   mylog("");    $l->p("Current version '" . $ver->{$fw_type . ' Version'} . "'");
 }  
     if (
 sub upload      $ver->{$fw_type . ' Version'}  eq $conf->{'ver'} &&
 {      $ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'}
   my $host = shift;    ) {
   my $fox  = shift;      $l->sp("Already updated!");
   my $file = shift;      return 1;
     }
   mylog("Getting current version");  
   my $ver = get_ver($host);    my $try = 0;
     while (1) {
   if (      if ($try >= $max_tries) {
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&        $l->sp("Couldn't update in $max_tries tries!");
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}        return undef;
   ) {      }
     mylog("Already updated!");      $try++;
     return 1;  
   }      #sysinfo($self->{'_host'});
   
   my $try = 0;      $l->p("Enabling TFTPd");
   while (1) {      $t->enable_tftpd || die "Couldn't enable tftpd";
     if ($try >= $max_tries) {  
       mylog("Couldn't update in $max_tries tries!");      $l->p("Uploading file ($file)");
       return undef;      # use tftp to push the file up
     }      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
     $try++;  
       $tftp->put($file, $file)
     #sysinfo($host);        or die "Error uploading: " . $tftp->error;
       
     mylog("Enabling TFTPd");      # waitfor some sort of output
     enable_tftpd($host) || die "Couldn't enable tftpd";      # make sure it says 'Success.' otherwise error
       #print "LAST: " . $self->lastline;
     mylog("Uploading file");      #my @lines = $self->getlines;
     # use tftp to push the file up      #print Dump \@lines;
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');  
       $l->p("Checking upload ($conf->{'file_cksum'})");
     $tftp->put($file, $file)      my $results = $t->tftpd;
       or die "Error uploading: " . $tftp->error;      # check the 'File Length' against ???
       if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
     # waitfor some sort of output        $l->sp(
     # make sure it says 'Success.' otherwise error          "File checksum '" . $results->{'File Checksum'} .
     #print "LAST: " . $host->lastline;          " does not match config file '" . $conf->{'file_cksum'} . "'!"
     #my @lines = $host->getlines;        );
     #print Dump \@lines;        next;
           }
     mylog("Checking upload");      $l->p("File checksum matches . . . ");
     my $results = check_tftpd($host);  
     # check the 'File Length' against ???      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {        $l->sp(
       mylog("File checksum does not match config file!");          "File length '" . $results->{'File Length'} .
       next;          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
     }        );
         next;
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {      }
       mylog("File length does not match config file!");      $l->p("File length matches . . . ");
       next;  
     }      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
         $l->sp(
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {          "File name '" . $results->{'File Name'} .
       mylog("File name does not match config file!");          "' does not match config file '" . $conf->{'file_name'} . "'!"
       next;        );
     }        next;
       }
     mylog("Updating flash");      $l->p("File name  matches . . . ");
     $results = updateflash(  
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}      my $image_type = 'mainimage';
     ) or die "Couldn't update flash: " . $host->lastline;      if ($fw_type eq 'FPGA') {
     unless (        $image_type = 'fpgaimage';
       defined $results->{'Checksum'} &&      }
       $results->{'Checksum'} eq $conf->{'cksum'}      $l->p("Updating $image_type (new checksum '$conf->{'cksum'}')");
     ) {      unless ($results = $t->updateflash(
       mylog("Saved checksum does not match config file!");        args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} .
       next;                ' '          . $conf->{'cksum'},
     }        Timeout => 90,
           ) ) {
     mylog("Successfully updated!");        $l->sp("Couldn't update flash: $!");
     return 1;        next;
   }      }
 }  
       unless (
 sub get_ver        defined $results->{'Checksum'} &&
 {        $results->{'Checksum'} eq $conf->{'cksum'}
   my $host = shift;      ) {
   return cmd($host, 'ver');        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
 }        next;
       }
 sub enable_tftpd      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
 {            "matches ($conf->{'cksum'})");
   my $host = shift;  
       $l->sp("Successfully updated!");
   my $vals = cmd($host, 'tftpd on', 'Success.');      return 1;
     }
   if ($vals->{'Tftpd'} eq 'listen') {  }
     return $vals;  
   } else {  sub read_conf
     return undef;  {
   }    my $file = shift;
 }    my %conf;
     my $in_ip_list = 0;
 sub check_tftpd    open my $fh, $file or die "Couldn't open file $file: $!";
 {    while (<$fh>) {
   my $host = shift;      chomp;
   return cmd($host, 'tftpd', 'Success.');      next if /^#/;
 }      next if /^$/;
       if ($in_ip_list) {
 sub sysinfo        s/\s+//g; # Whitespace is a no no
 {  
   my $host = shift;        if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
   return cmd($host, 'sysinfo', 'Success.');          push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
 }        } else {
           push @{ $conf{'ips'} }, $_;
 sub updateflash        }
 {      } else {
   my $host = shift;        my ($key, $val) = split /\s+/, $_, 2;
   my $old = shift;  
   my $new = shift;        if (lc($key) eq 'ips') {
           $in_ip_list = 1;
   return undef unless $new;          next;
         }
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);  
 }        $key =~ s/^\s+//;
         $key =~ s/\s+$//;
 sub cmd        $val =~ s/^\s+//;
 {        $val =~ s/\s+$//;
   my $host   = shift;  
   my $string = shift;        $conf{ lc($key) } = $val;
   my $expect_last = shift;      }
   my $timeout = shift || 5;    }
     close $fh;
   my @lines = $host->cmd(String => $string, Timeout => $timeout);  
     #print Dump \%conf;
   my $vals = decode_lines(@lines);    foreach (
       'password',
   my $last = $host->lastline;      'file_name', 'file_size', 'file_cksum',
       'ver', 'cksum', 'ips',
   unless ($expect_last) {    ) {
     return $vals;      die "No $_ specified in config file!"
   }        if (not exists $conf{$_});
     }
   if ($last =~ /$expect_last$/) {  
     return $vals;    #print Dump \%conf;
   } else {    #exit;
     warn "Error with command ($string): $last";    return \%conf;
     return undef;  }
   }  
 }  package Mylogger;
   
 sub decode_lines  use Fcntl ':flock'; # import LOCK_* constants
 {  #use YAML;
   my @lines = @_;  use constant LOG_PRINT => 128;
   use constant LOG_SAVE  =>  64;
   my %conf;  
   DESTROY {
   my $key = '';    my $self = shift;
   my $val = '';    if ($self->{'MYLOG'}) {
   my $in_key = 0;      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
   my $in_val = 0;      close $self->{'MYLOG'};
     }
   foreach my $line (@lines) {  }
     my @chars = split //, $line;  
   sub new {
     my $last_key = '';    my $package = shift;
     foreach my $c (@chars) {    my $self = shift || {};
   
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {    $self->{'base_path'}  ||= '.';
         if ($c eq '[') {    $self->{'log_path'}   ||= $self->{'base_path'};
           $in_key = 1;    $self->{'log_prefix'} ||= 'LOG';
           $in_val = 0;    $self->{'log_file'}   ||= GetLogName(
         } else {      $self->{'log_prefix'},
           $in_key = 0;      $self->{'log_path'}
           $in_val = 0;    );
         }    bless $self, $package;
   }
         if ($key) {  
           $key =~ s/^\s+//;  sub s
           $key =~ s/\s+$//;  {
     my $self = shift;
           $val =~ s/^\s+//;    my $m = shift;
           $val =~ s/\s+$//;    return $self->mylog($m, LOG_SAVE);
   }
           if ($key eq 'Checksum' && $last_key) {  
             # Special case for these bastids.  sub p
             my $new = $last_key;  {
             $new =~ s/\s+\S+$//;    my $self = shift;
             $key = $new . " " . $key;    my $m = shift;
           }    return $self->mylog($m, LOG_PRINT);
   }
           $last_key = $key;  
           $conf{$key} = $val;  sub sp
           $key = '';  {
           $val = '';    my $self = shift;
         }    my $m = shift;
     return $self->mylog($m, LOG_SAVE | LOG_PRINT);
       } elsif ($c eq ']') {  }
         $in_val = 1;  
         $in_key = 0;  sub mylog
         $c = shift @chars;  {
     my $self = shift;
       } elsif ($in_key) {  
         $key .= $c;    my $thing = shift;
     chomp $thing;
       } elsif ($in_val) {  
         $val .= $c;    my $which = shift;
       }  
     }    my $MYLOG;
   }    if ($which & LOG_PRINT) {
   #print Dump \%conf;      print $thing, "\n";
     }
   if (%conf) {  
     return \%conf;    if ($which & LOG_SAVE) {
   } else {      if ($self->{'MYLOG'}) {
     return \@lines;        $MYLOG = $self->{'MYLOG'};
   }      } else {
 }        unless ($MYLOG) {
                   open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
 sub read_conf            or die "Couldn't open logfile!\n";
 {          my $ofh = select $MYLOG;
   my $file = shift;          $|=1;
   my %conf;          select $ofh;
   my $in_ip_list = 0;          $self->{'MYLOG'} = $MYLOG;
   open my $fh, $file or die "Couldn't open file $file: $!";  
   while (<$fh>) {          $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
     chomp;        }
     next if /^#/;      }
     next if /^$/;      flock($MYLOG, LOCK_EX);
     if ($in_ip_list) {      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
       push @{ $conf{'ips'} }, $_;        or die "Couldn't print to MYLOG: $!";
     } else {      flock($MYLOG, LOCK_UN);
       my ($key, $val) = split /\s+/, $_, 2;    }
   }
       if (lc($key) eq 'ips') {  
         $in_ip_list = 1;  sub GetLogName
         next;  {
       }    my $prefix  = shift || die "Invalid prefix passed for log";
   
       $conf{ lc($key) } = $val;    my $logdate = GetLogDate();
     }    my $logver  = 0;
   }    my $logname;
   close $fh;  
     do {
   #print Dump \%conf;      $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
   foreach (      $logver++;
     'password',    } until (not -e $logname);
     'file_name', 'file_size', 'file_cksum',  
     'ver', 'cksum', 'ips',    return $logname;
   ) {  }
     die "No $_ specified in config file!"  
       if (not exists $conf{$_});  sub GetLogDate
   }  {
     my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   return \%conf;  
 }    $mon++;
     $year += 1900;
 sub mylog  
 {    if ($min  < 10) { $min  = "0$min"  }
         my $thing = shift;    if ($sec  < 10) { $sec  = "0$sec"  }
     chomp $thing;    if ($hour < 10) { $hour = "0$hour" }
     if ($mday < 10) { $mday = "0$mday" }
         unless ($MYLOG) {    if ($mon  < 10) { $mon  = "0$mon"  }
                 open ($MYLOG, '>>', $log_file)  
                         or die "Couldn't open logfile!\n";    my $time = $year . $mon . $mday;
                 my $ofh = select $MYLOG;  
                 $|=1;    return $time;
                 select $ofh;  }
         mylog("Opened log ($log_file)");  
         }  
   
     print $thing, "\n";  
         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.4  
changed lines
  Added in v.1.20

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