version 1.2, 2005/11/16 05:26:37 |
version 1.24, 2007/02/02 16:29:58 |
|
|
#!/usr/bin/perl
|
#!/usr/bin/perl |
# $RedRiver: update_trango.pl,v 1.1 2005/11/16 03:28:42 andrew Exp $
|
# $RedRiver: update_trango.pl,v 1.23 2007/02/01 18:44:30 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 YAML qw/ LoadFile /; |
use Net::TFTP;
|
use Net::TFTP; |
use YAML;
|
use lib '.'; |
|
use Net::Telnet::Trango; |
my @Foxes = (
|
|
'10.100.3.8',
|
my $config_file = shift || 'update_trango.yaml'; |
# '10.100.3.11',
|
my $max_tries = 3; |
# '10.100.3.12',
|
|
# '10.100.3.13',
|
my $l = Mylogger->new( { log_prefix => 'UT' } ); |
);
|
|
|
$l->sp("Reading config file '$config_file'"); |
my $password = 'password';
|
my $conf = LoadFile($config_file); |
|
|
my $new_file = 'fsu53_2p0a2H0003D05101403.s19';
|
my @foxes = ( |
my $new_ver = 'FSU 1p06H0003D04111001';
|
'10.100.1.2', |
my $new_cksum = 'x1D28DCC4';
|
'10.100.2.2', |
|
'10.100.3.2', |
my $max_tries = 3;
|
'10.100.3.3', |
|
'10.100.3.4', |
|
'10.100.3.8', |
|
'10.100.4.2', |
|
'10.100.60.5', |
|
'10.100.80.5', |
|
'10.100.150.5', |
foreach my $fox (@Foxes) {
|
'10.100.5.2', |
print "getting temp from Fox: $fox\n";
|
'10.100.6.2', |
## Connect and login.
|
'10.100.7.2', |
my $host = new Net::Telnet (Timeout => 5,
|
'10.100.8.2', |
Prompt => '/#> *$/');
|
'10.100.9.2', |
$host->open($fox);
|
'10.100.21.2', |
$host->dump_log('dump.log');
|
'10.100.22.2', |
|
'10.100.23.2', |
## Login to remote host.
|
'10.100.32.2', |
$host->waitfor(
|
'10.100.33.2', |
-match => '/password: ?$/i',
|
'10.100.41.2', |
-errmode => "return",
|
'10.100.51.2', |
) or die "problem connecting to host ($fox): ", $host->lastline;
|
'10.100.52.2', |
$host->print($password);
|
'10.100.53.2', |
$host->waitfor(
|
'10.100.54.2', |
-match => $host->prompt,
|
'10.100.61.2', |
-errmode => "return",
|
'10.100.62.2', |
) or die "login ($fox) failed: ", $host->lastline;
|
); |
|
|
|
foreach my $fox (@foxes) { |
## Send commands
|
$l->sp("Checking: $fox"); |
if ( upload($host, $new_file) ) {
|
my $needs_reboot = 0; |
$host->send('reboot');
|
|
} else {
|
## Connect and login. |
$host->send('exit');
|
my $t = new Net::Telnet::Trango ( |
}
|
Timeout => 5, |
$host->close;
|
Errmode => 'return', |
}
|
) or die "Couldn't make new connection: $!"; |
|
$l->p("Connecting to $fox"); |
sub upload
|
unless ( $t->open($fox) ) { |
{
|
$l->sp("Error connecting: $!"); |
my $host = shift;
|
next; |
my $file = shift;
|
} |
my $ver = get_ver($host);
|
|
|
foreach my $firmware_type ('Firmware', 'FPGA') { |
if (
|
|
$ver->{'Firmware Version'} eq $new_ver &&
|
if (! exists $conf->{$firmware_type}) { |
$ver->{'Checksum'} eq $new_cksum
|
$l->s("No configs for '$firmware_type'"); |
) {
|
$t->close(); |
print "Already updated!";
|
next; |
return 1;
|
} |
}
|
|
|
my $host_type = $t->host_type; |
my $try = 0;
|
if ($firmware_type eq 'FPGA') { |
while (1) {
|
$host_type =~ s/\s.*$//; |
$try++;
|
} |
|
|
enable_tftpd($host) || die "Couldn't enable tftpd";
|
if (! exists $conf->{$firmware_type}->{$host_type}) { |
|
$l->sp("No '$firmware_type' config for type $host_type"); |
# use tftp to push the file up
|
$t->close(); |
|
next; |
# waitfor some sort of output
|
} |
# make sure it says 'Success.' otherwise error
|
|
|
if (! $t->logged_in) { |
my $results = check_tftpd($host);
|
$l->p("Logging in"); |
# check the 'File Length' against ???
|
$t->login($conf->{general}->{'password'}); |
|
unless ($t->logged_in) { |
# 'updateflash mainimage $old_cksum $new_cksum'
|
$l->p('Failed!'); |
# OR
|
$t->bye(); |
# 'save mainimage [current firmware checksum] [new firmware checksum]'
|
next; |
# waitfor a prompt, look at the end of the output for 'Success.' otherwise
|
} |
# error.
|
} |
# decode_lines to get 'Checksum' to see if it matches $new_cksum
|
|
|
foreach my $k (keys %{ $conf->{general} }) { |
$ver = get_ver($host);
|
$conf->{$firmware_type}->{$host_type}->{$k} ||= $conf->{general}->{$k}; |
# check versions
|
} |
|
$conf->{$firmware_type}->{$host_type}->{firmware_type} ||= $firmware_type; |
|
$conf->{$firmware_type}->{$host_type}->{type} = $host_type; |
if ($try >= $max_tries) {
|
|
warn "Couldn't update in $max_tries tries!";
|
$l->sp("$host_type $firmware_type"); |
return undef;
|
$l->p("Sending commands"); |
}
|
## Send commands |
}
|
my $rc = upload($t, $conf->{$firmware_type}->{$host_type}); |
}
|
if ($rc) { |
|
$l->sp("Successfull!"); |
sub get_ver
|
$needs_reboot = 1; |
{
|
} elsif (defined $rc) { |
my $host = shift;
|
$l->sp("Already up to date"); |
return cmd($host, 'ver');
|
} else { |
}
|
$l->sp("Failed"); |
|
$t->bye; |
sub enable_tftpd
|
next; |
{
|
} |
my $host = shift;
|
|
|
} |
my $vals = cmd($host, 'tftpd on');
|
|
|
if ($needs_reboot) { |
if ($vals->{'Tftpd'} eq 'listen') {
|
$l->sp("Rebooting $fox"); |
return 1;
|
$t->reboot; |
} else {
|
} else { |
return undef;
|
$l->sp("Bye $fox"); |
}
|
$t->bye(); |
}
|
} |
|
$l->sp(""); |
sub check_tftpd
|
} |
{
|
|
my $host = shift;
|
sub upload |
return cmd($host, 'tftpd');
|
{ |
}
|
my $t = shift; |
|
my $conf = shift; |
sub cmd
|
|
{
|
my $file = $conf->{firmware_path} . '/' . $conf->{file_name}; |
my $host = shift;
|
|
my $string = shift;
|
my $fw_type = $conf->{firmware_type}; |
|
|
my @lines = $host->cmd($string);
|
my $ver = $t->ver; |
|
|
my $vals = decode_lines(@lines);
|
if ( |
return $vals;
|
$ver->{$fw_type . ' Version'} eq $conf->{'ver'} && |
}
|
$ver->{$fw_type . ' Checksum'} eq $conf->{'cksum'} |
|
) { |
sub decode_lines
|
return 0; |
{
|
} |
### XXX ver has 2 Checksums. one for FPGA and one for Firmware. DOH!
|
|
my @lines = @_;
|
$l->sp("Config information:"); |
|
$l->sp(" Hardware Type: $conf->{'type'}"); |
my %conf;
|
$l->sp(" File Name: $conf->{'file_name'}"); |
|
$l->sp(" File Size: $conf->{'file_size'}"); |
my $key = '';
|
$l->sp(" File Checksum: $conf->{'file_cksum'}"); |
my $val = '';
|
$l->sp(" Conf Version: $conf->{'ver'}"); |
my $in_key = 0;
|
$l->sp(" Cur Version: $ver->{$fw_type . ' Version'}"); |
my $in_val = 0;
|
$l->sp(" Conf Checksum: $conf->{'cksum'}"); |
|
$l->sp(" Cur Checksum: $ver->{$fw_type . ' Checksum'}"); |
foreach my $line (@lines) {
|
|
my @chars = split //, $line;
|
$l->sp("Updating"); |
|
my $try = 0; |
foreach my $c (@chars) {
|
while (1) { |
next if $c eq "\r";
|
if ($try >= $max_tries) { |
next if $c eq "\n";
|
$l->sp("Couldn't update in $max_tries tries!"); |
|
return; |
if ($c eq '[') {
|
} |
$in_key = 1;
|
$try++; |
$in_val = 0;
|
|
if ($key) {
|
#sysinfo($self->{'_host'}); |
$val =~ s/\s+$//;
|
|
$conf{$key} = $val;
|
$l->p("Enabling TFTPd"); |
$key = '';
|
unless ($t->enable_tftpd) { |
$val = '';
|
$l->sp("Couldn't enable tftpd"); |
}
|
next; |
|
} |
} elsif ($c eq ']') {
|
|
$in_val = 1;
|
$l->p("Uploading file ($file)"); |
$in_key = 0;
|
# use tftp to push the file up |
$c = shift @chars;
|
my $tftp = Net::TFTP->new($t->Host, Mode => 'octet'); |
|
|
} elsif ($in_key) {
|
unless ($tftp->put($file, $file)) { |
$key .= $c;
|
$l->sp("Error uploading: " . $tftp->error); |
|
next; |
} elsif ($in_val) {
|
} |
$val .= $c;
|
|
}
|
$l->p("Checking upload ($conf->{'file_cksum'})"); |
}
|
my $results = $t->tftpd; |
}
|
# check the 'File Length' against ??? |
print Dump \%conf;
|
if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) { |
|
$l->sp( |
if (%conf) {
|
"File checksum '" . $results->{'File Checksum'} . |
return \%conf;
|
" does not match config file '" . $conf->{'file_cksum'} . "'!" |
} else {
|
); |
return \@lines;
|
next; |
}
|
} |
}
|
$l->p("File checksum matches . . . "); |
|
|
|
if ($results->{'File Length'} !~ /^$conf->{'file_size'} bytes/) { |
|
$l->sp( |
|
"File length '" . $results->{'File Length'} . |
|
"does not match config file '" . $conf->{'file_size'} . " bytes'!" |
|
); |
|
next; |
|
} |
|
$l->p("File length matches . . . "); |
|
|
|
if ( uc($results->{'File Name'}) ne uc($file) ) { |
|
$l->sp( |
|
"File name '" . $results->{'File Name'} . |
|
"' does not match config file '" . $file . "'!" |
|
); |
|
next; |
|
} |
|
$l->p("File name matches . . . "); |
|
|
|
my $image_type = 'mainimage'; |
|
if ($fw_type eq 'FPGA') { |
|
$image_type = 'fpgaimage'; |
|
} |
|
$l->p("Updating $image_type (new checksum '$conf->{'cksum'}')"); |
|
unless ($results = $t->updateflash( |
|
args => $image_type . ' ' . $ver->{$fw_type . ' Checksum'} . |
|
' ' . $conf->{'cksum'}, |
|
Timeout => 90, |
|
) ) { |
|
$l->sp("Couldn't update flash: $!"); |
|
next; |
|
} |
|
|
|
unless ( |
|
defined $results->{'Checksum'} && |
|
$results->{'Checksum'} eq $conf->{'cksum'} |
|
) { |
|
$l->sp("Saved checksum " . $results->{'Checksum'} . " does not match config file " . $conf->{'cksum'} . "!"); |
|
next; |
|
} |
|
$l->p("Uploaded checksum ($results->{'Checksum'}) " . |
|
"matches ($conf->{'cksum'})"); |
|
|
|
$l->sp("Successfully updated!"); |
|
return 1; |
|
} |
|
} |
|
|
|
package Mylogger; |
|
|
|
use Fcntl ':flock'; # import LOCK_* constants |
|
#use YAML; |
|
use constant LOG_PRINT => 128; |
|
use constant LOG_SAVE => 64; |
|
|
|
DESTROY { |
|
my $self = shift; |
|
if ($self->{'MYLOG'}) { |
|
$self->p("Closing log ($self->{'log_path'}/$self->{'log_file'})"); |
|
close $self->{'MYLOG'}; |
|
} |
|
} |
|
|
|
sub new { |
|
my $package = shift; |
|
my $self = shift || {}; |
|
|
|
$self->{'base_path'} ||= '.'; |
|
$self->{'log_path'} ||= $self->{'base_path'}; |
|
$self->{'log_prefix'} ||= 'LOG'; |
|
$self->{'log_file'} ||= GetLogName( |
|
$self->{'log_prefix'}, |
|
$self->{'log_path'} |
|
); |
|
bless $self, $package; |
|
} |
|
|
|
sub s |
|
{ |
|
my $self = shift; |
|
my $m = shift; |
|
return $self->mylog($m, LOG_SAVE); |
|
} |
|
|
|
sub p |
|
{ |
|
my $self = shift; |
|
my $m = shift; |
|
return $self->mylog($m, LOG_PRINT); |
|
} |
|
|
|
sub sp |
|
{ |
|
my $self = shift; |
|
my $m = shift; |
|
return $self->mylog($m, LOG_SAVE | LOG_PRINT); |
|
} |
|
|
|
sub mylog |
|
{ |
|
my $self = shift; |
|
|
|
my $thing = shift; |
|
chomp $thing; |
|
|
|
my $which = shift; |
|
|
|
my $MYLOG; |
|
if ($which & LOG_PRINT) { |
|
print $thing, "\n"; |
|
} |
|
|
|
if ($which & LOG_SAVE) { |
|
if ($self->{'MYLOG'}) { |
|
$MYLOG = $self->{'MYLOG'}; |
|
} else { |
|
unless ($MYLOG) { |
|
open ($MYLOG, '>>', $self->{'log_path'} . '/' . $self->{'log_file'}) |
|
or die "Couldn't open logfile!\n"; |
|
my $ofh = select $MYLOG; |
|
$|=1; |
|
select $ofh; |
|
$self->{'MYLOG'} = $MYLOG; |
|
|
|
$self->p("Opened log ($self->{'log_path'}/$self->{'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; |
|
} |