in reply to Re: Automatic IP address Generation
in thread Automatic IP address Generation

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 ?

Replies are listed 'Best First'.
Re^3: Automatic IP address Generation
by ikegami (Patriarch) on Jul 30, 2008 at 20:21 UTC
    #!/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.

      Hi ikegami, I have removed the blank space part. But , One more thing , I also need to check if the IP is already assigned someone or not..If it is assigned to some workstation , then that IP add should be skipped and the next available IP address should be taken. Thanks,
        How do you know if it's been assigned?
      Hi Ikegami , Yes it gives me answer . But the thing is it is also taking the white spaces and also assign them the IP address. i mean it also take the blank lines into consideration
        It doesn't take blank lines into consideration because you didn't mention the possibility of blank lines. Inserting the following at the start of the loop will solve that problem.
        next if /^\s*\z/;