3 |
use strict; |
use strict; |
4 |
use IO::Socket::Multicast; |
use IO::Socket::Multicast; |
5 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
6 |
|
use Getopt::Long; |
7 |
|
|
8 |
use constant GROUP => '224.0.0.103'; |
my $GROUP = '224.0.0.103'; |
9 |
use constant PORT => '3235'; |
my $PORT = '3235'; |
10 |
|
|
11 |
my $local_port = 1000; |
my $debug = 0; |
12 |
|
my $quiet = 1; |
13 |
|
my $verbose = 0; |
14 |
|
|
15 |
my $resend_search_delay = 3; |
my $resend_search_delay = 3; |
16 |
|
my $tftp_path = '/srv/tftp/'; |
17 |
|
|
18 |
my $sock = IO::Socket::Multicast->new(LocalPort=>PORT,ReuseAddr=>1); |
GetOptions( |
19 |
$sock->mcast_add(GROUP) || die "Couldn't set group: $!\n"; |
"port=i" => \$PORT, |
20 |
|
"group=s" => \$GROUP, |
21 |
|
"debug!" => \$debug, |
22 |
|
"quiet!" => \$quiet, |
23 |
|
"verbose!" => \$verbose, |
24 |
|
"search=i" => \$resend_search_delay, |
25 |
|
"tftp=s" => \$tftp_path, |
26 |
|
); |
27 |
|
|
28 |
|
$quiet = 0 if $verbose; |
29 |
|
|
30 |
|
# tab-delimited list of user id/passwd to try on ants |
31 |
|
my @try_accounts = ( "Administrator\t" ); |
32 |
|
|
33 |
|
my $passwd_path = $0; |
34 |
|
$passwd_path =~ s/[^\/]+$/passwd/; |
35 |
|
|
36 |
|
if (-e $passwd_path) { |
37 |
|
open(my $fh, $passwd_path) || die "can't open $passwd_path: $!"; |
38 |
|
while(<$fh>) { |
39 |
|
chomp; |
40 |
|
next if /^#/ || /^$/ || /^\s+$/; |
41 |
|
if (/^\S+\t\S+$/) { |
42 |
|
push @try_accounts, $_; |
43 |
|
} else { |
44 |
|
warn "invalid $passwd_path entry: $_\n"; |
45 |
|
} |
46 |
|
} |
47 |
|
print "found ", $#try_accounts + 1, " accounts to try on password protected ants\n"; |
48 |
|
} |
49 |
|
|
50 |
|
warn "search for ants every ${resend_search_delay}s\ntftp server path: $tftp_path\n"; |
51 |
|
|
52 |
|
sub fw { |
53 |
|
my ($board, $offset,$len) = @_; |
54 |
|
open(my $fh, "$tftp_path/$board") || die "Can't open image $tftp_path/$board: $!"; |
55 |
|
my $b; |
56 |
|
seek($fh, $offset, 0) || die "can't seek to $offset: $!"; |
57 |
|
read($fh, $b, $len) || die "can't read $len bytes from $offset: $!"; |
58 |
|
close($fh); |
59 |
|
return $b; |
60 |
|
} |
61 |
|
|
62 |
|
sub fw_build { |
63 |
|
my $board_name = shift || return 0; |
64 |
|
my $v = join('.', unpack('CCCC',fw($board_name,0x20,4)) ); |
65 |
|
print "# fw_build $board_name $v\n" unless $quiet; |
66 |
|
return $v; |
67 |
|
} |
68 |
|
|
69 |
|
sub fw_exists { |
70 |
|
my $board = shift; |
71 |
|
return -e "$tftp_path/$board"; |
72 |
|
} |
73 |
|
|
74 |
|
my $sock = IO::Socket::Multicast->new(LocalPort=>$PORT,ReuseAddr=>1); |
75 |
|
$sock->mcast_add($GROUP) || die "Couldn't set group: $!\n"; |
76 |
$sock->mcast_ttl(1); |
$sock->mcast_ttl(1); |
77 |
|
|
78 |
sub ant2hash { |
sub ant2hash { |
89 |
|
|
90 |
sub mdap_send { |
sub mdap_send { |
91 |
my $data = shift; |
my $data = shift; |
92 |
warn ">> $data\n>>----------\n"; |
|
93 |
my $null = chr(0); |
my $xor = 0; |
94 |
$sock->mcast_send( "${data}", GROUP . ':' . PORT ); |
map { $xor ^= ord($_) } split(//,$data); |
95 |
|
$data .= sprintf('%02X', $xor); |
96 |
|
|
97 |
|
$sock->mcast_send( $data, "${GROUP}:${PORT}" ); |
98 |
|
if ($debug) { |
99 |
|
warn ">> ", dump( $data ), $/; |
100 |
|
} elsif( ! $quiet ) { |
101 |
|
$data =~ s/\s+/ /gi; |
102 |
|
warn ">> ", substr($data,0,70), $/; |
103 |
|
} |
104 |
|
} |
105 |
|
|
106 |
|
my $ant_passwd; |
107 |
|
my $ant_unknown_password; |
108 |
|
my $ant_ok_password; |
109 |
|
my $ant_flashing; |
110 |
|
|
111 |
|
sub ant_credentials { |
112 |
|
my $ant = shift || die "no ant?"; |
113 |
|
my $i = $ant_passwd->{$ant} || 0; |
114 |
|
my ($user_id,$user_pwd) = split(/\t/, $try_accounts[$i]); |
115 |
|
#warn "ant $ant as [$i] $user_id / $user_pwd\n"; |
116 |
|
return ($user_id,$user_pwd); |
117 |
|
} |
118 |
|
|
119 |
|
sub ant_unknown_password { |
120 |
|
my $ant = shift || die "no ant?"; |
121 |
|
if ( $ant_unknown_password->{$ant} ) { |
122 |
|
$ant_unknown_password->{$ant}--; |
123 |
|
} |
124 |
|
return $ant_unknown_password->{$ant}; |
125 |
|
} |
126 |
|
|
127 |
|
sub ant_another_passwd { |
128 |
|
my $ant = shift || die "no ant?"; |
129 |
|
|
130 |
|
return 0 if ant_unknown_password( $ant ); |
131 |
|
return 0 if $ant_ok_password->{$ant}; |
132 |
|
|
133 |
|
$ant_passwd->{$ant}++; |
134 |
|
|
135 |
|
if ( $ant_passwd->{$ant} > $#try_accounts ) { |
136 |
|
print "$ant ant with unknown password\n"; |
137 |
|
$ant_unknown_password->{$ant} = 10; |
138 |
|
$ant_passwd->{$ant} = 0; |
139 |
|
return 0; |
140 |
|
} |
141 |
|
return 1; |
142 |
|
} |
143 |
|
|
144 |
|
sub forget_ant { |
145 |
|
my $ant = shift || die "no ant?"; |
146 |
|
delete $ant_unknown_password->{$ant}; |
147 |
|
delete $ant_passwd->{$ant}; |
148 |
|
delete $ant_ok_password->{$ant}; |
149 |
|
} |
150 |
|
|
151 |
|
my $once; |
152 |
|
|
153 |
|
sub once { |
154 |
|
my $m = join('', @_); |
155 |
|
$once->{$m}++; |
156 |
|
print $m if ($once->{$m} == 1); |
157 |
|
} |
158 |
|
|
159 |
|
my $status = ''; |
160 |
|
|
161 |
|
sub status { |
162 |
|
my $m = join('', @_); |
163 |
|
if ($m ne $status) { |
164 |
|
print $m; |
165 |
|
$status = $m; |
166 |
|
} |
167 |
} |
} |
168 |
|
|
169 |
local $SIG{ALRM} = sub { |
local $SIG{ALRM} = sub { |
170 |
mdap_send("ANT-SEARCH MDAP/1.1\r\n46"); |
mdap_send("ANT-SEARCH MDAP/1.1\r\n"); |
171 |
alarm( $resend_search_delay ); |
alarm( $resend_search_delay ); |
172 |
}; |
}; |
173 |
|
|
174 |
alarm( $resend_search_delay ); |
alarm( $resend_search_delay ); |
175 |
|
|
176 |
|
mdap_send("ANT-SEARCH MDAP/1.1\r\n"); |
177 |
|
|
178 |
while (1) { |
while (1) { |
179 |
my $data; |
my $data; |
180 |
next unless $sock->recv($data,1024); |
next unless $sock->recv($data,1024); |
181 |
|
|
182 |
if ( $data =~ m#^(REPLY-\S+)\s(MDAP/\d+\.\d+)# ) { |
if ( $data =~ m#^(INFO|ANT-SEARCH|EXEC-CLI|REPLY-\S+)\s(MDAP)/(\d+\.\d+)# ) { |
183 |
|
|
184 |
my ($type,$proto) = ($1,$2); |
my ($type,$proto,$mdap_ver) = ($1,$2,$3); |
185 |
|
|
186 |
my $h = ant2hash($data); |
my $h = ant2hash($data); |
187 |
|
|
188 |
print "# $type $proto ", dump( $h ), $/; |
my $client_version = $h->{'MDAP-VERSION'}; |
189 |
|
$mdap_ver = $client_version if ($client_version); |
190 |
|
|
191 |
|
print "<< $type $proto/$mdap_ver << ", length($data), " bytes\n" unless $quiet; |
192 |
|
|
193 |
|
warn dump($h),$/ if ($debug); |
194 |
|
|
195 |
|
# we are getting our own messages (since our source port |
196 |
|
# is same as destination) |
197 |
|
next if ( $type =~ m#^(INFO|ANT-SEARCH|EXEC-CLI)# ); |
198 |
|
|
199 |
|
my $ant = $h->{'ANT-ID'} || die "no ANT-ID in ", dump( $h ); |
200 |
|
my $seq_nr = $h->{'SEQ-NR'}; |
201 |
|
#warn "SEQ-NR: $seq_nr ok: ",$ant_ok_password->{$ant},"\n" if ($seq_nr); |
202 |
|
|
203 |
|
my ($user_id,$user_pwd) = ant_credentials( $ant ); |
204 |
|
|
205 |
if ($type eq 'REPLY-ANT-SEARCH') { |
if ($type eq 'REPLY-ANT-SEARCH') { |
206 |
mdap_send("INFO MDAP/1.2 |
mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n") unless ant_unknown_password( $ant ); |
|
SEQ-NR:1 |
|
|
TO-ANT:0644JTHJ4 |
|
|
USER-ID:Administrator |
|
|
USER-PWD:\r\n22"); |
|
207 |
} elsif ($type eq 'REPLY-INFO') { |
} elsif ($type eq 'REPLY-INFO') { |
|
if ( |
|
|
$h->{'_BUILD'} eq '6.1.7.2' && |
|
|
$h->{'_BUILDNAME'} eq 'ZZO9AA6.172' && |
|
|
$h->{'_BUILDVARIANT'} eq 'AA' && |
|
|
1 |
|
|
) { |
|
|
mdap_send("EXEC-CLI MDAP/1.2 |
|
|
CLI-CMD:software upgrade |
|
|
SEQ-NR:1 |
|
|
TO-ANT:0644JTHJ4 |
|
|
USER-ID:Administrator |
|
|
USER-PWD:\r\n1F"); |
|
|
} else { |
|
208 |
|
|
209 |
|
if ( $seq_nr < 0 ) { |
210 |
|
# if ( $ant_ok_password ) { |
211 |
|
# $ant_ok_password->{$ant} = 0; |
212 |
|
# } elsif ( ant_another_passwd( $ant ) ) { |
213 |
|
if ( ant_another_passwd( $ant ) ) { |
214 |
|
($user_id,$user_pwd) = ant_credentials( $ant ); |
215 |
|
mdap_send("INFO MDAP/$mdap_ver\r\nSEQ-NR:1\r\nTO-ANT:$ant\r\nUSER-ID:$user_id\r\nUSER-PWD:$user_pwd\r\n"); |
216 |
|
} |
217 |
|
next; |
218 |
|
} else { |
219 |
|
$ant_ok_password->{$ant}++; |
220 |
} |
} |
221 |
|
|
222 |
|
my $board = $h->{'_BOARD_NAME'} || die "no _BOARD_NAME?"; |
223 |
|
if ( fw_exists( $board ) ) { |
224 |
|
my $build = $h->{'_BUILD'} || die "no _BUILD?"; |
225 |
|
my $new_build = fw_build( $board ); |
226 |
|
if ( $build ne $new_build ) { |
227 |
|
print "+ $ant version $build -> $new_build\n"; |
228 |
|
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"); |
229 |
|
} else { |
230 |
|
once "$ant OK version $build", |
231 |
|
$ant_unknown_password->{$ant} ? ' with unknown password' : |
232 |
|
$ant_ok_password->{$ant} ? ' password protected' : |
233 |
|
'', |
234 |
|
"\n"; |
235 |
|
$ant_flashing->{$ant} = 0; |
236 |
|
my $waiting = 0; |
237 |
|
my $count = 0; |
238 |
|
map { |
239 |
|
$waiting++ if ($ant_flashing->{$_}); |
240 |
|
$count++; |
241 |
|
} keys %$ant_flashing; |
242 |
|
if ($waiting == 0) { |
243 |
|
status "ALL $count ANTS FLASHED to $build\n"; |
244 |
|
$ant_flashing = undef; |
245 |
|
} else { |
246 |
|
status "$waiting of $count ants still flasing upto $build\n"; |
247 |
|
} |
248 |
|
} |
249 |
|
} else { |
250 |
|
once "!! NO FIRMWARE for $board in $tftp_path for ant $ant, skipping update\n"; |
251 |
|
} |
252 |
|
} elsif ( $type eq 'REPLY-EXEC-CLI' && $h->{'SEQ-NR'} == 1 ) { |
253 |
|
print "+ $ant bootp mode re-flash started\n" if ($verbose); |
254 |
|
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"); |
255 |
|
forget_ant( $ant ); |
256 |
|
$ant_flashing->{$ant}++; |
257 |
} else { |
} else { |
258 |
print "!! reply ignored\n"; |
print "!! reply ignored ", dump( $h ), $/; |
259 |
} |
} |
260 |
|
|
261 |
} else { |
} else { |
262 |
print $data, "\n", "-" x 30, "\n"; |
warn "<=" x 15, "\n", $data, "\n", "<=" x 15, "\n"; |
263 |
} |
} |
264 |
} |
} |