giany has asked for the wisdom of the Perl Monks concerning the following question:

I have this problem generating a file ..
I have 22 clases and foreach ip i have to generate those 3 lines. The problem is that from those 22 clases i have 3 clases..(6,17,19) that have 1 or 2 subclasses.. and the ip must be smth like 10.x.1.y or 10.x.2.y. So for 3 I have exceptions..

How can I fix this problem?

This is the code I wrote for all 22 classes with no exceptions.
my $DEV = "dev eth1"; my $count=500; for ($x = 1;$x < 23;$x++) { for ($y = 1;$y < 255;$y++) { print HTB "\n class add $DEV parent 1:7 classid 1:0x".$count." rate +4kbit ceil 512Kbit burst 4kbit prio 1 quantum 2000\n"; print HTB " filter add $DEV protocol ip parent 1:0 prio 1 u32 match +ip dst 10.".$x.".0.".$y."/32 match ip src 0.0.0.0/0 flowid 1:0 x".$count."\n"; print HTB " qdisc add $DEV parent 1:0x".$count." handle 0x".$count++ +.": pfifo limit 5\n"; } }

2006-02-10 Retitled by planetscape, as per Monastery guidelines
Original title: 'Generate problem'

Replies are listed 'Best First'.
Re: IP generation algorithm problem
by NetWallah (Canon) on Feb 09, 2006 at 22:04 UTC
    Here is an outline, to get you started ..
    use strict; my %exception_second_octet = map {$_=> "Put Subclass Info here"} qw( 6 + 17 19); for my $second_octet (1..23){ if ($exception_second_octet{$second_octet}){ # Put Subclass generation code here } else { for my $fourth_octet(1..255){ my $generated_ip = "10.$second_octet.0.$fourth_octet/32"; # Do whatever else ... } } }
    UPDATE: Shouldn't your fourth octet be limited to 253, to avoid the multicast and broadcast addresses for a class-C network ?

         "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

Re: IP generation algorithm problem
by Util (Priest) on Feb 10, 2006 at 14:54 UTC

    My solution is similar to NetWallah's, but it allows subnet customization in the %special_subnets data, rather than in code.

    Since you are pre-pending '0x' to $count, I added code to convert $count to hex first; ignore if inappropriate.

    Working, lightly tested code:

    my $DEV = 'dev eth1'; my $count = 500; my %special_subnets = ( 6 => [ 0, 1, 2], 17 => [ 0, 1], 19 => [ 0, 1, 2], ); foreach my $oct1 ( 10 ) { foreach my $oct2 ( 1 .. 22 ) { my $third_octets_aref = $special_subnets{$oct2} || [ 0 ]; foreach my $oct3 ( @{ $third_octets_aref } ) { foreach my $oct4 ( 1 .. 254 ) { # 253 ?? print HTB generate_rule( $DEV, $count, $oct1, $oct2, $oct3, $o +ct4 ); $count++; } } } } sub generate_rule { my ( $dev, $count, @octets ) = @_; my $ip = join '.', @octets; # my $countx = '0x' . $count; my $countx = sprintf '0x%04x', $count; return <<"END_RULE"; class add $dev parent 1:7 classid 1:$countx rate 4kbit ceil 512Kbit +burst 4kbit prio 1 quantum 2000 filter add $dev protocol ip parent 1:0 prio 1 u32 match ip dst $ip/32 + match ip src 0.0.0.0/0 flowid 1:$countx qdisc add $dev parent 1:$countx handle $countx: pfifo limit 5 END_RULE }

      There seems to be a problem and I can`t figure it out.. This is the code:
      use strict; use warnings; system('/usr/bin/clear'); print "\n\n"; open(IN1,"/root/work/htb/banner"); open(HTB,">/root/work/htb/full"); my @head_htb=<IN1>; close(IN1); print HTB "@head_htb"; my $DEV = 'dev eth1'; my $count = 500; my %special_subnets = ( 6 => [ 0, 1, 2], 17 => [ 0, 1], 19 => [ 0, 1, 2], ); foreach my $oct1 ( 10 ) { foreach my $oct2 ( 1 .. 22 ) { my $third_octets_aref = $special_subnets{$oct2} || [ 0 ]; foreach my $oct3 ( @{ $third_octets_aref } ) { foreach my $oct4 ( 1 .. 254 ) { # 253 ?? print HTB generate_rule( $DEV, $count, $oct1, $oct2, $oct +3, $oct4 ); $count++; } } } } sub generate_rule { my ( $dev, $count, @octets ) = @_; my $ip = join '.', @octets; #my $countx = '0x' . $count; my $countx = sprintf '0x%04x', $count; return <<"RULE"; class add $dev parent 1:7 classid 1:$count rate 4kbit ceil 512 +Kbit burst 4kbit prio 1 quantum 2000 filter add $dev protocol ip parent 1:0 prio 1 u32 match ip dst + $ip/32 match ip src 0.0.0.0/0 flowid 1:$count qdisc add $dev parent 1:$count handle $countx: pfifo limit 5 RULE } close (HTB);
      And the error is :
      Can't find string terminator "RULE" anywhere before EOF at genhtb.pl l +ine 44.
      As far as I know a ENTER must be pressed after RULE so perl can reconize it as a string terminator..
        I`ve fixed : there must be no space in front of RULE. Thx for help!