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

#!/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.

Replies are listed 'Best First'.
Re^4: Automatic IP address Generation
by ajd335 (Novice) on Jul 30, 2008 at 22:53 UTC
    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 that's the thing I am searching for. It should be something like this : when it moves with next ++ , it should check if IP is already assigned to any workstation ( May be checking DNS records - Dns is running on Windows 2003) . If so , It should skip it and move for other one... I am wondering , is it possible to check the DNS records someway?
Re^4: Automatic IP address Generation
by ajd335 (Novice) on Jul 30, 2008 at 20:56 UTC
    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/;