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