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

Hi Monks,

The requirement is to generate sequential Class B ip addresses with the "number of ip addresses" as the input.

If the number of ip addresses is 254 , the addresses generated should be 192.168.1.1 , 192.168.1.2,....192.168.1.254.
If the number of ip addresses is 500 , the addresses generated should be 192.168.1.1, .. ,192.168.1.254,192.168.2.1, .. 192.168.2.246
And so on.

I have written part code which generates sequential ip addresses for 254 address. Not able to generate for a number greater than 254.

my $_first_octet_num = "1"; my $_ip_address; foreach (1..254){ $_ip_address = join ('.', 192,168,$_second_octet_num,$_first_o +ctet_num++) if ($_ <= 254); print "\n$_ip_address\n"; }

Help and Advice is appreciated !

Thanks in Advance!

Replies are listed 'Best First'.
Re: Generation of sequential IP addresses for Class B network
by irah (Pilgrim) on Jun 24, 2009 at 08:41 UTC

    You have not declared the variable, $_second_octet_num variable. I don't know how your program is generating 192.168.1.1 to 192.168.1.254 without that.

    Here I am giving the code for generating a IP address from 192.168.1.1 to 192.168.254.254. You can adjust the code according to your situation.

    #!/usr/bin/perl use strict; use warnings; my $first_octet_num = "1"; my $ip_address; foreach (1..254) { foreach (1..255) { if ($_ <= 254) { $ip_address = join ('.', 192,168,$first_octet_num,$_); print "$ip_address\n"; } else { $first_octet_num++; } } }

    One more information for you, IP address start with 192, it's not class B. It's class C. Reference here.

      One more information for you, IP address start with 192, it's not class B. It's class C.

      The size of a network is no longer determined by the first octet.

      The private use space at 192.168 is large enough to host a class B (/16) network if desired. Of course, the OP is clearly requesting a series of class C (/24) networks based on his requirements. Odd, since he obviously sees it at one large network.

      Really sorry , clumsy at pasting the code. I dont know how i forgot to paste the declaration.

      Thanks for the code though, i managed to tailor it to my needs.

      Cheers!

Re: Generation of sequential IP addresses for Class B network
by Mr. Muskrat (Canon) on Jun 24, 2009 at 15:02 UTC
Re: Generation of sequential IP addresses for Class B network
by Bloodnok (Vicar) on Jun 24, 2009 at 12:21 UTC
    Are you sure you want the range to start at 1 ?

    IIRC, that's the standard gateway IP address for the network...

    A user level that continues to overstate my experience :-))
      Not particluar whether the ip address starts from 1 or not.

      im just creating dummy ip addresses as part of the automation framework that i am building.

Re: Generation of sequential IP addresses for Class B network
by Anonymous Monk on Jun 24, 2009 at 07:58 UTC
    Not able to generate for a nuber greater than 254.

    Why not? All you do is put number of ip addresses into a variable ... and you're programming :)

      Because the programming logic needs to implement the following for ip address numbers greater than 254 say 500:

      When the first 254 ip addresses are generated, the third octet should be incremented by 1 once and fourth octet should be reset to 1 and increment by 1 till it reaches 254.Ans this pattern should continue.

      Looking for a script to do the same.

        This site is not a script writing service. You are supposed to do some programming and problem solving yourself.

        What have you tried to increase the third octet by 1 whenever the fourth octet reaches 254 and there are still IP addresses to be generated? It sounds to me as if nested loops would help you here, or simply treating an IP address as one number of four "digits" that go from 0 to 255 instead of treating it as four separate numbers.