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

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

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