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