1 |
package MDAP::ChangeIP; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Module::Pluggable; |
7 |
use MDAP; |
8 |
|
9 |
my $serial2ip = { |
10 |
'CP0636JT3SH' => '192.168.1.242;10.0.0.1', |
11 |
'CP0644JTHJ4' => '192.168.1.243;10.0.0.2', |
12 |
'CP0624BH55U' => '192.168.1.244;10.0.0.3', |
13 |
'CP0703KT3PP' => '10.0.0.4', |
14 |
}; |
15 |
|
16 |
sub check { |
17 |
my ( $self, $h ) = @_; |
18 |
|
19 |
my $serial = $h->{'_PROD_SERIAL_NBR'} || die "no serial?"; |
20 |
my $ip_list = $h->{'IP_LIST'} || die "no IP_LIST"; |
21 |
|
22 |
if ( my $ip = $serial2ip->{$serial} ) { |
23 |
if ( $ip eq $ip_list ) { |
24 |
once "OK ",__PACKAGE__," $serial $ip_list"; |
25 |
return; |
26 |
} |
27 |
|
28 |
warn "## change_ip $serial ", |
29 |
$h->{'_VARIANT_FRIENDLY_NAME'}, " ", |
30 |
$h->{'_BOARD_NAME'}, " ", |
31 |
$h->{'_BUILD'}, " ", |
32 |
"IP $ip_list -> $ip\n" if $debug; |
33 |
|
34 |
my $ip_status; |
35 |
$ip_status->{$_}-- foreach ( split(/;/, $ip_list ) ); |
36 |
$ip_status->{$_}++ foreach ( split(/;/, $ip ) ); |
37 |
foreach my $current_ip ( sort { $ip_status->{$b} <=> $ip_status->{$a} } keys %$ip_status ) { |
38 |
if ( $ip_status->{$current_ip} < 0 ) { |
39 |
warn "-- remove IP $current_ip\n" if $debug; |
40 |
return "ip ipdelete addr=$current_ip"; |
41 |
} elsif ( $ip_status->{$current_ip} > 0 ) { |
42 |
my $mask = ''; |
43 |
$mask = '/8' if $current_ip =~ m/^10\./; |
44 |
$mask = '/24' if $current_ip =~ m/^192\.168\./; |
45 |
warn "++ add IP $current_ip$mask\n" if $debug; |
46 |
return "ip ipadd intf=LocalNetwork addr=$current_ip$mask"; |
47 |
} |
48 |
} |
49 |
return; |
50 |
} else { |
51 |
once "ERROR: can't find serial2ip mapping for $serial IP $ip_list"; |
52 |
return; |
53 |
} |
54 |
} |
55 |
|
56 |
1; |