[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.3 and 1.18

version 1.3, 2005/11/16 20:48:48 version 1.18, 2007/01/31 19:01:12
Line 1 
Line 1 
 #!/usr/bin/perl  #!/usr/bin/perl
 # $RedRiver: update_trango.pl,v 1.2 2005/11/16 05:26:37 andrew Exp $  # $RedRiver: update_trango.pl,v 1.17 2007/01/31 18:44:27 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 Net::TFTP;
 use Net::TFTP;  use lib '.';
 #use YAML;  use Net::Telnet::Trango;
   
   my $config_file = shift || 'update_trango.conf';
 my $config_file = shift || 'fox53.conf';  my $max_tries = 3;
 my $conf = read_conf($config_file);  
   
 my $max_tries = 1;  
   my $l = Mylogger->new( { log_prefix => 'UT' } );
   
   
   $l->sp("Reading config file '$config_file'");
   my $conf = read_conf($config_file);
   
 foreach my $fox (@{ $conf->{'ips'} }) {  $l->sp("  Hardware Type: $conf->{'type'}");
   print STDERR "Updating: $fox\n";  $l->sp("  File Name:     $conf->{'file_name'}");
   $l->sp("  File Size:     $conf->{'file_size'}");
   ## Connect and login.  $l->sp("  File Checksum: $conf->{'file_cksum'}");
   my $host = new Net::Telnet (Timeout => 5,  $l->sp("  FW Version:    $conf->{'ver'}");
                               Prompt => '/#> *$/');  $l->sp("  FW Checksum:   $conf->{'cksum'}");
   print "Connecting to $fox\n";  $l->sp("");
   $host->open($fox);  
   $host->dump_log('dump.log');  
   
   ## Login to remote host.  
   $host->waitfor(  
     -match => '/password: ?$/i',  
     -errmode => "return",  foreach my $fox (@{ $conf->{'ips'} }) {
   ) or die "problem connecting to host ($fox): ", $host->lastline;    $l->sp("Updating: $fox");
   
   my $login_banner = $host->lastline;    ## Connect and login.
   my ($type, $version) = $login_banner =~    my $t = new Net::Telnet::Trango (
     /Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;      Timeout => 5,
       Errmode => 'return',
   if ($type ne $conf->{'type'}) {    ) or die "Couldn't make new connection: $!";
     print STDERR "Wrong type of unit ('$type' should be '$conf->{'type'}')\n";    $l->p("Connecting to $fox");
     $host->close;    unless ( $t->open($fox) ) {
     next;      $l->sp("Error connecting: $!");
   }      next;
     }
   if ($version eq $conf->{'ver'}) {  
     print STDERR "Already up to date with firmware version '$version'\n";    if ($t->host_type ne $conf->{'type'}) {
     $host->close;      $l->sp("Wrong type of unit ('" . $t->host_type . "' should be '$conf->{'type'}')");
     next;      $t->close;
   }      next;
     }
   print "Logging in\n";  
   $host->print($conf->{'password'});    if ($t->firmware_version eq $conf->{'ver'}) {
   $host->waitfor(      $l->sp("Already up to date with firmware version '" . $t->firmware_version . "'");
     -match => $host->prompt,      $t->close;
     -errmode => "return",      next;
   ) or die "login ($fox) failed: ", $host->lastline;    }
   
     $l->p("Logging in");
   print "Sending commands\n";    $t->login($conf->{'password'}) || die "Couldn't login: $!";
   ## Send commands  
   if ( upload($host, $fox, $conf->{'file_name'}) ) {    $l->p("Sending commands");
     print "Rebooting\n";    ## Send commands
     $host->print("reboot\n");    if ( upload($t, $conf->{'file_name'}) ) {
     $host->getline;       $l->p("Rebooting");
   } else {      $t->reboot;
     print "Exiting\n";    } else {
     $host->print("exit\n");      $l->p("Exiting");
     $host->getline;      $t->exit;
   }    }
   $host->close;  
 }    $l->sp("");
   }
 sub upload  
 {  sub upload
   my $host = shift;  {
   my $fox  = shift;    my $t    = shift;
   my $file = shift;    my $file = shift;
   
   print "Getting current version\n";    my $ver = $t->ver;
   my $ver = get_ver($host);    $l->p("Current version '$ver->{'Firmware Version'}'");
   
   if (    if (
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&      $ver->{'Firmware Version'}  eq $conf->{'ver'} &&
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}      $ver->{'Firmware Checksum'} eq $conf->{'cksum'}
   ) {    ) {
     print STDERR "Already updated!";      $l->sp("Already updated!");
     return 1;      return 1;
   }    }
   
   my $try = 0;    my $try = 0;
   while (1) {    while (1) {
     if ($try >= $max_tries) {      if ($try >= $max_tries) {
       print STDERR "Couldn't update in $max_tries tries!";        $l->sp("Couldn't update in $max_tries tries!");
       return undef;        return undef;
     }      }
     $try++;      $try++;
   
     #sysinfo($host);      #sysinfo($self->{'_host'});
       
     print "Enabling TFTPd\n";      $l->p("Enabling TFTPd");
     enable_tftpd($host) || die "Couldn't enable tftpd";      $t->enable_tftpd || die "Couldn't enable tftpd";
   
     print "Uploading file\n";      $l->p("Uploading file ($file)");
     # use tftp to push the file up      # use tftp to push the file up
     my $tftp = Net::TFTP->new($fox, Mode => 'octet');      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
   
     $tftp->put($file, $file)      $tftp->put($file, $file)
       or die "Error uploading: " . $tftp->error;        or die "Error uploading: " . $tftp->error;
   
     # waitfor some sort of output      # waitfor some sort of output
     # make sure it says 'Success.' otherwise error      # make sure it says 'Success.' otherwise error
     #print "LAST: " . $host->lastline;      #print "LAST: " . $self->lastline;
     #my @lines = $host->getlines;      #my @lines = $self->getlines;
     #print Dump \@lines;      #print Dump \@lines;
       
     print "Checking upload\n";      $l->p("Checking upload ($conf->{'file_cksum'})");
     my $results = check_tftpd($host);      my $results = $t->tftpd;
     # check the 'File Length' against ???      # check the 'File Length' against ???
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
       print STDERR "File checksum does not match config file!";        $l->sp(
       next;          "File checksum '" . $results->{'File Checksum'} .
     }          "does not match config file '" . $conf->{'file_cksum'} . "'!"
         );
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {        next;
       print STDERR "File length does not match config file!";      }
       next;      $l->p("File checksum matches . . . ");
     }  
       if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {        $l->sp(
       print STDERR "File name does not match config file!";          "File length '" . $results->{'File Length'} .
       next;          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
     }        );
         next;
     print "Updating flash\n";      }
     $results = updateflash(      $l->p("File length matches . . . ");
       $host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}  
     ) or die "Couldn't update flash: " . $host->lastline;      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
     unless (        $l->sp(
       defined $results->{'Checksum'} &&          "File name '" . $results->{'File Name'} .
       $results->{'Checksum'} eq $conf->{'cksum'}          "' does not match config file '" . $conf->{'file_name'} . "'!"
     ) {        );
       print STDERR "Saved checksum does not match config file!";        next;
       next;      }
     }      $l->p("File name  matches . . . ");
       
     print STDERR "Successfully updated!\n";      $l->p("Updating flash (new checksum '$conf->{'cksum'}')");
     return 1;      unless ($results = $t->updateflash(
   }        args => 'mainimage ' . $ver->{'Firmware Checksum'} .
 }                ' '          . $conf->{'cksum'},
         Timeout => 90,
 sub get_ver      ) ) {
 {        $l->sp("Couldn't update flash: $!");
   my $host = shift;        next;
   return cmd($host, 'ver');      }
 }  
       unless (
 sub enable_tftpd        defined $results->{'Checksum'} &&
 {        $results->{'Checksum'} eq $conf->{'cksum'}
   my $host = shift;      ) {
         $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
   my $vals = cmd($host, 'tftpd on', 'Success.');        next;
       }
   if ($vals->{'Tftpd'} eq 'listen') {      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
     return $vals;            "matches ($conf->{'cksum'})");
   } else {  
     return undef;      $l->sp("Successfully updated!");
   }      return 1;
 }    }
   }
 sub check_tftpd  
 {  sub read_conf
   my $host = shift;  {
   return cmd($host, 'tftpd', 'Success.');    my $file = shift;
 }    my %conf;
     my $in_ip_list = 0;
 sub sysinfo    open my $fh, $file or die "Couldn't open file $file: $!";
 {    while (<$fh>) {
   my $host = shift;      chomp;
   return cmd($host, 'sysinfo', 'Success.');      next if /^#/;
 }      next if /^$/;
       if ($in_ip_list) {
 sub updateflash        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})/) {
   my $old = shift;          push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
   my $new = shift;        } else {
           push @{ $conf{'ips'} }, $_;
   return undef unless $new;        }
       } else {
   return cmd($host, "updateflash mainimage $old $new", 'Success.', 90);        my ($key, $val) = split /\s+/, $_, 2;
 }  
         if (lc($key) eq 'ips') {
 sub cmd          $in_ip_list = 1;
 {          next;
   my $host   = shift;        }
   my $string = shift;  
   my $expect_last = shift;        $key =~ s/^\s+//;
   my $timeout = shift || 5;        $key =~ s/\s+$//;
         $val =~ s/^\s+//;
   my @lines = $host->cmd(String => $string, Timeout => $timeout);        $val =~ s/\s+$//;
   
   my $vals = decode_lines(@lines);        $conf{ lc($key) } = $val;
       }
   my $last = $host->lastline;    }
     close $fh;
   unless ($expect_last) {  
     return $vals;    #print Dump \%conf;
   }    foreach (
       'password',
   if ($last =~ /$expect_last$/) {      'file_name', 'file_size', 'file_cksum',
     return $vals;      'ver', 'cksum', 'ips',
   } else {    ) {
     warn "Error with command ($string): $last";      die "No $_ specified in config file!"
     return undef;        if (not exists $conf{$_});
   }    }
 }  
     #print Dump \%conf;
 sub decode_lines    #exit;
 {    return \%conf;
   my @lines = @_;  }
   
   my %conf;  package Mylogger;
   
   my $key = '';  use Fcntl ':flock'; # import LOCK_* constants
   my $val = '';  #use YAML;
   my $in_key = 0;  use constant LOG_PRINT => 128;
   my $in_val = 0;  use constant LOG_SAVE  =>  64;
   
   foreach my $line (@lines) {  DESTROY {
     my @chars = split //, $line;    my $self = shift;
     if ($self->{'MYLOG'}) {
     my $last_key = '';      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
     foreach my $c (@chars) {      close $self->{'MYLOG'};
     }
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {  }
         if ($c eq '[') {  
           $in_key = 1;  sub new {
           $in_val = 0;    my $package = shift;
         } else {    my $self = shift || {};
           $in_key = 0;  
           $in_val = 0;    $self->{'base_path'}  ||= '.';
         }    $self->{'log_path'}   ||= $self->{'base_path'};
     $self->{'log_prefix'} ||= 'LOG';
         if ($key) {    $self->{'log_file'}   ||= GetLogName(
           $key =~ s/^\s+//;      $self->{'log_prefix'},
           $key =~ s/\s+$//;      $self->{'log_path'}
     );
           $val =~ s/^\s+//;    bless $self, $package;
           $val =~ s/\s+$//;  }
   
           if ($key eq 'Checksum' && $last_key) {  sub s
             # Special case for these bastids.  {
             my $new = $last_key;    my $self = shift;
             $new =~ s/\s+\S+$//;    my $m = shift;
             $key = $new . " " . $key;    return $self->mylog($m, LOG_SAVE);
           }  }
   
           $last_key = $key;  sub p
           $conf{$key} = $val;  {
           $key = '';    my $self = shift;
           $val = '';    my $m = shift;
         }    return $self->mylog($m, LOG_PRINT);
   }
       } elsif ($c eq ']') {  
         $in_val = 1;  sub sp
         $in_key = 0;  {
         $c = shift @chars;    my $self = shift;
     my $m = shift;
       } elsif ($in_key) {    return $self->mylog($m, LOG_SAVE | LOG_PRINT);
         $key .= $c;  }
   
       } elsif ($in_val) {  sub mylog
         $val .= $c;  {
       }    my $self = shift;
     }  
   }    my $thing = shift;
   #print Dump \%conf;    chomp $thing;
   
   if (%conf) {    my $which = shift;
     return \%conf;  
   } else {    my $MYLOG;
     return \@lines;    if ($which & LOG_PRINT) {
   }      print $thing, "\n";
 }    }
   
 sub read_conf    if ($which & LOG_SAVE) {
 {      if ($self->{'MYLOG'}) {
   my $file = shift;        $MYLOG = $self->{'MYLOG'};
   my %conf;      } else {
   my $in_ip_list = 0;        unless ($MYLOG) {
   open my $fh, $file or die "Couldn't open file $file: $!";                  open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
   while (<$fh>) {            or die "Couldn't open logfile!\n";
     chomp;          my $ofh = select $MYLOG;
     next if /^#/;          $|=1;
     next if /^$/;          select $ofh;
     if ($in_ip_list) {          $self->{'MYLOG'} = $MYLOG;
       push @{ $conf{'ips'} }, $_;  
     } else {          $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
       my ($key, $val) = split /\s+/, $_, 2;        }
       }
       if (lc($key) eq 'ips') {      flock($MYLOG, LOCK_EX);
         $in_ip_list = 1;      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
         next;        or die "Couldn't print to MYLOG: $!";
       }      flock($MYLOG, LOCK_UN);
     }
       $conf{ lc($key) } = $val;  }
     }  
   }  sub GetLogName
   close $fh;  {
     my $prefix  = shift || die "Invalid prefix passed for log";
   #print Dump \%conf;  
   foreach (    my $logdate = GetLogDate();
     'password',    my $logver  = 0;
     'file_name', 'file_size', 'file_cksum',    my $logname;
     'ver', 'cksum', 'ips',  
   ) {    do {
     die "No $_ specified in config file!"      $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
       if (not exists $conf{$_});      $logver++;
   }    } until (not -e $logname);
   
   return \%conf;    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.3  
changed lines
  Added in v.1.18

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