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

Hi, Im now trying to generate a set of IP addresses, from 192.168.0.1, 192.168.0.2....upto 192.168.0.254 and then 192.168.1.1, 192.168.1.2 etc upt 192.168.1.254 and so on. I used the split command but for some reason am unable to transition from 192.168.0.254 to 192.168.1.1. Any help would be appreciated. Here is the code
print ("Enter the number of XML files required to be created:"); $number=<STDIN>; print ("\nEnter the name of the XML file:"); $file_name=<STDIN>; chomp($file_name); $xml=".xml"; $" = ':'; for ($i=1; $i<=$number; $i++) { $files="$file_name$i$xml"; open(FILE, ">$files") or die "Can't open the file: $i\n"; $first="SCS"; $leading = sprintf("%07d", $i); $firstname="$first$leading"; print FILE "\n <firstname>$firstname</firstname> "; $mac = sprintf("%012d", $i); @split = ($mac =~ /../g); $ip=sprintf("192168%06d",$i); @splitip= ($ip =~ /.../g); @splitipfinal= join('.', @splitip) . "\n"; print FILE "\n <name>NEW - @split</name> "; print FILE "\n <ipaddress>@splitipfinal</ipaddress> "; } print "\nThe XMLs have been created, check in the same directory!!";
And Prasad, sorry if I asked. Ive got a ton to do, in a short period of time, with my limited knowledge of perl. I always try before I ask. Thanks anyways... The next time you see me around, dont bother ok!

Replies are listed 'Best First'.
Re: IP address generation
by Fletch (Bishop) on May 24, 2005 at 12:18 UTC

    Also handy (but no one's mentioned yet) is Net::Netmask which will enumerate a given net block.

Re: IP address generation
by demerphq (Chancellor) on May 24, 2005 at 12:25 UTC

    Maybe this will help:

    use strict; use warnings; sub make_ip_list { my ( $from, $to )=@_; $_ = unpack "N", pack "C4", split /\./, $_ for $from, $to; do { print join(".",unpack "C4", pack "N", $from++ ),"\n"; } until $from > $to; } make_ip_list("0.0.0.0","0.0.1.0");
    ---
    $world=~s/war/peace/g

Re: IP address generation
by moot (Chaplain) on May 24, 2005 at 14:12 UTC
    The very useful NetAddr::IP has some methods to walk through an IP block:
    use NetAddr::IP; my $ip = NetAddr::IP->new("192.168.0.0/16"); my $bcast = $ip->broadcast; while ($ip < $bcast) { # do something with $ip $ip++; }
Re: IP address generation
by castaway (Parson) on May 24, 2005 at 11:54 UTC
    Some code you tried would also be appreciated, especially if you have already attempted it yourself, that way we can correct you, and you will learn something. (As opposed to just getting a solution and using it)

    C.

Re: IP address generation
by holli (Abbot) on May 24, 2005 at 12:10 UTC
    Guessing wild I'd say you used something like
    @a = split /./, "192.168.0.1";
    which does not work as you might think, because the first parameter of split is a regular expression. Hence the dot is special and you must escape it.
    @a = split /\./, "192.168.0.1";


    holli, /regexed monk/
Re: IP address generation
by blazar (Canon) on May 24, 2005 at 12:02 UTC
    What is "and so on"? IIUC, how 'bout
    map { my $i=$_; map "192.168.$i.$_", 1..254 } 0..255;
    ?
Re: IP address generation
by prasadbabu (Prior) on May 24, 2005 at 12:03 UTC

    You should try something, as castaway suggested, we are not here to do your homework.

    Anyhow it is not appreciated in this forum.

    If i understood your question correctly, this should work.

    for $c (0..255) { for $t (1..254) { $a= "192.168.".$c.".".$t; push (@a , $a); } } $"= "\n"; print "@a";

    updated:

    Prasad