Re: Convert CDIR notation to IP range

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxx



[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux