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

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

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