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

Hey all.. I am trying to reset IP address of all PCs in the work- group. I have selected some criteria for that.. e.g management Group : 100.1.3.41 to 100.1.3.50 or so support staff 100.1.4.41 to ..... I have a text document in which I have all the records( IP n hostname ,different text files for management and support staff n so on) I want to have some perl script that will assign automatic IP address. e.g host1_001 100.22.22.11 (management) will change to 100.1.3.41 and the next management workstation will get the next available IP address. So Is there any way we can achieve so in Perl... I will really appreciate your help.. Thanks..

Replies are listed 'Best First'.
Re: Automatic IP address Generation
by ikegami (Patriarch) on Jul 30, 2008 at 00:30 UTC
    The dotted form of IP addresses is just a representation of a 32-bit number. You'll have better luck working with an IP address as a number.
    use Socket qw( inet_aton inet_ntoa ); sub fr_dotted { unpack 'N', inet_aton @_ ? $_[0] : $_ } sub to_dotted { inet_ntoa pack 'N', @_ ? $_[0] : $_ } my $next = fr_dotted('100.1.3.41'); my $max = fr_dotted('100.1.3.50'); die if $next > $max; my $new = $next++; print(to_dotted($new), "\n");

    Update: Had inet_aton where inet_ntoa should have been used.

      Hi Ikegami, Thanks for your help. My result.txt file contains data like
      host1-001 10.22.33.22 host2-001 10.33.68.254
      something like above , And as i told you before I want to have update that with
      host1-001 10.1.3.41 host2-001 10.1.3.42
      so in your above code , where should i give the result.txt( which contains old IP as argument)...I really dont have idea. Thanks,
      Hi Ikegami , I have tried out the below.
      #!/usr/bin/perl open(HANDLE , "result3.txt") || die("Could not open file!"); while(<HANDLE>) { chomp; use Socket qw( inet_aton inet_ntoa ); sub fr_dotted { unpack 'N', inet_aton @_ ? $_[0] : $_ } sub to_dotted { inet_aton pack 'N', @_ ? $_[0] : $_ } my $next = fr_dotted('100.1.3.41'); my $max = fr_dotted('100.1.3.50'); die if $next > $max; my $new = $next++; $new1 = to_dotted($new); print $new1; } close(HANDLE);
      But that does not give me the desired results. Am I doing it correct ?
        #!/usr/bin/perl use strict; use warnings; use Socket qw( inet_aton inet_ntoa ); sub fr_dotted { unpack 'N', inet_aton @_ ? $_[0] : $_ } sub to_dotted { inet_ntoa pack 'N', @_ ? $_[0] : $_ } my $next = fr_dotted('100.1.3.41'); my $max = fr_dotted('100.1.3.50'); while (<>) { chomp; my ($host) = split(' '); die("Exceed max\n") if $next > $max; my $addr = $next++; print($host, ' ', to_dotted($addr), "\n"); }
        renum.pl infile > outfile -or- perl -i.bak renum.pl file

        Untested.

Re: Automatic IP address Generation
by NetWallah (Canon) on Jul 30, 2008 at 01:47 UTC
    It sounds like you are looking for a Windows DHCP server that can handle reservations.

    On the perl side, if you want to examine IP addresses, subnets etc, I recommend a module like NetAddr::IP.

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

Re: Automatic IP address Generation
by MidLifeXis (Monsignor) on Jul 30, 2008 at 00:06 UTC

    It may make your life easier to assign groups based on base-2 rather than base-10.

    --MidLifeXis