[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.13 and 1.16

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

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