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