/[mdap]/mdap-server.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /mdap-server.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 33 by dpavlin, Tue Apr 24 18:57:19 2007 UTC revision 69 by dpavlin, Sun Nov 18 10:27:37 2007 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    
3    use warnings;
4  use strict;  use strict;
5  use IO::Socket::Multicast;  use IO::Socket::Multicast;
6  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
7  use Getopt::Long;  use Getopt::Long;
8    
9    use lib './lib';
10    use MDAP;
11    
12  my $GROUP = '224.0.0.103';  my $GROUP = '224.0.0.103';
13  my $PORT  = '3235';  my $PORT  = '3235';
14    
15  my $debug = 0;  #my $debug = 0; # from MDAP
16  my $quiet = 1;  my $quiet = 1;
17  my $verbose = 0;  my $verbose = 0;
18  my $credentials = 0;  my $credentials = 0;
19    
20    my $plugins = 1;
21    
22  my $resend_search_delay = 3;  my $resend_search_delay = 3;
23  my $tftp_path = '/srv/tftp/';  my $tftp_path = '/srv/tftp/';
24    
25    my $flashed_cmd = 'system config led=flash';
26    
27  GetOptions(  GetOptions(
28          "port=i"        => \$PORT,          "port=i"        => \$PORT,
29          "group=s"       => \$GROUP,          "group=s"       => \$GROUP,
# Line 25  GetOptions( Line 33  GetOptions(
33          "search=i"      => \$resend_search_delay,          "search=i"      => \$resend_search_delay,
34          "tftp=s"        => \$tftp_path,          "tftp=s"        => \$tftp_path,
35          "credentials"   => \$credentials,          "credentials"   => \$credentials,
36            "exec=s"        => \$flashed_cmd,
37            'plugins!'      => \$plugins,
38  );  );
39    
40  $quiet = 0 if $verbose;  $quiet = 0 if $verbose;
# Line 41  if (-e $passwd_path) { Line 51  if (-e $passwd_path) {
51          while(<$fh>) {          while(<$fh>) {
52                  chomp;                  chomp;
53                  next if /^#/ || /^$/ || /^\s+$/;                  next if /^#/ || /^$/ || /^\s+$/;
54                  if (/^\S+\s\S+$/) {                  if (/^\S+\s+\S+$/) {
55                          push @try_accounts, $_;                          push @try_accounts, $_;
56                  } else {                  } else {
57                          warn "invalid $passwd_path entry: $_\n";                          warn "invalid $passwd_path entry: $_\n";
58                  }                  }
59          }          }
60          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",
61                    $credentials ? " and display credentials" : "", "\n";
62  }  }
63    
64  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";
65    
66  sub fw {  sub fw {
67          my ($board, $offset,$len) = @_;          my ($board, $offset,$len) = @_;
# Line 90  sub ant2hash { Line 101  sub ant2hash {
101          return $hash;          return $hash;
102  }  }
103    
104    sub _dump_data {
105            my $data = shift;
106            $data =~ s/\s+/ /gs;
107            return substr($data,0,150);
108    }
109    
110  sub mdap_send {  sub mdap_send {
111          my $data = shift;          my $data = shift;
112    
# Line 101  sub mdap_send { Line 118  sub mdap_send {
118          if ($debug) {          if ($debug) {
119                  warn ">> ${GROUP}:${PORT} >> ", dump( $data ), $/;                  warn ">> ${GROUP}:${PORT} >> ", dump( $data ), $/;
120          } elsif( ! $quiet ) {          } elsif( ! $quiet ) {
121                  $data =~ s/\s+/ /gi;                  warn ">> ",_dump_data($data),$/;
                 warn ">> ", substr($data,0,70), $/;  
122          }          }
123  }  }
124    
# Line 151  sub forget_ant { Line 167  sub forget_ant {
167          delete $ant_ok_password->{$ant};          delete $ant_ok_password->{$ant};
168  }  }
169    
 my $once;  
   
 sub once {  
         my $m = join('', @_);  
         $once->{$m}++;  
         print $m if ($once->{$m} == 1);  
 }  
   
170  my $status = '';  my $status = '';
171    
172  sub status {  sub status {
# Line 191  while (1) { Line 199  while (1) {
199                  my $client_version = $h->{'MDAP-VERSION'};                  my $client_version = $h->{'MDAP-VERSION'};
200                  $mdap_ver = $client_version if ($client_version);                  $mdap_ver = $client_version if ($client_version);
201    
202                  print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet;                  print "<< $type $proto/$mdap_ver << ", length($data), " bytes: ",_dump_data($data),$/ unless $quiet;
203    
204                  warn dump( $data, $h ),$/ if ($debug);                  warn dump( $data, $h ),$/ if ($debug);
205    
# Line 200  while (1) { Line 208  while (1) {
208                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );                  next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# );
209    
210                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );                  my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h );
211                  my $seq_nr = $h->{'SEQ-NR'};                  my $seq_nr = $1 if (defined $h->{'SEQ-NR'} && $h->{'SEQ-NR'} =~ m/^-*(\d)+/);
212                  #warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr);                  #warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr);
213    
214                  my ($user_id,$user_pwd) = ant_credentials( $ant );                  my ($user_id,$user_pwd) = ant_credentials( $ant );
# Line 228  while (1) { Line 236  while (1) {
236                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";                                  my $build = $h->{'_BUILD'} || die "no _BUILD?";
237                                  my $new_build = fw_build( $board );                                  my $new_build = fw_build( $board );
238                                  if ( $build ne $new_build ) {                                  if ( $build ne $new_build ) {
239                                          print "+ $ant version $build -> $new_build\n";                                          print "+ $ant $board version $build -> $new_build\n";
240                                          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");                                          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");
241                                          $ant_flashing->{$ant}++;                                          $ant_flashing->{$ant}++;
242                                            next;
243                                  } else {                                  } else {
244                                          once "$ant OK version $build",                                          once "$ant $board OK version $build",
245                                                  $ant_unknown_password->{$ant} ? ' with unknown password' :                                                  $ant_unknown_password->{$ant} ? ' with unknown password' :
246                                                  $ant_passwd->{$ant} ? ' password protected' :                                                  $ant_passwd->{$ant} ? ' password protected' :
247                                                  '',                                                  '';
                                                 "\n";  
248    
249                                          $ant_flashing->{$ant} = 0;                                          $ant_flashing->{$ant} = 0;
250                                          # green|red|orange|flash|off                                          # green|red|orange|flash|off
251                                          mdap_send("EXEC-CLI MDAP/$mdap_ver\r\nCLI-CMD:system config led=flash\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n");                                          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");
252                                          my $waiting = 0;                                          my $waiting = 0;
253                                          my $count = 0;                                          my $count = 0;
254                                          map {                                          map {
# Line 248  while (1) { Line 256  while (1) {
256                                                  $count++;                                                  $count++;
257                                          } keys %$ant_flashing;                                          } keys %$ant_flashing;
258                                          if ($waiting == 0) {                                          if ($waiting == 0) {
259                                                  status "ALL $count ANTS FLASHED to $build\n";                                                  status "ALL $count ANTS FLASHED\n";
260                                                  $ant_flashing = undef;                                                  $ant_flashing = undef;
261                                          } else {                                          } else {
262                                                  status "$waiting of $count ants still flasing upto $build\n";                                                  status "$waiting of $count ants still flasing\n";
263                                            }
264                                    }
265    
266                                    if ( $plugins ) {
267                                            foreach my $plugin ( MDAP->plugins ) {
268                                                    warn "## calling $plugin\n" if $debug;
269                                                    if ( my $command = $plugin->check( $h ) ) {
270    
271                                                            warn ">> $plugin $ant $board ",
272                                                                    $h->{'ANT-NAME'}, " ",
273                                                                    $h->{'_BUILD'}, " ",
274                                                                    "\t:$command\n";
275                    
276                                                            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");
277                                                            last;
278                                                    }
279                                          }                                          }
280                                  }                                  }
281    
282                          } else {                          } else {
283                                  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";
284                          }                          }
285    
286                  } elsif ( $type eq 'REPLY-EXEC-CLI' ) {                  } elsif ( $type eq 'REPLY-EXEC-CLI' ) {
287                          print "+ $type ", dump( $data ), "\n" if ($verbose);                          print "##<< $type\n$data\n" if $debug;
288                          if ( $seq_nr == 1 ) {                          if ( $seq_nr == 1 ) {
289                                  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");                                  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");
290                                  forget_ant( $ant ) if ( $ant_flashing->{$ant} );                                  forget_ant( $ant ) if ( $ant_flashing->{$ant} );
291                            } elsif ( $seq_nr < 0 ) {
292                                    warn "EXEC-CLI failed: $data\n";
293                          }                          }
294    
295                  } else {                  } else {
296                          print "!! reply $type ignored ", dump( $h ), $/;                          print "!! reply $type ignored ", dump( $h ), $/;
297                  }                  }

Legend:
Removed from v.33  
changed lines
  Added in v.69

  ViewVC Help
Powered by ViewVC 1.1.26