Tuesday, September 13, 2011

Calculate IP's between specified range

If you want to calculate all IP's between the specified range then following perl script can be used.
#!/usr/bin/perl
$numArgs = $#ARGV + 1;
if ($numArgs ne "2"){
        print "USAGE: perl $0 <start ip address> <end ip address>\n";
        exit;
}

$sip=$ARGV[0];
$eip=$ARGV[1];

($sip1,$sip2,$sip3,$sip4)=split(/[.]/,$sip);
($eip1,$eip2,$eip3,$eip4)=split(/[.]/,$eip);

$next_ip=$sip;
$end_ip=$eip;
while ($next_ip ne $end_ip){

        $next_ip="$sip1.$sip2.$sip3.$sip4";
        print $next_ip."\n";

        $sip4++;
        if ( $sip4 > 255){
                $sip4=0;
                $sip3 =$sip3 + 1;
        }

        if ( $sip3 > 255){
                $sip3=0;
                $sip2 =$sip2 + 1;
        }

       if ( $sip2 > 255){
                $sip2=0;
                $sip1 =$sip1 + 1;
        }

}
Usage
perl create_ip_ranges.pl 192.168.0.0  192.168.0.2
will return
192.168.0.0
192.168.0.1
192.168.0.2

No comments:

Post a Comment