Convert CDIR notation to IP range

John Nichel john at kegworks.com
Wed Jul 28 17:11:05 UTC 2004


Alexander Dalloz wrote:
> #!/usr/bin/perl -w
> # $Id: cidr,v 0.1 2002/08/26 15:18:45 gm Exp $
> # convert CIDR notation to network/broadcast/netmask notation
> $Usage =  "usage: $0 [-s] ipaddr/bits\n\tipaddr in dotted squad\n";
> $ARGV[0] or die $Usage; shift && $set++ if $ARGV[0] eq '-s';
> $ARGV[0] or die $Usage; ($hostaddr,$bits) = split(/\//,$ARGV[0]);
> (($hostaddr =~ /^(\d{1,3}\.){3}\d{1,3}$/) && $bits =~ /^\d{1,2}$/ ) ||
> die $Usage;
> print "$ARGV[0]: " unless $set;
> $addr = pack("C4",split(/\./,$hostaddr));
> $net = $addr & ($mask = pack("N",-(1<<(32-($bits % 32)))));
> $brc = $addr | ~ $mask;
> for($net,$brc,$mask){ $_ = join('.', unpack("C4",$_)) }
> print $set ? "$hostaddr" : "network $net", " broadcast $brc netmask
> $mask\n";
> 
> [ hope it survives line wrapping ]
> 
> Alexander
> 
> 
> 

Cool, that works nicely.  Thanks.

-- 
John C. Nichel
ÜberGeek
KegWorks.com
716.856.9675
john at kegworks.com





More information about the fedora-list mailing list