--- mdap-server.pl 2007/04/24 14:53:04 30 +++ mdap-server.pl 2007/12/04 22:05:21 83 @@ -1,20 +1,29 @@ #!/usr/bin/perl +use warnings; use strict; use IO::Socket::Multicast; use Data::Dump qw/dump/; use Getopt::Long; +use lib './lib'; +use MDAP; + my $GROUP = '224.0.0.103'; my $PORT = '3235'; -my $debug = 0; +#my $debug = 0; # from MDAP my $quiet = 1; my $verbose = 0; +my $credentials = 0; + +my $plugins = 1; my $resend_search_delay = 3; my $tftp_path = '/srv/tftp/'; +my $flashed_cmd = 'system config led=flash'; + GetOptions( "port=i" => \$PORT, "group=s" => \$GROUP, @@ -23,9 +32,13 @@ "verbose!" => \$verbose, "search=i" => \$resend_search_delay, "tftp=s" => \$tftp_path, + "credentials" => \$credentials, + "exec=s" => \$flashed_cmd, + 'plugins!' => \$plugins, ); $quiet = 0 if $verbose; +$credentials = 1 if $debug; # tab-delimited list of user id/passwd to try on ants my @try_accounts = ( "Administrator\t" ); @@ -38,16 +51,17 @@ while(<$fh>) { chomp; next if /^#/ || /^$/ || /^\s+$/; - if (/^\S+\t\S+$/) { + if (/^\S+\s+\S+$/) { push @try_accounts, $_; } else { warn "invalid $passwd_path entry: $_\n"; } } - print "found ", $#try_accounts + 1, " accounts to try on password protected ants\n"; + print "found ", $#try_accounts + 1, " accounts to try on password protected ants", + $credentials ? " and display credentials" : "", "\n"; } -warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\n"; +warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\nflashed to current version: $flashed_cmd\n"; sub fw { my ($board, $offset,$len) = @_; @@ -87,6 +101,12 @@ return $hash; } +sub _dump_data { + my $data = shift; + $data =~ s/\s+/ /gs; + return substr($data,0,150); +} + sub mdap_send { my $data = shift; @@ -96,10 +116,9 @@ $sock->mcast_send( $data, "${GROUP}:${PORT}" ); if ($debug) { - warn ">> ", dump( $data ), $/; + warn ">> ${GROUP}:${PORT} >> ", dump( $data ), $/; } elsif( ! $quiet ) { - $data =~ s/\s+/ /gi; - warn ">> ", substr($data,0,70), $/; + warn ">> ",_dump_data($data),$/; } } @@ -148,14 +167,6 @@ delete $ant_ok_password->{$ant}; } -my $once; - -sub once { - my $m = join('', @_); - $once->{$m}++; - print $m if ($once->{$m} == 1); -} - my $status = ''; sub status { @@ -188,16 +199,16 @@ my $client_version = $h->{'MDAP-VERSION'}; $mdap_ver = $client_version if ($client_version); - print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet; - - warn dump($h),$/ if ($debug); - # we are getting our own messages (since our source port # is same as destination) next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# ); + print "<< $type $proto/$mdap_ver << ", length($data), " bytes: ",_dump_data($data),$/ unless $quiet; + + warn dump( $data, $h ),$/ if ($debug); + my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h ); - my $seq_nr = $h->{'SEQ-NR'}; + my $seq_nr = $1 if (defined $h->{'SEQ-NR'} && $h->{'SEQ-NR'} =~ m/^(-*\d)+/); #warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr); my ($user_id,$user_pwd) = ant_credentials( $ant ); @@ -217,22 +228,28 @@ next; } else { $ant_ok_password->{$ant}++; + print "$ant credentials $user_id $user_pwd\n" if ( $credentials && $ant_ok_password->{$ant} == 1 ) || $debug; } my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?"; if ( fw_exists( $board ) ) { my $build = $h->{'_BUILD'} || die "no _BUILD?"; + $build =~ s/^(\d+)\.(\d+)\.(\d+)\.(\d+).*$/$1.$2.$3.$4/; my $new_build = fw_build( $board ); if ( $build ne $new_build ) { - print "+ $ant version $build -> $new_build\n"; + print "+ $ant $board version $build -> $new_build\n"; mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:software upgrade\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); + $ant_flashing->{$ant}++; + next; } else { - once "$ant OK version $build", + once "$ant $board OK version $build", $ant_unknown_password->{$ant} ? ' with unknown password' : - $ant_ok_password->{$ant} ? ' password protected' : - '', - "\n"; + $ant_passwd->{$ant} ? ' password protected' : + ''; + $ant_flashing->{$ant} = 0; + # green|red|orange|flash|off + mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:$flashed_cmd\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); my $waiting = 0; my $count = 0; map { @@ -240,22 +257,44 @@ $count++; } keys %$ant_flashing; if ($waiting == 0) { - status "ALL $count ANTS FLASHED to $build\n"; + status "ALL $count ANTS FLASHED\n"; $ant_flashing = undef; } else { - status "$waiting of $count ants still flasing upto $build\n"; + status "$waiting of $count ants still flasing\n"; } } + + if ( $plugins ) { + foreach my $plugin ( MDAP->plugins ) { + warn "## calling $plugin\n" if $debug; + if ( my $command = $plugin->check( $h ) ) { + + warn ">> $plugin $ant $board ", + $h->{'ANT-NAME'}, " ", + $h->{'_BUILD'}, " ", + "\t:$command\n"; + + mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:$command\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); + last; + } + } + } + } else { - once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update\n"; + once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update"; } - } elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) { - print "+ $ant bootp mode re-flash started\n" if ($verbose); + + } elsif ( $type eq 'REPLY-EXEC-CLI' ) { + print "##<< $type $seq_nr\n$data\n" if $debug; + if ( $seq_nr == 1 ) { mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nSEQ-NR:2\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); - forget_ant( $ant ); - $ant_flashing->{$ant}++; + forget_ant( $ant ) if ( $ant_flashing->{$ant} ); + } elsif ( $seq_nr < 0 ) { + warn "EXEC-CLI failed: $data\n"; + } + } else { - print "!! reply ignored ", dump( $h ), $/; + print "!! reply $type ignored ", dump( $h ), $/; } } else {