version 1.4, 2005/11/16 21:13:39 |
version 1.7, 2005/11/17 20:09:45 |
|
|
#!/usr/bin/perl
|
#!/usr/bin/perl
|
# $RedRiver: update_trango.pl,v 1.3 2005/11/16 20:48:48 andrew Exp $
|
# $RedRiver: update_trango.pl,v 1.6 2005/11/17 00:40:06 andrew Exp $
|
########################################################################
|
########################################################################
|
# update_trango.pl *** Updates trango foxes with a new firmware
|
# update_trango.pl *** Updates trango foxes with a new firmware
|
#
|
#
|
|
|
my $max_tries = 1;
|
my $max_tries = 1;
|
|
|
my $log_file = GetLogName('UT');
|
my $log_file = GetLogName('UT');
|
|
use constant LOG_SAVE => 1;
|
|
|
|
|
my $MYLOG; # file handle for logging so we don't have to close and open it all the time
|
my $MYLOG; # file handle for logging so we don't have to close and open it all the time
|
END {
|
END {
|
if ($MYLOG) {
|
if ($MYLOG) {
|
|
|
|
|
|
|
foreach my $fox (@{ $conf->{'ips'} }) {
|
foreach my $fox (@{ $conf->{'ips'} }) {
|
mylog("Updating: $fox");
|
mylog("Updating: $fox", LOG_SAVE);
|
|
|
## Connect and login.
|
## Connect and login.
|
my $host = new Net::Telnet (Timeout => 5,
|
my $host = new Net::Telnet (Timeout => 5,
|
Prompt => '/#> *$/');
|
Prompt => '/#> *$/');
|
mylog("Connecting to $fox");
|
mylog("Connecting to $fox");
|
$host->open($fox);
|
unless ( $host->open( Host => $fox, Errmode => 'return') ) {
|
|
mylog("Couldn't connect to $fox. Connection timed out.", LOG_SAVE);
|
|
next;
|
|
}
|
$host->dump_log('dump.log');
|
$host->dump_log('dump.log');
|
|
|
## Login to remote host.
|
## Login to remote host.
|
$host->waitfor(
|
unless ($host->waitfor(
|
-match => '/password: ?$/i',
|
-match => '/password: ?$/i',
|
-errmode => "return",
|
-errmode => "return",
|
) or die "problem connecting to host ($fox): ", $host->lastline;
|
) ) {
|
|
mylog("problem connecting to host ($fox): " . $host->lastline, LOG_SAVE);
|
|
next;
|
|
}
|
|
|
my $login_banner = $host->lastline;
|
my $login_banner = $host->lastline;
|
my ($type, $version) = $login_banner =~
|
my ($type, $version) = $login_banner =~
|
/Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;
|
/Welcome to Trango Broadband Wireless (\w+)-(.+)$/i;
|
|
|
if ($type ne $conf->{'type'}) {
|
if ($type ne $conf->{'type'}) {
|
mylog("Wrong type of unit ('$type' should be '$conf->{'type'}')");
|
mylog("Wrong type of unit ('$type' should be '$conf->{'type'}')", LOG_SAVE);
|
$host->close;
|
$host->close;
|
next;
|
next;
|
}
|
}
|
|
|
if ($version eq $conf->{'ver'}) {
|
if ($version eq $conf->{'ver'}) {
|
mylog("Already up to date with firmware version '$version'");
|
mylog("Already up to date with firmware version '$version'", LOG_SAVE);
|
$host->close;
|
$host->close;
|
next;
|
next;
|
}
|
}
|
|
|
mylog("Logging in");
|
mylog("Logging in");
|
$host->print($conf->{'password'});
|
$host->print($conf->{'password'});
|
$host->waitfor(
|
unless ($host->waitfor(
|
-match => $host->prompt,
|
-match => $host->prompt,
|
-errmode => "return",
|
-errmode => "return",
|
) or die "login ($fox) failed: ", $host->lastline;
|
) ) {
|
|
mylog("login ($fox) failed: " . $host->lastline);
|
|
next;
|
|
}
|
|
|
|
|
mylog("Sending commands");
|
mylog("Sending commands");
|
|
|
$host->getline;
|
$host->getline;
|
}
|
}
|
$host->close;
|
$host->close;
|
mylog("");
|
mylog("", LOG_SAVE);
|
}
|
}
|
|
|
sub upload
|
sub upload
|
|
|
$ver->{'Firmware Version'} eq $conf->{'ver'} &&
|
$ver->{'Firmware Version'} eq $conf->{'ver'} &&
|
$ver->{'Firmware Checksum'} eq $conf->{'cksum'}
|
$ver->{'Firmware Checksum'} eq $conf->{'cksum'}
|
) {
|
) {
|
mylog("Already updated!");
|
mylog("Already updated!", LOG_SAVE);
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
mylog("Enabling TFTPd");
|
mylog("Enabling TFTPd");
|
enable_tftpd($host) || die "Couldn't enable tftpd";
|
enable_tftpd($host) || die "Couldn't enable tftpd";
|
|
|
mylog("Uploading file");
|
mylog("Uploading file ($file)");
|
# use tftp to push the file up
|
# use tftp to push the file up
|
my $tftp = Net::TFTP->new($fox, Mode => 'octet');
|
my $tftp = Net::TFTP->new($fox, Mode => 'octet');
|
|
|
|
|
#my @lines = $host->getlines;
|
#my @lines = $host->getlines;
|
#print Dump \@lines;
|
#print Dump \@lines;
|
|
|
mylog("Checking upload");
|
mylog("Checking upload ($conf->{'file_cksum'})");
|
my $results = check_tftpd($host);
|
my $results = check_tftpd($host);
|
# check the 'File Length' against ???
|
# check the 'File Length' against ???
|
if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
|
if ( $results->{'File Checksum'} ne $conf->{'file_cksum'}) {
|
mylog("File checksum does not match config file!");
|
mylog("File checksum does not match config file!", LOG_SAVE);
|
next;
|
next;
|
}
|
}
|
|
mylog("File checksum ($results->{'File Checksum'}) " .
|
|
"matches ($conf->{'file_cksum'})");
|
|
|
if ($results->{'File Length'} !~ /^$conf->{'file_size'} bytes/) {
|
if ($results->{'File Length'} !~ /^$conf->{'file_size'} bytes/) {
|
mylog("File length does not match config file!");
|
mylog("File length does not match config file!", LOG_SAVE);
|
next;
|
next;
|
}
|
}
|
|
mylog("File length ($results->{'File Length'}) " .
|
|
"matches ($conf->{'file_size'})");
|
|
|
if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
|
if ( uc($results->{'File Name'}) ne uc($conf->{'file_name'}) ) {
|
mylog("File name does not match config file!");
|
mylog("File name does not match config file!", LOG_SAVE);
|
next;
|
next;
|
}
|
}
|
|
mylog("File name ($results->{'File Name'}) " .
|
|
"matches ($conf->{'file_name'})");
|
|
|
mylog("Updating flash");
|
mylog("Updating flash (new checksum '$conf->{'cksum'}')");
|
$results = updateflash(
|
$results = updateflash(
|
$host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}
|
$host, $ver->{'Firmware Checksum'}, $conf->{'cksum'}
|
) or die "Couldn't update flash: " . $host->lastline;
|
) or die "Couldn't update flash: " . $host->lastline;
|
|
|
defined $results->{'Checksum'} &&
|
defined $results->{'Checksum'} &&
|
$results->{'Checksum'} eq $conf->{'cksum'}
|
$results->{'Checksum'} eq $conf->{'cksum'}
|
) {
|
) {
|
mylog("Saved checksum does not match config file!");
|
mylog("Saved checksum does not match config file!", LOG_SAVE);
|
next;
|
next;
|
}
|
}
|
|
mylog("Uploaded checksum ($results->{'Checksum'}) " .
|
|
"matches ($conf->{'cksum'})");
|
|
|
mylog("Successfully updated!");
|
mylog("Successfully updated!", LOG_SAVE);
|
return 1;
|
return 1;
|
}
|
}
|
}
|
}
|
|
|
next if /^#/;
|
next if /^#/;
|
next if /^$/;
|
next if /^$/;
|
if ($in_ip_list) {
|
if ($in_ip_list) {
|
push @{ $conf{'ips'} }, $_;
|
if (/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})-(\d{1,3})/) {
|
|
push @{ $conf{'ips'} }, $1 . $_ for ($2..$3);
|
|
} else {
|
|
push @{ $conf{'ips'} }, $_;
|
|
}
|
} else {
|
} else {
|
my ($key, $val) = split /\s+/, $_, 2;
|
my ($key, $val) = split /\s+/, $_, 2;
|
|
|
|
|
if (not exists $conf{$_});
|
if (not exists $conf{$_});
|
}
|
}
|
|
|
|
#print Dump \%conf;
|
|
#exit;
|
return \%conf;
|
return \%conf;
|
}
|
}
|
|
|
sub mylog
|
sub mylog
|
{
|
{
|
my $thing = shift;
|
my $thing = shift;
|
chomp $thing;
|
chomp $thing;
|
|
my $save = shift;
|
|
|
unless ($MYLOG) {
|
print $thing, "\n";
|
open ($MYLOG, '>>', $log_file)
|
|
or die "Couldn't open logfile!\n";
|
|
my $ofh = select $MYLOG;
|
|
$|=1;
|
|
select $ofh;
|
|
mylog("Opened log ($log_file)");
|
|
}
|
|
|
|
print $thing, "\n";
|
if (defined $save && $save == LOG_SAVE) {
|
flock($MYLOG, LOCK_EX);
|
unless ($MYLOG) {
|
print $MYLOG (scalar gmtime), "\t", $thing, "\n"
|
open ($MYLOG, '>>', $log_file)
|
or die "Couldn't print to MYLOG: $!";
|
or die "Couldn't open logfile!\n";
|
flock($MYLOG, LOCK_UN);
|
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
|
sub GetLogName
|