
sh.ajay12
User
Dec 30, 2013, 3:16 AM
Post #23 of 30
(8257 views)
|
Re: [sh.ajay12] Grow, Shrink, Combine Ip address net mask
[In reply to]
|
Can't Post
|
|
Hello FishMonger and Laurent, Here is the code to delete an ip address. Similarly i want to write different subroutines to GROW and SHRINK and COMBINE subnets. I would request you to please let me know, on which subroutine should i first start my work. GROW,SHRINK,OR COMBINE. If you have suggestions, please provide me.
sub remove_address { $BLOCK=new2 Net::Netmask("$ADDRESS"); if ( ! defined($BLOCK) ) { print_error("Bad Address: $ADDRESS"); print_error("END"); return(); } $OK=0; $SQL="select ip, devicename from cpe_device_copy where shortname = '$CUSTOMER'"; $STH=$DBH->prepare($SQL); $STH->execute(); sql_check(); while ( @ROW=$STH->fetchrow_array() ) { if ( $BLOCK->match($ROW[0]) ) { print_error("Device $ROW[0] $ROW[1] is using the range you wish to remove"); $OK++; } } if ( $OK ) { print_error("END"); return; } $SQL="select address from customer_address_copy where shortname = '$CUSTOMER'"; $STH=$DBH->prepare($SQL); $STH->execute(); sql_check(); undef($R); while ( @ROW=$STH->fetchrow_array() ) { $T=new2 Net::Netmask("$ROW[0]"); if ( $T->match($BLOCK->base()) && $T->bits()<=$BLOCK->bits() ) { print_error(sprintf("Remove ajay %s From $ROW[0]",$BLOCK->desc())); if ( defined($R) ) { print_error("Error: More than one block matches"); print_error("END"); return(); } $R=$ROW[0]; } } if ( ! defined($R) ) { print_error("ERROR: No Blocks Match"); print_error("END"); return(); } $RBLOCK=new2 Net::Netmask("$R"); $BITS=$BLOCK->bits(); foreach $IP ( $RBLOCK->enumerate($BITS) ) { $LIST{$IP}=1; } $LIST{$BLOCK->base()}=0; foreach $IP ( keys(%LIST) ) { next if ( $LIST{$IP}==0 ); $BLOCKS[$#BLOCKS+1]=new Net::Netmask("$IP/$BITS"); } $LASTUPDATE=`date`; chomp($LASTUPDATE); $LASTUPDATE="$USERNAME: $LASTUPDATE"; foreach $BLOCK ( cidrs2cidrs(@BLOCKS) ) { print_error(sprintf("INFO: Add %s<br>\n",$BLOCK->desc())); $SQL=sprintf("insert into customer_address_copy ( shortname, address, lastupdate ) values ( '$CUSTOMER','%s','$LASTUPDATE' )",$BLOCK->desc()); $DBH->do($SQL); sql_check(); } print_error(sprintf("Remove Ajay %s<br>\n",$RBLOCK->desc())); $SQL=sprintf("delete from customer_address_copy where shortname='$CUSTOMER' and address='%s'",$RBLOCK->desc()); #printf("$SQL"); $DBH->do($SQL); sql_check(); print_error("END"); }
(This post was edited by FishMonger on Dec 30, 2013, 8:04 AM)
|