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