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

Contents of /lib/MDAP/ChangeIP.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 85 - (show annotations)
Wed Dec 5 23:13:38 2007 UTC (16 years, 4 months ago) by dpavlin
File size: 1567 byte(s)
use NetAddr::IP to assign IP addresses to unknown ants
1 package MDAP::ChangeIP;
2
3 use strict;
4 use warnings;
5
6 use Module::Pluggable;
7 use MDAP;
8 use NetAddr::IP;
9
10 # start IPs
11 my $ip1 = NetAddr::IP->new('192.168.1.242/24');
12 my $ip2 = NetAddr::IP->new('10.0.0.1/8');
13
14 my @serials = ( qw/
15 CP0636JT3SH
16 CP0644JTHJ4
17 CP0624BH55U
18 CP0703KT3PP
19 /);
20
21 my $serial2ip;
22
23 foreach my $serial ( @serials ) {
24 $serial2ip->{$serial} = $ip1++ . ';' . $ip2++;
25 }
26
27 sub check {
28 my ( $self, $h ) = @_;
29
30 my $serial = $h->{'_PROD_SERIAL_NBR'} || die "no serial?";
31 my $ip_list = $h->{'IP_LIST'} || die "no IP_LIST";
32
33 if ( my $ip = $serial2ip->{$serial} ) {
34 my $ip_only = $ip;
35 $ip_only =~ s/\/\d+//g;
36 if ( $ip eq $ip_list || $ip_only eq $ip_list ) {
37 once "OK ",__PACKAGE__," $serial $ip_list";
38 return;
39 }
40
41 warn "## change_ip $serial ",
42 $h->{'_VARIANT_FRIENDLY_NAME'}, " ",
43 $h->{'_BOARD_NAME'}, " ",
44 $h->{'_BUILD'}, " ",
45 "IP $ip_list -> $ip\n" if $debug;
46
47 my $ip_status;
48 $ip_status->{$_}-- foreach ( split(/;/, $ip_list ) );
49 $ip_status->{$_}++ foreach ( split(/;/, $ip_only ) );
50 foreach my $current_ip ( sort { $ip_status->{$b} <=> $ip_status->{$a} } keys %$ip_status ) {
51 if ( $ip_status->{$current_ip} < 0 ) {
52 warn "-- remove IP $current_ip\n" if $debug;
53 return "ip ipdelete addr=$current_ip";
54 } elsif ( $ip_status->{$current_ip} > 0 ) {
55 warn "++ add IP $current_ip\n" if $debug;
56 return "ip ipadd intf=LocalNetwork addr=$current_ip";
57 }
58 }
59 return;
60 } else {
61 once "WARNING: unknown serial $serial assigning $ip1;$ip2";
62 $serial2ip->{$serial} = $ip1++ . ';' . $ip2++;
63 return;
64 }
65 }
66
67 1;

  ViewVC Help
Powered by ViewVC 1.1.26