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

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

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