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