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

version 1.14, 2005/12/30 19:26:06 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.13 2005/12/30 01:02:41 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::TFTP;  use Net::TFTP;
 use YAML;  use lib '.';
 use lib '.';  use Net::Telnet::Trango;
 use Net::Telnet::Trango;  
   my $config_file = shift || 'update_trango.conf';
 my $config_file = shift || 'update_trango.conf';  my $max_tries = 3;
 my $max_tries = 3;  
   
   
   my $l = Mylogger->new( { log_prefix => 'UT' } );
 my $l = Mylogger->new( { log_prefix => 'UT' } );  
   
   $l->sp("Reading config file '$config_file'");
 $l->sp("Reading config file '$config_file'");  my $conf = read_conf($config_file);
 my $conf = read_conf($config_file);  
   $l->sp("  Hardware Type: $conf->{'type'}");
 $l->sp("  Hardware Type: $conf->{'type'}");  $l->sp("  File Name:     $conf->{'file_name'}");
 $l->sp("  File Name:     $conf->{'file_name'}");  $l->sp("  File Size:     $conf->{'file_size'}");
 $l->sp("  File Size:     $conf->{'file_size'}");  $l->sp("  File Checksum: $conf->{'file_cksum'}");
 $l->sp("  File Checksum: $conf->{'file_cksum'}");  $l->sp("  FW Version:    $conf->{'ver'}");
 $l->sp("  FW Version:    $conf->{'ver'}");  $l->sp("  FW Checksum:   $conf->{'cksum'}");
 $l->sp("  FW Checksum:   $conf->{'cksum'}");  $l->sp("");
 $l->sp("");  
   
   
   
   
   
   foreach my $fox (@{ $conf->{'ips'} }) {
 foreach my $fox (@{ $conf->{'ips'} }) {    $l->sp("Updating: $fox");
   $l->sp("Updating: $fox");  
     ## Connect and login.
   ## Connect and login.    my $t = new Net::Telnet::Trango (
   my $t = new Net::Telnet::Trango (      Timeout => 5,
     Timeout => 5,      Errmode => 'return',
     Errmode => 'return',    ) or die "Couldn't make new connection: $!";
   ) or die "Couldn't make new connection: $!";    $l->p("Connecting to $fox");
     unless ( $t->open($fox) ) {
   $l->p("Connecting to $fox");      $l->sp("Error connecting: $!");
   unless ( $t->open($fox) ) {      next;
     $l->sp("Error connecting: $!");    }
     next;  
   }    if ($t->host_type ne $conf->{'type'}) {
       $l->sp("Wrong type of unit ('" . $t->host_type . "' should be '$conf->{'type'}')");
   if ($t->host_type ne $conf->{'type'}) {      $t->close;
     $l->sp("Wrong type of unit ('$t->host_type' should be '$conf->{'type'}')");      next;
     $t->close;    }
     next;  
   }    if ($t->firmware_version eq $conf->{'ver'}) {
       $l->sp("Already up to date with firmware version '" . $t->firmware_version . "'");
   if ($t->firmware_version eq $conf->{'ver'}) {      $t->close;
     $l->sp("Already up to date with firmware version '$t->firmware_version'");      next;
     $t->close;    }
     next;  
   }    $l->p("Logging in");
     $t->login($conf->{'password'}) || die "Couldn't login: $!";
   $l->p("Logging in");  
   $t->login($conf->{'password'}) || die "Couldn't login: $!";    $l->p("Sending commands");
     ## Send commands
   $l->p("Sending commands");    if ( upload($t, $conf->{'file_name'}) ) {
   ## Send commands       $l->p("Rebooting");
   #print Dump $t->login_banner;      $t->reboot;
   #print Dump $t->sysinfo;    } else {
   #print Dump $t->last_lines;      $l->p("Exiting");
   #print Dump $t->ver();      $t->exit;
   print Dump $t->tftpd();    }
   print Dump $t->enable_tftpd();  
   #print Dump [ "Exit", $t->exit ];    $l->sp("");
   print Dump $t->tftpd();  }
   print Dump $t->disable_tftpd();  
   print Dump $t->tftpd();  sub upload
   #if ( upload($t, $conf->{'file_name'}) ) {  {
   #   $l->p("Rebooting");    my $t    = shift;
   #  $t->reboot;    my $file = shift;
   #} else {  
     $l->p("Exiting");    my $ver = $t->ver;
     $t->exit;    $l->p("Current version '$ver->{'Firmware Version'}'");
   #}  
       if (
   $l->sp("");      $ver->{'Firmware Version'}  eq $conf->{'ver'} &&
 }      $ver->{'Firmware Checksum'} eq $conf->{'cksum'}
     ) {
 sub upload      $l->sp("Already updated!");
 {      return 1;
   my $t    = shift;    }
   my $file = shift;  
     my $try = 0;
   my $ver = $t->ver;    while (1) {
   $l->p("Current version '$ver->{'Firmware Version'}'");      if ($try >= $max_tries) {
         $l->sp("Couldn't update in $max_tries tries!");
   if (        return undef;
     $ver->{'Firmware Version'}  eq $conf->{'ver'} &&      }
     $ver->{'Firmware Checksum'} eq $conf->{'cksum'}      $try++;
   ) {  
     $l->sp("Already updated!");      #sysinfo($self->{'_host'});
     return 1;  
   }      $l->p("Enabling TFTPd");
       $t->enable_tftpd || die "Couldn't enable tftpd";
   my $try = 0;  
   while (1) {      $l->p("Uploading file ($file)");
     if ($try >= $max_tries) {      # use tftp to push the file up
       $l->sp("Couldn't update in $max_tries tries!");      my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');
       return undef;  
     }      $tftp->put($file, $file)
     $try++;        or die "Error uploading: " . $tftp->error;
   
     #sysinfo($self->{'_host'});      # waitfor some sort of output
           # make sure it says 'Success.' otherwise error
     $l->p("Enabling TFTPd");      #print "LAST: " . $self->lastline;
     $t->enable_tftpd || die "Couldn't enable tftpd";      #my @lines = $self->getlines;
       #print Dump \@lines;
     $l->p("Uploading file ($file)");  
     # use tftp to push the file up      $l->p("Checking upload ($conf->{'file_cksum'})");
     my $tftp = Net::TFTP->new($t->Host, Mode => 'octet');      my $results = $t->tftpd;
       # check the 'File Length' against ???
     $tftp->put($file, $file)      if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
       or die "Error uploading: " . $tftp->error;        $l->sp(
           "File checksum '" . $results->{'File Checksum'} .
     # waitfor some sort of output          "does not match config file '" . $conf->{'file_cksum'} . "'!"
     # make sure it says 'Success.' otherwise error        );
     #print "LAST: " . $self->lastline;        next;
     #my @lines = $self->getlines;      }
     #print Dump \@lines;      $l->p("File checksum matches . . . ");
       
     $l->p("Checking upload ($conf->{'file_cksum'})");      if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {
     my $results = $t->tftpd;        $l->sp(
     # check the 'File Length' against ???          "File length '" . $results->{'File Length'} .
     if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {          "does not match config file '" . $conf->{'file_size'} . " bytes'!"
       $l->sp(        );
         "File checksum '" . $results->{'File Checksum'} .        next;
         "does not match config file '" . $conf->{'file_cksum'} . "'!"      }
       );      $l->p("File length matches . . . ");
       next;  
     }      if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
     $l->p("File checksum matches . . . ");        $l->sp(
           "File name '" . $results->{'File Name'} .
     if ($results->{'File Length'}   !~ /^$conf->{'file_size'} bytes/) {          "' does not match config file '" . $conf->{'file_name'} . "'!"
       $l->sp(        );
         "File length '" . $results->{'File Length'} .        next;
         "does not match config file '" . $conf->{'file_size'} . " bytes'!"      }
       );      $l->p("File name  matches . . . ");
       next;  
     }      $l->p("Updating flash (new checksum '$conf->{'cksum'}')");
     $l->p("File length matches . . . ");      unless ($results = $t->updateflash(
         args => 'mainimage ' . $ver->{'Firmware Checksum'} .
     if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {                ' '          . $conf->{'cksum'},
       $l->sp(        Timeout => 90,
         "File name '" . $results->{'File Name'} .      ) ) {
         "' does not match config file '" . $conf->{'file_name'} . "'!"        $l->sp("Couldn't update flash: $!");
       );        next;
       next;      }
     }  
     $l->p("File name  matches . . . ");      unless (
         defined $results->{'Checksum'} &&
     $l->p("Updating flash (new checksum '$conf->{'cksum'}')");        $results->{'Checksum'} eq $conf->{'cksum'}
     unless ($results = $t->updateflash(      ) {
       args => 'mainimage ' . $ver->{'Firmware Checksum'} .        $l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " .  $conf->{'cksum'} . "!");
               ' '          . $conf->{'cksum'},        next;
       Timeout => 90,      }
     ) ) {      $l->p("Uploaded checksum ($results->{'Checksum'}) " .
       $l->sp("Couldn't update flash: $!");            "matches ($conf->{'cksum'})");
       next;  
     }      $l->sp("Successfully updated!");
       return 1;
     unless (    }
       defined $results->{'Checksum'} &&  }
       $results->{'Checksum'} eq $conf->{'cksum'}  
     ) {  sub read_conf
       $l->sp("Saved checksum does not match config file!");  {
       next;    my $file = shift;
     }    my %conf;
     $l->p("Uploaded checksum ($results->{'Checksum'}) " .    my $in_ip_list = 0;
           "matches ($conf->{'cksum'})");    open my $fh, $file or die "Couldn't open file $file: $!";
         while (<$fh>) {
     $l->sp("Successfully updated!");      chomp;
     return 1;      next if /^#/;
   }      next if /^$/;
 }      if ($in_ip_list) {
         s/\s+//g; # Whitespace is a no no
 sub read_conf  
 {        if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
   my $file = shift;          push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
   my %conf;        } else {
   my $in_ip_list = 0;          push @{ $conf{'ips'} }, $_;
   open my $fh, $file or die "Couldn't open file $file: $!";        }
   while (<$fh>) {      } else {
     chomp;        my ($key, $val) = split /\s+/, $_, 2;
     next if /^#/;  
     next if /^$/;        if (lc($key) eq 'ips') {
     if ($in_ip_list) {          $in_ip_list = 1;
       s/\s+//g; # Whitespace is a no no          next;
         }
       if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {  
         push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);        $key =~ s/^\s+//;
       } else {        $key =~ s/\s+$//;
         push @{ $conf{'ips'} }, $_;        $val =~ s/^\s+//;
       }        $val =~ s/\s+$//;
     } else {  
       my ($key, $val) = split /\s+/, $_, 2;        $conf{ lc($key) } = $val;
       }
       if (lc($key) eq 'ips') {    }
         $in_ip_list = 1;    close $fh;
         next;  
       }    #print Dump \%conf;
     foreach (
       $key =~ s/^\s+//;      'password',
       $key =~ s/\s+$//;      'file_name', 'file_size', 'file_cksum',
       $val =~ s/^\s+//;      'ver', 'cksum', 'ips',
       $val =~ s/\s+$//;    ) {
       die "No $_ specified in config file!"
       $conf{ lc($key) } = $val;        if (not exists $conf{$_});
     }    }
   }  
   close $fh;    #print Dump \%conf;
     #exit;
   #print Dump \%conf;    return \%conf;
   foreach (  }
     'password',  
     'file_name', 'file_size', 'file_cksum',  package Mylogger;
     'ver', 'cksum', 'ips',  
   ) {  use Fcntl ':flock'; # import LOCK_* constants
     die "No $_ specified in config file!"  #use YAML;
       if (not exists $conf{$_});  use constant LOG_PRINT => 128;
   }  use constant LOG_SAVE  =>  64;
   
   #print Dump \%conf;  DESTROY {
   #exit;    my $self = shift;
   return \%conf;    if ($self->{'MYLOG'}) {
 }      $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");
       close $self->{'MYLOG'};
 package Mylogger;    }
   }
 use Fcntl ':flock'; # import LOCK_* constants  
 #use YAML;  sub new {
 use constant LOG_PRINT => 128;    my $package = shift;
 use constant LOG_SAVE  =>  64;    my $self = shift || {};
   
 DESTROY {    $self->{'base_path'}  ||= '.';
   my $self = shift;    $self->{'log_path'}   ||= $self->{'base_path'};
   if ($self->{'MYLOG'}) {    $self->{'log_prefix'} ||= 'LOG';
     $self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})");    $self->{'log_file'}   ||= GetLogName(
     close $self->{'MYLOG'};      $self->{'log_prefix'},
   }      $self->{'log_path'}
 }    );
     bless $self, $package;
 sub new {  }
   my $package = shift;  
   my $self = shift || {};  sub s
   {
   $self->{'base_path'}  ||= '.';    my $self = shift;
   $self->{'log_path'}   ||= $self->{'base_path'};    my $m = shift;
   $self->{'log_prefix'} ||= 'LOG';    return $self->mylog($m, LOG_SAVE);
   $self->{'log_file'}   ||= GetLogName(  }
     $self->{'log_prefix'},  
     $self->{'log_path'}  sub p
   );  {
   bless $self, $package;    my $self = shift;
 }    my $m = shift;
     return $self->mylog($m, LOG_PRINT);
 sub s  }
 {  
   my $self = shift;  sub sp
   my $m = shift;  {
   return $self->mylog($m, LOG_SAVE);    my $self = shift;
 }    my $m = shift;
     return $self->mylog($m, LOG_SAVE | LOG_PRINT);
 sub p  }
 {  
   my $self = shift;  sub mylog
   my $m = shift;  {
   return $self->mylog($m, LOG_PRINT);    my $self = shift;
 }  
     my $thing = shift;
 sub sp    chomp $thing;
 {  
   my $self = shift;    my $which = shift;
   my $m = shift;  
   return $self->mylog($m, LOG_SAVE | LOG_PRINT);    my $MYLOG;
 }    if ($which & LOG_PRINT) {
       print $thing, "\n";
 sub mylog    }
 {  
   my $self = shift;    if ($which & LOG_SAVE) {
       if ($self->{'MYLOG'}) {
   my $thing = shift;        $MYLOG = $self->{'MYLOG'};
   chomp $thing;      } else {
         unless ($MYLOG) {
   my $which = shift;                  open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})
             or die "Couldn't open logfile!\n";
   my $MYLOG;          my $ofh = select $MYLOG;
   if ($which & LOG_PRINT) {          $|=1;
     print $thing, "\n";          select $ofh;
   }          $self->{'MYLOG'} = $MYLOG;
   
   if ($which & LOG_SAVE) {          $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");
     if ($self->{'MYLOG'}) {        }
       $MYLOG = $self->{'MYLOG'};      }
     } else {      flock($MYLOG, LOCK_EX);
       unless ($MYLOG) {      print $MYLOG (scalar gmtime), "\t", $thing, "\n"
                 open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'})        or die "Couldn't print to MYLOG: $!";
           or die "Couldn't open logfile!\n";      flock($MYLOG, LOCK_UN);
         my $ofh = select $MYLOG;    }
         $|=1;  }
         select $ofh;  
         $self->{'MYLOG'} = $MYLOG;  sub GetLogName
   {
         $self->p("Opened log ($self->{'log_path'}/$self->{'log_file'})");    my $prefix  = shift || die "Invalid prefix passed for log";
       }  
     }    my $logdate = GetLogDate();
     flock($MYLOG, LOCK_EX);    my $logver  = 0;
     print $MYLOG (scalar gmtime), "\t", $thing, "\n"    my $logname;
       or die "Couldn't print to MYLOG: $!";  
     flock($MYLOG, LOCK_UN);    do {
   }      $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';
 }      $logver++;
     } until (not -e $logname);
 sub GetLogName  
 {    return $logname;
   my $prefix  = shift || die "Invalid prefix passed for log";  }
   
   my $logdate = GetLogDate();  sub GetLogDate
   my $logver  = 0;  {
   my $logname;    my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();
   
   do {    $mon++;
     $logname = $prefix . $logdate . sprintf("%02d", $logver) . '.log';    $year += 1900;
     $logver++;  
   } until (not -e $logname);    if ($min  < 10) { $min  = "0$min"  }
     if ($sec  < 10) { $sec  = "0$sec"  }
   return $logname;    if ($hour < 10) { $hour = "0$hour" }
 }    if ($mday < 10) { $mday = "0$mday" }
     if ($mon  < 10) { $mon  = "0$mon"  }
 sub GetLogDate  
 {    my $time = $year . $mon . $mday;
   my ($sec,$min,$hour,$mday,$mon,$year,,,) = localtime();  
     return $time;
   $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.14  
changed lines
  Added in v.1.18

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