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