version 1.11, 2005/12/21 02:03:30 |
version 1.15, 2005/12/30 20:26:41 |
|
|
#!/usr/bin/perl
|
#!/usr/bin/perl
|
# $RedRiver: update_trango.pl,v 1.10 2005/12/21 01:17:06 andrew Exp $
|
# $RedRiver: update_trango.pl,v 1.14 2005/12/30 19:26:06 andrew Exp $
|
########################################################################
|
########################################################################
|
# update_trango.pl *** Updates trango foxes with a new firmware
|
# update_trango.pl *** Updates trango foxes with a new firmware
|
#
|
#
|
|
|
use warnings;
|
use warnings;
|
|
|
use Net::TFTP;
|
use Net::TFTP;
|
|
use YAML;
|
|
use lib '.';
|
|
use Net::Telnet::Trango;
|
|
|
my $config_file = shift || 'update_trango.conf';
|
my $config_file = shift || 'update_trango.conf';
|
my $max_tries = 3;
|
my $max_tries = 3;
|
|
|
$l->sp("Updating: $fox");
|
$l->sp("Updating: $fox");
|
|
|
## Connect and login.
|
## Connect and login.
|
my $t = new Trango::Telnet ({
|
my $t = new Net::Telnet::Trango (
|
Host => $fox,
|
|
Timeout => 5,
|
Timeout => 5,
|
});
|
Errmode => 'return',
|
|
) or die "Couldn't make new connection: $!";
|
|
|
$l->p("Connecting to $fox");
|
$l->p("Connecting to $fox");
|
my ($type, $version) = $t->connect();
|
unless ( $t->open($fox) ) {
|
|
$l->sp("Error connecting: $!");
|
unless (defined $type && defined $version) {
|
|
$l->sp("Error connecting!");
|
|
next;
|
next;
|
}
|
}
|
|
|
if ($type ne $conf->{'type'}) {
|
if ($t->host_type ne $conf->{'type'}) {
|
$l->sp("Wrong type of unit ('$type' should be '$conf->{'type'}')");
|
$l->sp("Wrong type of unit ('$t->host_type' should be '$conf->{'type'}')");
|
$t->close;
|
$t->close;
|
next;
|
next;
|
}
|
}
|
|
|
if ($version eq $conf->{'ver'}) {
|
if ($t->firmware_version eq $conf->{'ver'}) {
|
$l->sp("Already up to date with firmware version '$version'");
|
$l->sp("Already up to date with firmware version '$t->firmware_version'");
|
$t->close;
|
$t->close;
|
next;
|
next;
|
}
|
}
|
|
|
|
|
$l->p("Sending commands");
|
$l->p("Sending commands");
|
## Send commands
|
## Send commands
|
if ( upload($t, $conf->{'file_name'}) ) {
|
#print Dump $t->login_banner;
|
$l->p("Rebooting");
|
#print Dump $t->sysinfo;
|
$t->reboot;
|
#print Dump $t->last_lines;
|
} else {
|
#print Dump $t->ver();
|
|
#print Dump $t->tftpd();
|
|
#print Dump $t->enable_tftpd();
|
|
#print Dump [ "Exit", $t->exit ];
|
|
#print Dump $t->tftpd();
|
|
#print Dump $t->disable_tftpd();
|
|
#print Dump $t->tftpd();
|
|
print Dump $t->sulog(args => 5);
|
|
#if ( upload($t, $conf->{'file_name'}) ) {
|
|
# $l->p("Rebooting");
|
|
# $t->reboot;
|
|
#} else {
|
$l->p("Exiting");
|
$l->p("Exiting");
|
$t->exit;
|
$t->exit;
|
}
|
#}
|
$t->close;
|
|
$l->sp("");
|
$l->sp("");
|
}
|
}
|
|
|
|
|
|
|
$l->p("Updating flash (new checksum '$conf->{'cksum'}')");
|
$l->p("Updating flash (new checksum '$conf->{'cksum'}')");
|
unless ($results = $t->updateflash(
|
unless ($results = $t->updateflash(
|
$ver->{'Firmware Checksum'}, $conf->{'cksum'}
|
args => 'mainimage ' . $ver->{'Firmware Checksum'} .
|
|
' ' . $conf->{'cksum'},
|
|
Timeout => 90,
|
) ) {
|
) ) {
|
$l->sp("Couldn't update flash: $!");
|
$l->sp("Couldn't update flash: $!");
|
next;
|
next;
|
|
|
#exit;
|
#exit;
|
return \%conf;
|
return \%conf;
|
}
|
}
|
|
|
package Trango::Telnet;
|
|
use base 'Net::Telnet';
|
|
|
|
my %PRIVATE = (
|
|
connected => 0,
|
|
logged_in => 0,
|
|
);
|
|
|
|
sub new {
|
|
my $class = shift;
|
|
my $args = shift || {};
|
|
|
|
$args->{'Timeout'} ||= 5;
|
|
$args->{'Prompt'} ||= '/#> *$/';
|
|
|
|
foreach my $key (keys %{ $args }) {
|
|
$PRIVATE{$key} = $args->{$key};
|
|
}
|
|
|
|
my $self = $class->SUPER::new(%{ $args });
|
|
bless $self;
|
|
|
|
#bless $self, $package;
|
|
return $self;
|
|
}
|
|
|
|
sub connect
|
|
{
|
|
my $self = shift;
|
|
|
|
unless ( $self->open(
|
|
Host => $PRIVATE{'Host'},
|
|
Errmode => 'return',
|
|
) ) {
|
|
$! = "Couldn't connect to $self->{'Host'}. Connection timed out.";
|
|
return undef, undef;
|
|
}
|
|
#$self->dump_log('dump.log');
|
|
|
|
## Login to remote host.
|
|
unless ($self->waitfor(
|
|
-match => '/password: ?$/i',
|
|
-errmode => "return",
|
|
) ) {
|
|
$! = "problem connecting to host ($self->{'Host'}): " . $self->lastline;
|
|
return undef;
|
|
}
|
|
|
|
$self->login_banner($self->lastline);
|
|
|
|
$PRIVATE{'connected'} = 1;
|
|
|
|
return ($self->host_type, $self->firmware_version);
|
|
}
|
|
|
|
sub login
|
|
{
|
|
my $self = shift;
|
|
|
|
my $password = shift;
|
|
|
|
$self->print($password);
|
|
unless ($self->waitfor(
|
|
-match => $self->prompt,
|
|
-errmode => "return",
|
|
) ) {
|
|
$! = "login ($self->{'Host'}) failed: " . $self->lastline;
|
|
return undef;
|
|
}
|
|
|
|
$PRIVATE{'logged_in'} = 1;
|
|
|
|
return 1;
|
|
}
|
|
|
|
sub login_banner
|
|
{
|
|
my $self = shift;
|
|
|
|
my $banner = shift || $PRIVATE{'login_banner'};
|
|
|
|
my ($type, $ver) = $banner =~
|
|
/Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;
|
|
|
|
$self->host_type($type);
|
|
$self->firmware_version($ver);
|
|
|
|
return $banner;
|
|
}
|
|
|
|
sub host_type
|
|
{
|
|
my $self = shift;
|
|
|
|
my $type = shift || $PRIVATE{'host_type'};
|
|
$PRIVATE{'host_type'} = $type;
|
|
|
|
return $type;
|
|
}
|
|
|
|
sub firmware_version
|
|
{
|
|
my $self = shift;
|
|
|
|
my $ver = shift || $PRIVATE{'firmware_version'};
|
|
$PRIVATE{'firmware_version'} = $ver;
|
|
|
|
return $ver
|
|
}
|
|
|
|
sub Host
|
|
{
|
|
my $self = shift;
|
|
|
|
my $host = shift || $PRIVATE{'Host'};
|
|
$PRIVATE{'HOST'} = $host;
|
|
|
|
return $host;
|
|
}
|
|
|
|
|
|
sub reboot
|
|
{
|
|
my $self = shift;
|
|
|
|
$self->print("reboot\n");
|
|
$self->getline;
|
|
|
|
return 1;
|
|
}
|
|
|
|
sub exit
|
|
{
|
|
my $self = shift;
|
|
|
|
$self->print("exit\n");
|
|
$self->getline;
|
|
|
|
return 1;
|
|
}
|
|
|
|
sub ver
|
|
{
|
|
my $self = shift;
|
|
|
|
return $self->cmd('ver');
|
|
}
|
|
|
|
sub tftpd
|
|
{
|
|
my $self = shift;
|
|
|
|
return $self->cmd('tftpd', 'Success.');
|
|
}
|
|
|
|
sub sysinfo
|
|
{
|
|
my $self = shift;
|
|
|
|
return $self->cmd('sysinfo', 'Success.');
|
|
}
|
|
|
|
sub enable_tftpd
|
|
{
|
|
my $self = shift;
|
|
|
|
my $vals = $self->cmd('tftpd on', 'Success.');
|
|
|
|
if ($vals->{'Tftpd'} eq 'listen') {
|
|
return $vals;
|
|
} else {
|
|
return undef;
|
|
}
|
|
}
|
|
|
|
sub updateflash
|
|
{
|
|
my $self = shift;
|
|
|
|
my $old = shift;
|
|
my $new = shift;
|
|
|
|
return undef unless $new;
|
|
|
|
return $self->cmd("updateflash mainimage $old $new", 'Success.', 90);
|
|
}
|
|
|
|
sub cmd
|
|
{
|
|
my $self = shift;
|
|
|
|
my $string = shift;
|
|
my $expect_last = shift;
|
|
my $timeout = shift || $PRIVATE{'Timeout'};
|
|
|
|
unless (defined $string) {
|
|
$! = "No command passed";
|
|
return undef;
|
|
}
|
|
|
|
unless ($PRIVATE{'connected'}) {
|
|
$! = "Not connected";
|
|
return undef;
|
|
}
|
|
|
|
unless ($PRIVATE{'logged_in'}) {
|
|
$! = "Not logged in";
|
|
return undef;
|
|
}
|
|
|
|
my @lines = $self->SUPER::cmd(String => $string, Timeout => $timeout);
|
|
|
|
my $vals = _decode_lines(@lines);
|
|
|
|
unless ($expect_last) {
|
|
return $vals;
|
|
}
|
|
|
|
my $last = $self->lastline;
|
|
|
|
if ($last =~ /$expect_last$/) {
|
|
return $vals;
|
|
} else {
|
|
warn "Error with command ($string): $last";
|
|
return undef;
|
|
}
|
|
}
|
|
|
|
sub _decode_lines
|
|
{
|
|
my @lines = @_;
|
|
|
|
my %conf;
|
|
|
|
my $key = '';
|
|
my $val = '';
|
|
my $in_key = 0;
|
|
my $in_val = 0;
|
|
|
|
foreach my $line (@lines) {
|
|
my @chars = split //, $line;
|
|
|
|
my $last_key = '';
|
|
foreach my $c (@chars) {
|
|
|
|
if ($c eq '[' || $c eq "\r" || $c eq "\n") {
|
|
if ($c eq '[') {
|
|
$in_key = 1;
|
|
$in_val = 0;
|
|
} else {
|
|
$in_key = 0;
|
|
$in_val = 0;
|
|
}
|
|
|
|
if ($key) {
|
|
$key =~ s/^\s+//;
|
|
$key =~ s/\s+$//;
|
|
|
|
$val =~ s/^\s+//;
|
|
$val =~ s/\s+$//;
|
|
|
|
if ($key eq 'Checksum' && $last_key) {
|
|
# Special case for these bastids.
|
|
my $new = $last_key;
|
|
$new =~ s/\s+\S+$//;
|
|
$key = $new . " " . $key;
|
|
}
|
|
|
|
$last_key = $key;
|
|
$conf{$key} = $val;
|
|
$key = '';
|
|
$val = '';
|
|
}
|
|
|
|
} elsif ($c eq ']') {
|
|
$in_val = 1;
|
|
$in_key = 0;
|
|
$c = shift @chars;
|
|
|
|
} elsif ($in_key) {
|
|
$key .= $c;
|
|
|
|
} elsif ($in_val) {
|
|
$val .= $c;
|
|
}
|
|
}
|
|
}
|
|
#print Dump \%conf;
|
|
|
|
if (%conf) {
|
|
return \%conf;
|
|
} else {
|
|
return \@lines;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
package Mylogger;
|
package Mylogger;
|
|
|