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

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

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.16

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