Discussion:
Converting decimal dotted subnet mask to CIDR in a script
BSD Freak
2002-06-07 04:04:39 UTC
Permalink
Hi all,

I have a variable in a script we use

$MASK=255.255.255.0

Is there a utility or an easy way to process $MASK and get

$MASKCIDR=24

the CIDR value of 255.255.255.0


Many thanks...

---------------------------------------------------------------------
Faxes delivered directly to any email address, new to mBox!
Find out more http://www.mbox.com.au/fax

To Unsubscribe: send mail to ***@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
Mike Roest
2002-06-07 04:32:03 UTC
Permalink
You can
1)split each of the octects at the decimals.
2)convert each resulting numbers to binary
3)count the number of ones in the 4 binary numbers.
4)bang you have the result.

This procedure "should" work for all subnet masks
-Mike

-----Original Message-----
From: owner-freebsd-***@FreeBSD.ORG
[mailto:owner-freebsd-***@FreeBSD.ORG] On Behalf Of BSD Freak
Sent: Thursday, June 06, 2002 10:05 PM
To: FreeBSD Questions
Subject: Converting decimal dotted subnet mask to CIDR in a script


Hi all,

I have a variable in a script we use

$MASK=255.255.255.0

Is there a utility or an easy way to process $MASK and get

$MASKCIDR=24

the CIDR value of 255.255.255.0


Many thanks...

---------------------------------------------------------------------
Faxes delivered directly to any email address, new to mBox!
Find out more http://www.mbox.com.au/fax

To Unsubscribe: send mail to ***@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



To Unsubscribe: send mail to ***@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
Mike Roest
2002-06-07 04:49:49 UTC
Permalink
Here's a quick python script that does it. You should be able to port
this to any language fairly easily

--code--
#!/usr/local/bin/python

from string import *
subnet = "255.255.255.0"

(octet1,octet2,octet3,octet4) = subnet.split(".")

octet1 = int(octet1)
octet2 = int(octet2)
octet3 = int(octet3)
octet4 = int(octet4)

worker = octet1
count = 0
while worker != 0:
if worker % 2 == 1:
count = count + 1
worker = worker/2

worker = octet2
while worker != 0:
if worker % 2 == 1:
count = count +1
worker = worker/2

worker = octet3
while worker != 0:
if worker % 2 == 1:
count = count +1
worker = worker/2

worker = octet4
while worker != 0:
if worker % 2 == 1:
count= count+1
worker = worker/2

print count

--end code--

--Mike
-----Original Message-----
From: owner-freebsd-***@FreeBSD.ORG
[mailto:owner-freebsd-***@FreeBSD.ORG] On Behalf Of Mike Roest
Sent: Thursday, June 06, 2002 10:32 PM
To: 'BSD Freak'; 'FreeBSD Questions'
Subject: RE: Converting decimal dotted subnet mask to CIDR in a script


You can
1)split each of the octects at the decimals.
2)convert each resulting numbers to binary
3)count the number of ones in the 4 binary numbers.
4)bang you have the result.

This procedure "should" work for all subnet masks
-Mike

-----Original Message-----
From: owner-freebsd-***@FreeBSD.ORG
[mailto:owner-freebsd-***@FreeBSD.ORG] On Behalf Of BSD Freak
Sent: Thursday, June 06, 2002 10:05 PM
To: FreeBSD Questions
Subject: Converting decimal dotted subnet mask to CIDR in a script


Hi all,

I have a variable in a script we use

$MASK=255.255.255.0

Is there a utility or an easy way to process $MASK and get

$MASKCIDR=24

the CIDR value of 255.255.255.0


Many thanks...

---------------------------------------------------------------------
Faxes delivered directly to any email address, new to mBox!
Find out more http://www.mbox.com.au/fax

To Unsubscribe: send mail to ***@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



To Unsubscribe: send mail to ***@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



To Unsubscribe: send mail to ***@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
Warren Block
2002-06-07 05:43:26 UTC
Permalink
Post by BSD Freak
I have a variable in a script we use
$MASK=255.255.255.0
Is there a utility or an easy way to process $MASK and get
$MASKCIDR=24
the CIDR value of 255.255.255.0
Okay:

#!/usr/bin/perl -w
($byte1, $byte2, $byte3, $byte4) = split(/\./, $ARGV[0].".0.0.0.0");
$num = ($byte1 * 16777216) + ($byte2 * 65536) + ($byte3 * 256) + $byte4;
$bin = unpack("B*", pack("N", $num));
$count = ($bin =~ tr/1/1/);
print "$bin = $count bits\n";

The first line is fluff to allow command-line operation. The second
line converts dotted quad to plain decimal. The third line converts
decimal to a binary string. Line four counts the set bits.

However, no test is made on the validity of the mask--the bits should be
contiguous from the left. I'm pretty sure there's an easy regular
expression which would do that, but it's getting late.

-Warren Block * Rapid City, South Dakota USA




To Unsubscribe: send mail to ***@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message
parv
2002-06-07 07:25:46 UTC
Permalink
in message <***@mbox.com.au>,
wrote BSD Freak thusly...
Post by BSD Freak
I have a variable in a script we use
$MASK=255.255.255.0
Is there a utility or an easy way to process $MASK and get
$MASKCIDR=24
the CIDR value of 255.255.255.0
in addition to a general solution, python & perl programs, there is
at least one port on the subject: net/ipcalc ...

# ipcalc -b 0/255.255.255.0

Address: 0.0.0.0
Netmask: 255.255.255.0 = 24
Wildcard: 0.0.0.255
=>
Network: 0.0.0.0/24
Broadcast: 0.0.0.255
HostMin: 0.0.0.1
HostMax: 0.0.0.254
Hosts/Net: 254


--


To Unsubscribe: send mail to ***@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message

Loading...