1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#!/usr/bin/perl -w
# Copyright (c) 2000 RIPE NCC
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of the author not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
# AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#------------------------------------------------------------------------------
# Module Header
# Filename : ipcount.pl
# Purpose : IP addresses calculator
# Author : Manuel Valente <manuel@ripe.net>
# Date : 20000329
# Description :
# Language Version : Perl 5
# OSs Tested : BSDI 3.1
# Command Line :
# Input Files :
# Output Files :
# External Programs : Net::IP.pm
# Problems :
# To Do :
# Comments :
# $Id: ipcount,v 1.3 2002/10/15 09:18:14 manuel Exp $
#------------------------------------------------------------------------------
use strict;
use Math::BigInt;
use Net::IP qw(:PROC);
use Getopt::Std;
my %opts;
getopts ('rd:',\%opts);
scalar (@ARGV) < 1 and usage();
my $arg = join '',@ARGV;
$arg =~ s/\s+//g;
my $ip = new Net::IP($arg) or die ("Cannot create IP object $arg: ".Error());
my @list = $ip->find_prefixes() or die ($ip->error());
# Cut down the supplied range in smaller prefixes
if ($opts{d})
{
if (scalar(@list) > 1)
{
$ip->set ($list[0]) or die (Error());
warn ("\nWarning: The supplied Range does not fit in one single prefix.\n");
warn ("I will use the first prefix only (".$ip->print.")\n\n");
}
my $size = new Math::BigInt (2);
$size = $size->bpow (ip_iplengths($ip->version) - $opts{d}) - 1;
my $current = new Net::IP($ip->ip);
my $last = new Net::IP($ip->last_ip);
my $new_ip = new Net::IP(0);
my $count;
while ($current->bincomp ('lt', $last))
{
$new_ip->set($current->last_ip.'+'.$size) or die (Error());
print $new_ip->print,"\n";
if ($opts{r})
{
print $new_ip->reverse_ip,"\n";
}
$current->set($new_ip->last_ip .'+ 1') or die (Error());
$count++;
}
printf ("\nFound %s /%ss in %s\n\n",$count, $opts{d}, $ip->print);
exit;
}
my ($addr,@pr,$tot);
foreach (@list)
{
$addr = new Net::IP ($_) or die ("Cannot create IP object $_: ".Error());
printf ("%18s %15s - %-15s [%s]\n",$addr->print(),$addr->ip(),$addr->last_ip(), $addr->size());
if ($opts{r})
{
print $addr->reverse_ip,"\n";
}
$tot += $addr->size();
push (@pr,'/'.$addr->prefixlen());
};
if (scalar(@list) > 1)
{
print "\n";
printf ("%18s %15s - %-15s [%s]\n",$ip->ip().(join ',',@pr),$ip->ip(),$ip->last_ip(),$tot);
};
# Print usage and die
sub usage
{
print "
Usage:
ipcount [-r] [-d <prefix>] address
-r: Print Reverse Ranges
-d <prefix>: Cut down the original prefix in several prefixes
The address range can be one of:
ipcount IP + size
ipcount IP1 - IP2
ipcount IP/len
";
exit (1);
};
|